How to make the carGroup, and carGroup1 keep bouncing and when they reach and touch the border or the touch each other, they rotate to the opposite side, I am new to JavaFX and searched about the collision problem but I got confused on how to make these 2 cars bounce and whenever they touch either the border or each other, they rotate to the opposite side and continue bouncing. Here is my code in which I tried to create the 2 cars groups and animate them, but I face difficulties and the bouncing and collision parts
What I have tried:
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ShapeTest extends Application {
@Override
public void start(Stage stage) {
Pane pane = new Pane();
Scene scene = new Scene(pane, 300, 300);
Rectangle body = new Rectangle(150, 50, 100, 50);
body.setFill(Color.RED);
Circle wheel1 = new Circle(175, 100, 15);
Circle wheel2 = new Circle(225, 100, 15);
Rectangle roof = new Rectangle(160, 30, 80, 20);
roof.setFill(Color.DARKBLUE);
Rectangle window1 = new Rectangle(170, 35, 25, 15);
Rectangle window2 = new Rectangle(205, 35, 25, 15);
Shape windows = Shape.union(window1, window2);
windows.setFill(Color.LIGHTBLUE);
Group carGroup = new Group();
carGroup.getChildren().addAll(body, wheel1, wheel2, roof, windows);
carGroup.setLayoutX(5);
carGroup.setLayoutY(5);
pane.getChildren().add(carGroup);
pane.setBorder(new Border(new BorderStroke(Color.RED,
BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
carGroup.relocate(5, 5);
KeyValue kvX = new KeyValue(carGroup.layoutXProperty(), pane.getWidth() - pane.getBorder().getInsets().getRight() - carGroup.getBoundsInLocal().getMaxX());
KeyFrame kfX = new KeyFrame(Duration.seconds(1), kvX);
Timeline timelineX = new Timeline(kfX);
timelineX.setCycleCount(Timeline.INDEFINITE);
KeyValue kvY = new KeyValue(carGroup.layoutYProperty(), pane.getHeight() - pane.getBorder().getInsets().getBottom() - carGroup.getBoundsInLocal().getMaxY());
KeyFrame kfY = new KeyFrame(Duration.seconds(1), kvY);
Timeline timelineY = new Timeline(kfY);
timelineY.setCycleCount(Timeline.INDEFINITE);
timelineX.play();
timelineY.play();
Rectangle body1 = new Rectangle(150, 50, 100, 50);
body1.setFill(Color.YELLOW);
Circle wheel11 = new Circle(175, 100, 15);
Circle wheel22 = new Circle(225, 100, 15);
Rectangle roof1 = new Rectangle(160, 30, 80, 20);
roof1.setFill(Color.CYAN);
Rectangle window11 = new Rectangle(170, 35, 25, 15);
Rectangle window22 = new Rectangle(205, 35, 25, 15);
Shape windows1 = Shape.union(window11, window22);
windows1.setFill(Color.PINK);
Group carGroup1 = new Group();
carGroup1.getChildren().addAll(body1, wheel11, wheel22, roof1, windows1);
carGroup1.setLayoutX(5);
carGroup1.setLayoutY(5);
pane.getChildren().add(carGroup1);
carGroup1.relocate(20, 20);
KeyValue kvX1 = new KeyValue(carGroup1.layoutXProperty(), pane.getWidth() - pane.getBorder().getInsets().getRight() - carGroup1.getBoundsInLocal().getMaxX());
KeyFrame kfX1 = new KeyFrame(Duration.seconds(1), kvX1);
Timeline timelineX1 = new Timeline(kfX);
timelineX1.setCycleCount(Timeline.INDEFINITE);
KeyValue kvY1 = new KeyValue(carGroup1.layoutYProperty(), pane.getHeight() - pane.getBorder().getInsets().getBottom() - carGroup1.getBoundsInLocal().getMaxY());
KeyFrame kfY1 = new KeyFrame(Duration.seconds(1), kvY1);
Timeline timelineY1 = new Timeline(kfY1);
timelineY1.setCycleCount(Timeline.INDEFINITE);
timelineX1.play();
timelineY1.play();
stage.setTitle("Moving Cars");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}