有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检测矩形之间的冲突?

我想检测rect何时碰到障碍物(obst1和obst2,obst3暂时未添加)。我的障碍随着路径的转变而移动。所有的形状都是矩形。 提前谢谢你
这是我的代码:

Rectangle rect= new Rectangle(100,225,50,125);
     Rectangle obst1=new Rectangle(1200,300,30,50);
     Rectangle obst2=new Rectangle(1200,300,91,51);
     Rectangle obst3=new Rectangle(1200,300,20,50); 

     ImagePattern one = new ImagePattern(new Image("1.png"));
     ImagePattern two = new ImagePattern(new Image("2.png"));
     ImagePattern cactus1 = new ImagePattern(new Image("1cactus.png"));
     ImagePattern cactus4 = new ImagePattern(new Image("4cactus.png"));
     rect.setFill(one);`enter code here`

     rect.setArcHeight(5);
     rect.setArcWidth(5);
     //rect.setFill(Color.VIOLET);
     obst1.setFill(cactus1);
     obst3.setFill(RED);
     obst2.setFill(cactus4);

     PathTransition path = new PathTransition();
     path.setPath(l2);
     path.setNode(obst1);
     path.setDuration(Duration.millis(3000));
     path.setCycleCount(Timeline.INDEFINITE);
     path.setRate(-0.5);
     //path.play();

     PathTransition path2 = new PathTransition();
     path2.setPath(l2);
     path2.setNode(obst2);
     path2.setDuration(Duration.millis(3000));
     path2.setCycleCount(Timeline.INDEFINITE);
     path2.setRate(-0.6);
     //path2.play();

     PathTransition path3 = new PathTransition();
     path3.setPath(l2);
     path3.setNode(obst2);
     path3.setDuration(Duration.millis(3000));
     path3.setCycleCount(Timeline.INDEFINITE);
     path3.setRate(-0.3);
     //path3.play();

     int TimeLine=2;

     Pane pane = new Pane();
     pane.getChildren().add(obst1);
     pane.getChildren().add(obst2);
     //pane.getChildren().add(obst3);
     pane.getChildren().add(rect);
     pane.getChildren().add(l1);

     Scene scene = new Scene(pane, 300, 250);

     TranslateTransition tt = new TranslateTransition(Duration.millis(300));
     tt.setFromY(00f);
     tt.setToY(-100f);
     tt.setCycleCount(TimeLine);
     tt.setAutoReverse(true);

     ParallelTransition pt = new ParallelTransition(rect, tt); 

     scene.setOnKeyPressed(e->
        {
            if(e.getCode()==KeyCode.UP)
            {
                pt.play();
            }
            if(e.getCode()==KeyCode.ESCAPE)
            {
                     path.pause();
                     path2.pause();
                     path3.pause();
                     pt.pause();
            }
            if(e.getCode()==KeyCode.ENTER)
            {
                     path.play();
                     path2.play();
                     path3.play();
                     pt.play();
            }
        });


        EventHandler<ActionEvent> eventHandler = e -> 
                {
                    if(img==1)
                    {
                        rect.setFill(two);
                        img=2;
                    }
                    else
                    {
                        rect.setFill(one);
                        img=1;
                    }
                };

                Timeline animation = new Timeline(
                    new KeyFrame(Duration.millis(80), eventHandler));
                animation.setCycleCount(Timeline.INDEFINITE);
                animation.play();





        primaryStage.setTitle("Dinausor");
        primaryStage.setScene(scene);
        primaryStage.show();

共 (1) 个答案

  1. # 1 楼答案

    如果所有形状都是Rectangle对象,则可以使用intersects(...)方法检查碰撞。下面是一个例子:

    Rectangle r1 = new Rectangle(0,0,100,100);
    Rectangle r2 = new Rectangle(50,50,100,100);
    System.out.println(r1.intersects(r2));// prints "true".