如何在Javafx中使球从对象上反弹?
我现在有一个球从画布的墙上弹下来。我在屏幕中间添加了一个矩形。每当球与矩形碰撞时,我希望它也能从矩形上弹起,但我不知道怎么做。我有一个叫做“r”的矩形 如何使球将长方形视为一面墙,并在碰到它时改变方向?代码示例将不胜感激。谢谢 以下是我对球从墙上反弹的代码:如何在Javafx中使球从对象上反弹?,java,geometry,javafx,Java,Geometry,Javafx,我现在有一个球从画布的墙上弹下来。我在屏幕中间添加了一个矩形。每当球与矩形碰撞时,我希望它也能从矩形上弹起,但我不知道怎么做。我有一个叫做“r”的矩形 如何使球将长方形视为一面墙,并在碰到它时改变方向?代码示例将不胜感激。谢谢 以下是我对球从墙上反弹的代码: public void handle(ActionEvent t) { // Moves the ball depending on the values of X and Y
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();
}
公共无效句柄(ActionEvent t){
//根据X和Y的值移动球
circle.setLayoutX(circle.getLayoutX()+X);
circle.setLayoutY(circle.getLayoutY()+Y);
final Bounds=canvas.getBoundsInLocal();
//用于检查墙是否被击中的布尔值
布尔leftWall=circle.getLayoutX()=(bounds.getMaxY()-circle.getRadius());
//如果触到了底壁或顶壁,球将反转方向。
if(底壁| |顶壁){
Y=Y*-1;
}
//如果接触到左墙或右墙,球将反转方向。
if(左墙| |右墙){
X=X*-1;
}
}
}));
loop.setCycleCount(Timeline.unfinite);
loop.play();
}
我不知道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
}
while(1){
bool btop=位置y>=顶部
bool bbottom=pos.y帮了我大忙
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();
}
}