如何使球弹出Javafx中的对象? [英] How to make ball bounce off an object in Javafx?
问题描述
我现在有一个球从画布的墙上弹起来。我在屏幕中间添加了一个矩形。每当球与矩形碰撞时,我也想让它反弹,但我不知道该怎么做。我有一个叫做r的矩形。
我该如何让球体将矩形当作墙来处理,并在它碰到它时改变方向?代码示例将不胜感激。
<$ p
$ b
这是我的代码, c> public void handle(ActionEvent t){
//根据X和Y的值来移动球体
circle.setLayoutX(circle.getLayoutX()+ X);
circle.setLayoutY(circle.getLayoutY()+ Y);
final Bounds bounds = canvas.getBoundsInLocal();
//检查墙是否被命中的布尔值
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(bottomWall || topWall){
Y = Y * -1;
}
//如果触摸了左侧或右侧的墙,球会反转方向。
if(leftWall || rightWall){
X = X * -1;
}
}
}));
loop.setCycleCount(Timeline.INDEFINITE);
loop.play();
}
我不知道JavaFx但是这里是这样的想法:
$ p $ 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
}
是一个更好的和可扩展的解决方案(用于编写突破砖块)。
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();
}
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).
这篇关于如何使球弹出Javafx中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!