在JavaFX的碰撞检测器(二维迷宫) [英] Collision detector in javafx (2d maze)

查看:2576
本文介绍了在JavaFX的碰撞检测器(二维迷宫)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让我的球反弹的对象在屏幕上?

下面的图片是怎样的程序应工作一旦球的一个很好的例子,运行到一个障碍。

我发的球反弹的墙壁,但剩下的就是使它也反弹的对象。感谢您的帮助!

下面是源$ C ​​$ C:

 公共类2DGAME扩展应用{

    公共静态圈圈;
    公共静态窗格帆布;
    私人柜长= 0;
    双X = 0;
    双Y = 0;

    @覆盖
    公共无效启动(阶段primaryStage){

        帆布=新的窗格();
        一幕一幕=新场景(帆布,800,600);

        primaryStage.setTitle(2D球赛);
        primaryStage.setScene(场景);
        primaryStage.show();

        圈=新圆(20,Color.BLUE);
        circle.relocate(100,100);

        最后的矩形R =新的Rectangle(20,20,Color.DARKMAGENTA);
        r.setLayoutX(400);
        r.setLayoutY(300);

        。canvas.getChildren()和addAll(圈);
        。canvas.getChildren()的addAll(r)的;


        时间轴循环=新的时间线(新的关键帧(Duration.millis(5),新的事件处理程序< ActionEvent的>(){

            @覆盖
            公共无效手柄(ActionEvent的T){
                如果(计数器++%5 == 0){
                    //移动球取决于X和Y的值
                    circle.setLayoutX(circle.getLayoutX()+ X);
                    circle.setLayoutY(circle.getLayoutY()+ Y);

// code弹开墙
                    最后的边界范围= canvas.getBoundsInLocal();
                    布尔leftWall = circle.getLayoutX()&其中; =(bounds.getMinX()+ circle.getRadius());
                    布尔topWall = circle.getLayoutY()&其中; =(bounds.getMinY()+ circle.getRadius());
                    布尔rightWall = circle.getLayoutX()> =(bounds.getMaxX() -  circle.getRadius());
                    布尔bottomWall = circle.getLayoutY()> =(bounds.getMaxY() -  circle.getRadius());

   //窃听,不知道如何使它工作
                    最终边界rectangleBounds = r.getLayoutBounds();
                    布尔rectangle_left = circle.getLayoutX()&其中; =(rectangleBounds.getMinX()+ circle.getRadius());
                    布尔rectangle_right = circle.getLayoutX()> =(rectangleBounds.getMaxX() -  circle.getRadius());


   //如果底部或顶壁已被触摸,球反转方向。
                    如果(bottomWall || topWall){

                        Y = Y * -1;
                    }
    //如果左侧或右侧壁已被触摸,球反转方向。
                    如果(leftWall || rightWall){
                        X = X * -1;
                    }

    //窃听$ C $下boucning关障碍目标
                    如果(rectangle_left || rectangle_right){
                       // X = X *  -  1;
                    }

                }
            }

        }));

        loop.setCycleCount(Timeline.INDEFINITE);
        loop.play();
    }

    公共静态无效的主要(字串[] args){
        启动(参数);
    }

}
 

解决方案

您可以做,与相交()方法。

 布尔movingDown = TRUE;

无效checkforCollision(){
如果(circle.intersects(bottomWall.getBoundsInLocal()){
      !movingDown = movingDown;
      // 做一点事
    }

否则如果(circle.intersects(rectangle.getBoundsInParent())及&安培;!movingDown {
      !movingDown = movingDown;
      // 做一点事
    }
// 等等..
}
 

How do I make my ball bounce off objects on the screen?

The picture below is a good example of how the program should be working once the ball runs into an obstacle.

I made the ball bounce off the walls, but what's left is making it also bounce off objects. Thanks for the help!

Here's the source code:

public class 2DGAME extends Application {

    public static Circle circle;
    public static Pane canvas;
    private long counter = 0;
    double X = 0;
    double Y = 0;

    @Override
    public void start(Stage primaryStage) {

        canvas = new Pane();
        Scene scene = new Scene(canvas, 800, 600);

        primaryStage.setTitle("2D Ball Game");
        primaryStage.setScene(scene);
        primaryStage.show();

        circle = new Circle(20, Color.BLUE);
        circle.relocate(100, 100);

        final Rectangle r = new Rectangle(20, 20, Color.DARKMAGENTA);
        r.setLayoutX(400);
        r.setLayoutY(300);

        canvas.getChildren().addAll(circle);
        canvas.getChildren().addAll(r);


        Timeline loop = new Timeline(new KeyFrame(Duration.millis(5), new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                if (counter++ % 5 == 0) {
                    // Moves the ball depending on the values of X and Y
                    circle.setLayoutX(circle.getLayoutX() + X);
                    circle.setLayoutY(circle.getLayoutY() + Y);

//Code to bounce off walls
                    final Bounds bounds = canvas.getBoundsInLocal();
                    boolean leftWall = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius());
                    boolean topWall = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius());
                    boolean rightWall = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius());
                    boolean bottomWall = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius());

   //Bugged and not sure how to make it work
                    final Bounds rectangleBounds = r.getLayoutBounds();
                    boolean rectangle_left = circle.getLayoutX() <= (rectangleBounds.getMinX() + circle.getRadius());
                    boolean rectangle_right = circle.getLayoutX() >= (rectangleBounds.getMaxX() - circle.getRadius());


   //If the bottom or top wall has been touched, the ball reverses direction.
                    if (bottomWall || topWall) {

                        Y = Y * -1; 
                    }
    // If the left or right wall has been touched, the ball reverses direction.
                    if (leftWall || rightWall) {
                        X = X * -1;
                    }

    //Bugged code for boucning off obstacle object
                    if (rectangle_left || rectangle_right) {
                       // X = X * - 1;
                    }

                }
            }

        }));

        loop.setCycleCount(Timeline.INDEFINITE);
        loop.play();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

解决方案

You could do that with the intersects() method.

boolean movingDown = true;

void checkforCollision(){
if(circle.intersects(bottomWall.getBoundsInLocal()){
      movingDown = !movingDown;
      // do something
    }

else if(circle.intersects(rectangle.getBoundsInParent()) && !movingDown{
      movingDown = !movingDown;
      // do something
    }
// etc..
}

这篇关于在JavaFX的碰撞检测器(二维迷宫)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆