0

I currently have a ball bouncing off the walls of the canvas. I added a rectangle in the middle of the screen. Whenever the ball collides with the rectangle, I want it to bounce off it too, but I don't know how to do that. I have a rectangle called "r".

How do I make the ball treat the rectangle as a wall and change direction whenever it hits it? Code examples will be greatly appreciated. Thanks.

Here's my code for the ball bouncing off the walls:

public void handle(ActionEvent t) {

                    // Moves the ball depending on the values of X and Y
                    circle.setLayoutX(circle.getLayoutX() + X);
                    circle.setLayoutY(circle.getLayoutY() + Y);                        

                    final Bounds bounds = canvas.getBoundsInLocal();
           // Boolean values to check if a wall has been hit
                    boolean leftWall = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); 
                    boolean topWall = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius());
                    boolean rightWall = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius());
                    boolean bottomWall = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius());



                    // If the bottom or top wall has been touched, the ball reverses direction.
                    if (bottomWall || topWall) {

                        Y = Y * -1;
                    }
                    // If the left or right wall has been touched, the ball reverses direction.
                    if (leftWall || rightWall) {
                        X = X * -1;
                    }
                }              

        }));

        loop.setCycleCount(Timeline.INDEFINITE);
        loop.play();
    }
Gregg1989
  • 675
  • 6
  • 14
  • 24

2 Answers2

1

I don't know JavaFx but here is the idea :

while (1) {
    bool btop = pos.y >= top
    bool bbottom = pos.y <= bottom
    bool bleft = pos.x <= left
    bool bright = pos.x >= right
    bool rect_btop = pos.y <= rect_top && pos.x >= rect_left && pos.x <= rect_right
    bool rect_bbottom = pos.y <= rect_bottom && pos.x >= rect_left && pos.x <= rect_right
    bool rect_bright = pos.x <= rect_right && pos.y >= rect_bottom && pos.y <= rect_top
    bool rect_bleft = pos.x >= rect_left && pos.y >= rect_bottom && pos.y <= rect_top

    if (btop || bottom || rect_btop || rect_bbottom)
        vy -= vy

    if (bleft || bright || rect_bleft || rect_bright)
        vx -= vx
}

There are however far better and scalable solution (to code a breakout bricks).

matovitch
  • 1,264
  • 11
  • 26
1

this post help me a lot

package com.mkyong.javafx.animatedball;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BouncingBall extends Application{

    @Override
    public void start(Stage stage) {

        Pane canvas = new Pane();
        Scene scene = new Scene(canvas, 300, 300, Color.ALICEBLUE);
        Circle ball = new Circle(10, Color.CADETBLUE);
        ball.relocate(5, 5);

        canvas.getChildren().add(ball);

        stage.setTitle("Animated Ball");
        stage.setScene(scene);
        stage.show();

        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(20),
                new EventHandler < ActionEvent>() {

            double dx = 7; //Step on x or velocity
            double dy = 3; //Step on y

            @Override
            public void handle(ActionEvent t) {
                //move the ball
                ball.setLayoutX(ball.getLayoutX() + dx);
                ball.setLayoutY(ball.getLayoutY() + dy);

                Bounds bounds = canvas.getBoundsInLocal();

                //If the ball reaches the left or right border make the step negative
                if(ball.getLayoutX() &lt;= (bounds.getMinX() + ball.getRadius()) ||
                        ball.getLayoutX() >= (bounds.getMaxX() - ball.getRadius()) ){

                    dx = -dx;

                }

                //If the ball reaches the bottom or top border make the step negative
                if((ball.getLayoutY() >= (bounds.getMaxY() - ball.getRadius())) ||
                        (ball.getLayoutY() &lt;= (bounds.getMinY() + ball.getRadius()))){

                    dy = -dy;

                }
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();
    }

    public static void main(String[] args) {
        launch();
    }
}
  • Please correct code typo, replace < to < This is a right way to use Timeline class from javafx.animation package to solve bouncing problem. – Scirocco Jun 27 '22 at 19:39