(JavaFX)如何检查动画圈之间的碰撞?

时间:2014-11-29 23:51:30

标签: java javafx collision

我的任务是有一个按钮,用于添加出现在随机位置和方向的球。我在检查球之间的碰撞时遇到了麻烦。麻烦就在第123行。谢谢!

        import java.util.ArrayList;
        import java.util.Scanner;
        import javafx.animation.KeyFrame;
        import javafx.animation.Timeline;
        import javafx.beans.property.DoubleProperty;
        import javafx.event.ActionEvent;
        import javafx.scene.layout.Pane;
        import javafx.util.Duration;

        class BallPane extends Pane {

            private double radius = 10;

            private ArrayList<Ball> list = new ArrayList();
            private Timeline animation;

            public BallPane() {
                Scanner kb = new Scanner(System.in);
                System.out.println("\nPlease press RIGHT and LEFT arrow keys to increase and decrease"
                        + " the speed of the balls.");
                animation = new Timeline(
                        new KeyFrame(Duration.millis(50), (ActionEvent e) -> {
                            moveBall();
                        }));
                animation.setCycleCount(Timeline.INDEFINITE);
                animation.play(); // Start animation
            }

            public void addBall() {
                Ball balls = new Ball(BallPane.this);
                getChildren().add(balls.getCircle());
                list.add(balls);
            }

            public void moveBall() {

                double x, y, dx, dy;
                // Check boundaries

                Ball a;
                for (int i = 0; i < list.size(); i++) {
                    a = list.get(i);
                    x = a.getCircle().getCenterX();
                    y = a.getCircle().getCenterY();
                    dx = a.getDirectionX() + (int) Math.random() * 100;
                    dy = a.getDirectionY() + (int) Math.random() * 100;

                    if (x < a.getCircle().getRadius()
                            || x > getWidth() - a.getCircle().getRadius()) {

                        dx *= -1; // Change ball move direction
                    }
                    if (y < a.getCircle().getRadius()
                            || y > getHeight() - a.getCircle().getRadius()) {
                        dy *= -1; // Change ball move direction
                    }
                    // Adjust ball position
                    x += dx;
                    y += dy;
                    a.setDirectionX(dx);
                    a.setDirectionY(dy);
                    a.getCircle().setCenterX(x);
                    a.getCircle().setCenterY(y);
                    // Ball b = list.get(i + 1);
                }
        //line 123
//also trying to use the distance formula to check the distance between two circles' center points
               for (int i = 0; i < Timeline.INDEFINITE; i++) {
                    if (Math.sqrt(Math.pow(list.get(i).getCenterX()
                            - list.get(i + 1).getCenterX(), 2) + Math.pow(list.get(i).getCenterY()
                                    - list.get(i + 1).getCenterY(), 2)) <= 2 * radius) {
                        list.get(i).getCircle();
                        getChildren().remove(i);
                        list.remove(i);
                    }
                }
            }
        }//End of BallPane class.

感谢您的帮助!它主要是代码Stack Overflow ...你为什么要让我输入this.asdl; kfj dsal; kf jldfjals; kdjfldksajf l; askfj

3 个答案:

答案 0 :(得分:0)

试试这个:

   ArrayList<Integer> ballexplode=new ArrayList<Integer>();
   for (int i = 0; i < list.size()-1; i++) {
     for (int j=i+1; j < list.size();j++){
        if (Math.sqrt(Math.pow(list.get(i).getCenterX()
                - list.get(j).getCenterX(), 2) + Math.pow(list.get(i).getCenterY()
                        - list.get(j).getCenterY(), 2)) <= 2 * radius) {
            list.get(i).getCircle();
            ballexplode.add(i);
            ballexplode.add(j);
        }
      }
    }

        for (int i=ballexplode.size()-1;i>=0;i--){
        getChildren().remove(ballexplode.get(i));
        list.get(ballexplode.get(i)).getCircle();
         }

答案 1 :(得分:0)

通过颠倒循环顺序尝试从Mailkov的原始帖子修改以下代码

   for (int i = list.size()-1; i >=1 ; i--)
   {
     for (int j=(i-1); j >=0; j--)
     {
        if (Math.sqrt(Math.pow(list.get(i).getCenterX() - list.get(j).getCenterX(), 2) + 
            Math.pow(list.get(i).getCenterY() - list.get(j).getCenterY(), 2)) <= 2 * radius )
        {
            list.get(i).getCircle();
            getChildren().remove(i);
            list.remove(i);
            break; // I think we need to break out the for (j) loop here
        }
      }
    }

答案 2 :(得分:0)

我提出了解决方案: 谢谢您的帮助!

for (int i = list.size()-1; i >= 1; i--) {
                for (int j = (i - 1); j >= 0; j--) {
                    if (Math.sqrt(Math.pow(list.get(i).getCircle().getCenterX() - list.get(j).getCircle().getCenterX(), 2)
                            + Math.pow(list.get(i).getCircle().getCenterY() - list.get(j).getCircle().getCenterY(), 2)) <= 2 * radius) {

                        Circle oneBall = list.get(i).getCircle();
                        this.getChildren().remove(oneBall);
                        list.remove(i);
                        break; // I think we need to break out the for (j) loop here
                    }
                }
            }