[英]How to make ball bounce off an object in Javafx?
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".我有一个名为“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();
}
I don't know JavaFx but here is the idea :我不知道 JavaFx,但这是我的想法:
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).然而,有更好和可扩展的解决方案(编码突破砖)。
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() <= (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() <= (bounds.getMinY() + ball.getRadius()))){
dy = -dy;
}
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
public static void main(String[] args) {
launch();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.