gpt4 book ai didi

java - 在 JavaFX 动画中模拟真实的弹跳

转载 作者:行者123 更新时间:2023-12-02 03:03:48 24 4
gpt4 key购买 nike

我正在使用 JavaFX 创建一个弹跳球动画,类似于弹跳 Windows Logo 屏幕保护程序。我现在的代码很不错,但它只会以顺时针方式弹跳球。一般来说,这很好,但最终球会自行逆时针旋转,在这种情况下,它看起来不再现实。我一直在寻找一种方法来计算球应该如何弹跳;在我看来,这实际上取决于球进入的角度。我正在使用一个AnimationTimer,它将每帧翻译球一定的量。当球的Bounds遇到边界时,平移方向会改变,正是在这次 session 上我需要一个建议......BallAnimation 是一个内部类。

    class BallAnimation extends AnimationTimer{
private final Sphere ball;
private double movex = 0;
private double movey = 0;
private double xvariation = 0;
private double yvariation = 0;
private boolean right = true;
private boolean up = false;
private boolean changeColorRandomly = true;

private double rate = 1;



public BallAnimation(Sphere ball){
this.ball = ball;
ball.setLayoutX(200);
ball.setLayoutY(50);
}

public void handle(long now){
move(right,up);

Bounds ballBounds = ball.localToScene(ball.getBoundsInLocal());

if(ballBounds.intersects(rightWall.getBoundsInParent())){
calculateMotion(rightWall);
randomBounceAngle();
setRandomColor();
}
if(ballBounds.intersects(leftWall.getBoundsInParent())){
calculateMotion(leftWall);
randomBounceAngle();
setRandomColor();
}
if(ballBounds.intersects(ceiling.getBoundsInParent())){
calculateMotion(ceiling);
randomBounceAngle();
setRandomColor();
}
if(ballBounds.intersects(floor.getBoundsInParent())){
calculateMotion(floor);
randomBounceAngle();
setRandomColor();
}


}

private void calculateMotion(Line touchedWall){
if(touchedWall.equals(rightWall)){
right = false;
up = false;

}
if(touchedWall.equals(leftWall)){
right = true;
up = true;
}
if(touchedWall.equals(ceiling)){
right = true;
up = false;
}
if(touchedWall.equals(floor)){
right = false;
up = true;
}



}

public void move(boolean right, boolean up){
if(right && !up){
ball.setTranslateX((movex += (getRate() + xvariation)));
ball.setTranslateY((movey += (getRate() + yvariation)));
}
if(right && up){
ball.setTranslateX((movex += (getRate() + xvariation)));
ball.setTranslateY((movey -= (getRate() + yvariation)));
}
if(!right && up){
ball.setTranslateX((movex -= (getRate() + xvariation)));
ball.setTranslateY((movey -= (getRate() + yvariation)));
}
if(!right && !up){
ball.setTranslateX((movex -= (getRate() + xvariation)));
ball.setTranslateY((movey += (getRate() + yvariation)));
}
System.out.println("("+movex+", "+movey+")");

}

public double getRate(){
return rate;
}

public void setRate(double rate){
this.rate = rate;
}

public void randomBounceAngle(){
double ran = Math.random();
if(ran >= .50){
//shallow bounce angle
xvariation = 3;
yvariation = 2;

}else{
//sharp bounce angle
xvariation = 2;
yvariation = 3;
}
}

![bouncing ball[1]

...问题是,当球击中右边界时,它会向下弹起并远离,底部它会向上和向左弹起,左边界:向上和向右,天花板:向右和向下。大多数时候这都很好,但有时需要以其他方式反弹。

最佳答案

嗯,在完美的物理世界中,内角等于外角。如果您使用 x/y 轴,对于 x 轴的反射,请抵消球速度的 y 分量。对于 y 轴的反射,请取消球速度的 x 分量。

我使用图层并检测用于 Racket 控制的键盘敲击,用 javascript 重新编写了 pong(这是在 Netscape 4.7x 的 '00 或 '01 中)。我作弊了,设置了让球向 8 个方向移动的函数。如果球沿着轴(左/右或上/下)行进,则快速随机数会提供不同的弹跳。否则,以相同的角度弹出。

关于java - 在 JavaFX 动画中模拟真实的弹跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42058282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com