0

我有一个高度为 600 像素的视图,带有 12 个框,每个框为 50 像素 x 50 像素。我创建了一个球,用户点击它会根据重力落下。我这样做是为了进行物理演示,并且试图使其尽可能准确。目前,我使用的比例尺使用四个框(或 200 像素)作为一米。计算“y”位置的方法每秒被调用 50 次 (50 fps)。当我尝试运行包含此代码的程序时,它会关闭相当长的时间。

这是我调用来更新位置的方法:

public double calcY()
{
    velocity += acceleration*200/2500;
    return(getY()-velocity);
}

我乘以 200 以将米转换为像素。并除以 2500,因为它每秒被调用 50 次,所以 (50)^2 是 2500。

现在它并没有减少很多。谢谢您的帮助。

4

2 回答 2

2

Your calculations should never be tied to a fixed frame rate, measure the real time that has expired. FPS can and will vary greatly by factors outside your control.

You can keep track of time expired between calls to calcY() using System.currentTimeMillis() - that might be off a few ms between two calls, but it will be pretty precise across multiple calls.

Also last time I checked fall acceleration wasn't one meter per second, but 9.81 m/s. So your calculated acceleration seems to be off by a factor of 9.81.

Edit: Looking at your formula again, it makes no sense to me.

于 2013-10-29T18:06:23.540 回答
0

你应该从运动学方程开始。

x = x0 + v0*t + 1/2*a*t^2

加速度乘以时间的平方具有位置单位。这里的 t 是绝对时间。如果要按帧计算位置时间。

v = v0 + a*dt
x = x0 + v0*dt + 1/2*a*dt^2

每次在这里计算最终位置和速度,并将其用作下一帧的初始位置和速度。

于 2013-10-29T18:11:45.597 回答