0

I am making game and it brings colored balls up from the center of bottom with your mouse click. I have created classes according to the color of balls(Circle). I am using just Pane and using path transition to move balls. But I want to detect the collision with each new moving balls with existing (already added added balls to the pane). So far i could not find a way to detect that. How to do that? I made two lists. One containing the added balls and another for total balls from which each new balls are coming.

Ball classes

public class Ball extends Circle{
private String color;

public Ball(String color){
    this.color = color;
}

public String getColor() {
    return color;
}

}

Each balls of different colors..

public class BlueBall extends Ball{
private Image imgBlueBall = 
        new Image("/resources/blue.png");

 public BlueBall() {
    super("blue");
    ImagePattern bluePattern = new ImagePattern(imgBlueBall);
    setFill(bluePattern);
    setRadius(20);

 }

}

so like RedBall, GreenBall etc..

This is the application class

public class BallGameExe extends Application{
    private Pane root = new Pane();
    private boolean isIntersected;
    private static List<Ball> ballsList = new ArrayList<>();
    private ArrayList<Ball> addedBallsList = new ArrayList<>();
    private int index = 0;

@Override
public void start(Stage primaryStage) throws Exception {
    makeBallsList();
    root.setId("root");

    Path path = new Path();
    path.setStroke(Color.TRANSPARENT);
    MoveTo moveTo = new MoveTo();
    moveTo.setX(400.0f);
    moveTo.setY(600.0f);

    root.setOnMouseClicked( e -> {
        LineTo lineTo = new LineTo();
        lineTo.setX(e.getX());
        lineTo.setY(e.getY());

        Ball ball = ballsList.get(index);

        PathTransition pt = new PathTransition();
        pt.setDuration(Duration.millis(2000));
        pt.setNode(ball);
        pt.setPath(path);

        LineTo lineTo2 = new LineTo();

        if(e.getX() < 20 ){
            lineTo2.setX((780 - e.getX()) - (800 - e.getY()));
            lineTo2.setY(0);

            path.getElements().clear();
            path.getElements().add(moveTo);
            path.getElements().add(lineTo);
            path.getElements().add(lineTo2);

            pt.setPath(path);
            pt.play();

            root.getChildren().remove(path);
            root.getChildren().add(path);
            root.getChildren().add(ball);
            addedBallsList.add((Ball) ball);

        }else if( e.getX() > 780){
            lineTo2.setX((e.getX() - 20) - (e.getY() - 0));
            lineTo2.setY(0);

            path.getElements().clear();
            path.getElements().add(moveTo);
            path.getElements().add(lineTo);
            path.getElements().add(lineTo2);

            pt.setPath(path);
            pt.play();

            root.getChildren().remove(path);
            root.getChildren().add(path);
            root.getChildren().add(ball);
            addedBallsList.add((Ball) ball);

        }else {             
            path.getElements().clear();
            path.getElements().add(moveTo);
            path.getElements().add(lineTo);

            pt.setPath(path);
            pt.play();

            root.getChildren().remove(path);
            root.getChildren().add(path);
            root.getChildren().add(ball);
            addedBallsList.add((Ball) ball);

        }

        index++;    

    });

    Scene scene = new Scene(root, 800, 600);
    scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
    primaryStage.setTitle("BubblesGridPane");
    primaryStage.setScene(scene);
    primaryStage.show();
}

I have another method which basically create an array of 200 balls and make it random. Because I am planning to have different levels having 200 balls coming at each level. S this method populate the ArrayList ballsList.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Thomson Mathew
  • 419
  • 1
  • 9
  • 28
  • You didn't get any answers previously probably because 1)javafx Pane or whatever is completely irrelevant here since this is more of a general question about circle collision and 2)because of 1) there are plenty of questions + answers on this topic here on SO and you can also get a huge amount of tutorials by simply using Google or any other tool for searching online. – rbaleksandar Sep 12 '16 at 03:48
  • Thanks for comment. And obviously its not about simple circle collision. Its more about the pane i am using. How to get the nodes and make collision on the pain. If it was involving with couple of nodes i would have done that. I am not into gaming but I don't want to give up on this. – Thomson Mathew Sep 12 '16 at 05:33
  • You can take a look on this: http://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx – DVarga Sep 12 '16 at 05:57
  • See also this related [Q&A](http://stackoverflow.com/q/39185306/230513). – trashgod Sep 12 '16 at 08:36
  • Thank you very much guys. It helped me very much. – Thomson Mathew Sep 12 '16 at 14:48

0 Answers0