Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Java
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);

        //=======================================================================
        // CAR 1
        // create the car body
        Rectangle body = new Rectangle(150, 50, 100, 50);
        body.setFill(Color.RED);

        // create the wheels
        Circle wheel1 = new Circle(175, 100, 15);
        Circle wheel2 = new Circle(225, 100, 15);

        // create the car roof
        Rectangle roof = new Rectangle(160, 30, 80, 20);
        roof.setFill(Color.DARKBLUE);

        // create the windows
        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);

        // create the car group and add the shapes to it
        Group carGroup = new Group();
        carGroup.getChildren().addAll(body, wheel1, wheel2, roof, windows);
        carGroup.setLayoutX(5); // initial X position
        carGroup.setLayoutY(5); // initial Y position

        // add the car group to the pane
        pane.getChildren().add(carGroup);

        pane.setBorder(new Border(new BorderStroke(Color.RED,
                BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));

        carGroup.relocate(5, 5);

        // move the carGroup along the X axis
        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);

        // move the carGroup along the Y axis
        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();

        //=======================================================================
        // CAR 2
        // create the car body
        Rectangle body1 = new Rectangle(150, 50, 100, 50);
        body1.setFill(Color.YELLOW);

        // create the wheels
        Circle wheel11 = new Circle(175, 100, 15);
        Circle wheel22 = new Circle(225, 100, 15);

        // create the car roof
        Rectangle roof1 = new Rectangle(160, 30, 80, 20);
        roof1.setFill(Color.CYAN);

        // create the windows
        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);

        // create the car group and add the shapes to it
        Group carGroup1 = new Group();
        carGroup1.getChildren().addAll(body1, wheel11, wheel22, roof1, windows1);
        carGroup1.setLayoutX(5); // initial X position
        carGroup1.setLayoutY(5); // initial Y position

        // add the car group to the pane
        pane.getChildren().add(carGroup1);

        carGroup1.relocate(20, 20);

        // move the carGroup along the X axis
        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);

        // move the carGroup along the Y axis
        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();

        //=======================================================================
        // set the stage properties and show it
        stage.setTitle("Moving Cars");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Posted
Updated 2-Apr-23 22:51pm

1 solution

Whenever your objects move you need to check their edges to avoid collisions. So if an object is moving to the right, you need to check that its rightmost edge is not at the rightmost edge of the window. When moving up, you check the top edges, etc. When an object hits the edge of a window then you need to change its direction; either exact opposite or some random direction. In the latter case that simulates a ball bouncing around a closed room. You should work on getting a single object working first before you try to simulate multiple object collisions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900