Java ball bouncing程序中的重力,可刷新每场比赛的滴答声

Java ball bouncing程序中的重力,可刷新每场比赛的滴答声,java,position,gravity,bounce,Java,Position,Gravity,Bounce,嘿,我正试图找出在我的程序中实现重力的最佳方法,那就是简单的球反弹。该程序使用一种每秒调用50次的方法(游戏计时器滴答作响的速率),在这种方法中,我称之为重力方法。在重力法中,我现在有 public void Gravity(){ this.currentPositionY = this.currentPositionY + 9; if (this.currentPositionY >= 581){ this.currentPositionY=581; }

嘿,我正试图找出在我的程序中实现重力的最佳方法,那就是简单的球反弹。该程序使用一种每秒调用50次的方法(游戏计时器滴答作响的速率),在这种方法中,我称之为重力方法。在重力法中,我现在有

public void Gravity(){
   this.currentPositionY = this.currentPositionY + 9;
   if (this.currentPositionY >= 581){
      this.currentPositionY=581;
     }
}

我的代码有问题:在重力速度不是常数的情况下,它随时间而变化,但我不确定如何在经常调用重力方法的情况下实现时间。现在我也有了它,球停在581,这样它就不会从屏幕上掉下来。当球落得更长时,我如何实现更高的反弹,当球落得更少时,如何实现更短的反弹?谢谢你的时间

在方法外有一个变量用于其y速度。每滴答声,增加其速度,考虑重力。
如果球经过边界,将其设置为边界,并将“速度”设置为-1*速度,使其在另一个方向“反弹”

也许是这样的:

private int currentVelocityY = 0;
private int gravity = 3;
public void Gravity(){
    this.currentPositionY = this.currentPositionY + this.currentVelocityY;
    if (this.currentPositionY >= 581){
        this.currentPositionY=581;
        this.currentVelocityY = -1 * this.currentVelocityY;
    }
    currentVelocityY = currentVelocityY + gravity;
}

谢谢你的回复!我忽略了这一点,但在游戏中点击s按钮会增加球的高度,所以我不能暂停游戏,因为有人可能会让程序运行一段时间,然后单击s按钮增加高度,然后球会落得太快,因为游戏已经运行了很长时间。你可以使用增加高度的方法重置速度或设置最大速度上限。