calculate exact collision times for your circles instead of rounding to the next frame:
- if your objects are moving in more than 1D, this requires root-finding.
- 在上述确切的碰撞时间停止仿真.
- 在第一次碰撞中解决圆的物理问题.
- 重新计算以确定由于新轨迹而引起的任何碰撞.
- 重新开始仿真.
- 重复,直到到达没有碰撞的帧结束为止.
您可能会注意到,这有可能进行大量计算.如果您的球失去动能并最终彼此搁置,这可能会特别令人讨厌-如果您的物理学使这成为可能,则您需要为静止接触"添加某种阈值(不幸的是,这可能会使情况复杂化)你的物理学极大).
You may notice that this has the potential for lots of computation. It can be particularly nasty in cases where your balls lose kinetic energy and end up resting on each other -- if your physics makes this likely, you will need to add some sort of threshold for "resting contact" (which, unfortunately, can complicate your physics enormously).
更新,以回应评论:我想明确地说,我的回答忽略了您的一种假设-如果您假装帧边界之间没有任何时间,就无法准确地处理碰撞.碰撞不会发生在框架边界处.通常,冲突将发生在帧边界之间 ,因此您的计算需要反映出来.
Update, in response to comments: I want to make clear that my answer ignores one of your assumptions -- you can't handle collisions accurately if you pretend that there isn't any time between frame boundaries. The collisions don't happen at the frame boundaries; in general, the collisions will happen between the frame boundaries, so your computations need to reflect that.
如果您假设帧之间的所有运动都是线性的(即,模拟对帧边界进行了所有加速),则实际上确定是否,何时何地发生碰撞非常简单.它将帧间模拟"几乎减少为零-即使模拟为2D或3D,也可以以封闭形式求解方程式:
If you assume that all motion between frames is linear (i.e., your simulation does all acceleration on the frame boundaries), then determining whether, where, and when there is a collision is actually pretty simple. It reduces your interframe "simulation" to practically nothing -- you can solve your equations in closed form, even if your simulation is 2D or 3D:
posAB = posA - posB [relative vector between circles A and B]
velAB = velA - velB [relative velocity between circles A and B]
posAB(t) = posAB(0) + t * velAB [relative vector as a function of time]
rAB = rA + rB [sum of radii of the two circles]
collision happens when distance(t) = abs(posAB(t)) == rAB
-> rAB^2 = | posAB(t) |^2 = | posAB(0) + t * velAB |^2
-> t^2 * |velAB|^2 + t * 2*posAB(0).velAB + |posAB(0)|^2 - rAB^2 == 0
solve quadratic equation for t:
- if discriminant is negative, there is no collision.
- if collision times are outside current timestep, there is no current collision.
- otherwise, smallest t should be the correct collision time.
- watch out for cases like 2 circles coming *out* of collision...
最后,听起来您正在尝试进行过早的优化.更好地使事情正常运行,然后使其快速运行;您必须先运行代码,才能知道实际的瓶颈.实际上,我还认为您低估了现代计算机的功能.当然,您总是可以添加足够多的对象来关闭计算机,但是我想您会惊讶地发现它花了多少钱...
Finally, it sounds like you're trying to do premature optimization. Better to make things work right, then make them fast; you won't know your actual bottlenecks until you have running code. I also think you are, in fact, underestimating the power of modern computers. Of course, you can always add enough objects to bog down your computer, but I think you will be surprised how many it takes...
这篇关于实时计算碰撞-处理时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!