[英]How to make ball bounce off an object in Javafx?
我目前有一個球在畫布的牆壁上彈跳。 我在屏幕中間添加了一個矩形。 每當球與矩形碰撞時,我也希望它反彈,但我不知道該怎么做。 我有一個名為“r”的矩形。
如何讓球將矩形視為牆並在擊中時改變方向? 代碼示例將不勝感激。 謝謝。
這是我的球從牆上反彈的代碼:
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();
}
我不知道 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
}
然而,有更好和可擴展的解決方案(編碼突破磚)。
這篇文章對我幫助很大
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.