重力模拟

发布于 2024-11-29 09:48:39 字数 690 浏览 2 评论 0原文

我想模拟自由落体和与地面的碰撞(例如弹跳球)。物体将在真空中下落——可以忽略空气阻力。与地面的碰撞会导致一些能量损失,因此物体最终会停止移动。我使用 JOGL 渲染一个点,它是我的下落物体。重力恒定 (-9.8 m/s^2)。

我找到了一种欧拉方法来计算点的新位置:

deltaTime = currentTime - previousTime;
vel += acc * deltaTime;
pos += vel * deltaTime;

但我做错了。该点弹跳几次,然后向下移动(非常慢)。

这是一个伪代码(初始 pos = (0.0f, 2.0f, 0.0f), 初始 vel(0.0f, 0.0f, 0.0f), 重力 = -9.8f):

display()
{
     calculateDeltaTime();
     velocity.y += gravity * deltaTime;
     pos.y += velocity.y * deltaTime;

     if(pos.y < -2.0f) //a collision with the ground
     {
        velocity.y = velocity.y * energyLoss * -1.0f;
     }

}

实现真实效果的最佳方法是什么?欧拉方法如何引用等加速度方程?

I want to simulate a free fall and a collision with the ground (for example a bouncing ball). The object will fall in a vacuum - an air resistance can be omitted. A collision with the ground should causes some energy loss so finally the object will stop moving. I use JOGL to render a point which is my falling object. A gravity is constant (-9.8 m/s^2).

I found an euler method to calculate a new position of the point:

deltaTime = currentTime - previousTime;
vel += acc * deltaTime;
pos += vel * deltaTime;

but I'm doing something wrong. The point bounces a few times and then it's moving down (very slow).

Here is a pseudocode (initial pos = (0.0f, 2.0f, 0.0f), initial vel(0.0f, 0.0f, 0.0f), gravity = -9.8f):

display()
{
     calculateDeltaTime();
     velocity.y += gravity * deltaTime;
     pos.y += velocity.y * deltaTime;

     if(pos.y < -2.0f) //a collision with the ground
     {
        velocity.y = velocity.y * energyLoss * -1.0f;
     }

}

What is the best way to achieve a realistic effect ? How the euler method refer to the constant acceleration equations ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-12-06 09:48:39

因为浮点不能很好地舍入,所以你永远不会得到实际上为 0 的速度。你可能会得到类似 -0.00000000000001 之类的东西。

当它足够接近时,您需要将其设为 0.0。 (定义一些增量。)

Because floating points dont round-up nicely, you'll never get at a velocity that's actually 0. You'd probably get something like -0.00000000000001 or something.

you need to to make it 0.0 when it's close enough. (define some delta.)

度的依靠╰つ 2024-12-06 09:48:39

为了扩展我上面的评论,并回答 Tobias,我将在此处添加完整的答案。

经过初步检查,我确定您的速度过快。简单来说,动能和速度的关系是E = mv^2 /2,所以对速度求导后可以得到

delta_E = m v delta_v

那么,取决于能量损失定义后,就可以建立delta_Eenergyloss之间的关系。例如,在大多数情况下energyloss = delta_E/E_initial,则上述关系可以简化为:

delta_v = energyloss*v_initial / 2

这假设时间间隔很小,允许您替换v第一个方程是 v_initial,所以你应该能够摆脱你所做的事情。需要明确的是,delta_v 是从碰撞块内的 velocity.y 中减去的,而不是从您拥有的内容中减去的。

至于加不加空气阻力的问题,答案是要看情况。对于较小的初始落差,这并不重要,但由于弹跳和较高的落点,能量损失较小,这可能会开始产生影响。对于 1 克、直径 1 英寸(2.54 厘米)的光滑球体,我绘制了有空气摩擦和无空气摩擦之间的时间差与跌落高度的关系:

有和没有空气阻力的时间差异与跌落高度

对于低能量损失材料(保留 80 - 90+ % 的能量),我会考虑将其添加到 10 米及更高的跌落高度高度。但是,如果水滴低于 2 - 3 米,我就不会打扰。

如果有人想要计算结果,我会分享。

To expand upon my comment above, and to answer Tobias, I'll add a complete answer here.

Upon initial inspection, I determined that you were bleeding off velocity to fast. Simply put, the relationship between kinetic energy and velocity is E = m v^2 /2, so after taking the derivative with respect to velocity you get

delta_E = m v delta_v

Then, depending on how energyloss is defined, you can establish the relationship between delta_E and energyloss. For instance, in most cases energyloss = delta_E/E_initial, then the above relationship can be simplified as

delta_v = energyloss*v_initial / 2

This is assuming that the time interval is small allowing you to replace v in the first equation with v_initial, so you should be able to get away with it for what your doing. To be clear, delta_v is subtracted from velocity.y inside your collision block instead of what you have.

As to the question of adding air-resistance or not, the answer is it depends. For small initial drop heights, it won't matter, but it can start to matter with smaller energy losses due to bounce and higher drop points. For a 1 gram, 1 inch (2.54 cm) diameter, smooth sphere, I plotted time difference between with and without air friction vs. drop height:

difference in time with and without air-drag vs. drop height

For low energy loss materials (80 - 90+ % energy retained), I'd consider adding it in for 10 meter, and higher, drop heights. But, if the drops are under 2 - 3 meters, I wouldn't bother.

If anyone wants the calculations, I'll share them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文