碰撞检测和弹跳
我正在尝试一下,并试图为以后的项目做一些事情,但我有一个问题,我似乎无法找到解决方案。
现在我已经有了有效且准确的碰撞检测,但问题在于创建物理原理。现在的物理原理很简单,一个物体有一个表示其方向和速度的矢量,当发生碰撞时,矢量会反转。
问题是,当物体碰撞时,它们会互相推挤,最终导致矢量永远反转,从而导致奇怪的结果。例如神奇地向上漂浮,或者看起来完全忽略物理原理。
有人可以帮我吗?
编辑: 检测是通过分离轴定理完成的,我没有放置代码,因为真正发生的只是有一个循环来检查碰撞,当发现碰撞时,向量会反转。就像我说的,实际的检测工作正常,我认为我的问题是正如 Sibbo 所说的移动物体,这样它们就不会再发生碰撞。
I'm playing around a bit and trying to get some things to work for a later project, but I have a problem that I can't seem to find a solution to.
Right now I have working and accurate collision detection, but the problem is creating the physics. The physics are simple right now, an object has a vector for it's direction and speed, when there is a collision the vector is inverted.
The problem is that when objects collide they get shoved into each other and end up inverting the vector forever causing strange results. E.g. magically floating upwards, or appearing to ignore the physics entirely.
Could anyone help me out?
EDIT:
Detection is done by separating axis theorem and I didn't put code because all that really goes on is there is a loop to check for collisions, when one is found the vectors are inverted. Like I said, the actual detection works fine, I think my problem is as Sibbo said with moving the objects so they don't collide anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道你的实现,但是当你检测到碰撞时,你应该移动对象,以便它们不再接触。这将防止你的程序进入无限碰撞循环
I don't know your implementation, but when you detect a collision, you should move the objects so that they don't touch anymore. That will prevent your program to enter an endless-collision-loop
将对象稍微分开(碰撞阈值之外)并反转速度矢量。假设您也使用矢量作为位置,只需更改其值,以便对象不再被视为碰撞。
Move the objects slightly apart (outside the threshold of collision) as well as inverting the velocity vector. Assuming you are using a vector for position as well, just alter its values such that the objects are no longer considered to be colliding.
我最近为一个大学项目编写了台球游戏的物理原理(因此,虽然这可能不是符合行业标准的代码,但它做了它应该做的事情);为了防止无限循环地计算相同碰撞的影响,我们最终所做的是:我们将所有碰撞存储在 java.util.List 中,并且每当计算下一个碰撞时,我们都会检查两个对象是否都参与了碰撞已经是计算出的最后一次碰撞的一部分;如果是这种情况,该对象对将被忽略。
这有一个优点和一个缺点:优点是,您不会通过使用(最小)偏移移动来引入不准确性,因此对象将不再接触。缺点是,两个相同的对象之间不能一个接一个地发生两次碰撞(例如,不能有 [(ball_1,ball_2); (ball_1, ball_2); ...],你只能有一些东西像 [(ball_1, ball_2); (ball_1, table); (ball_1, ball_2); 作为碰撞列表)。
I recently wrote the physics for a billard game for a university project (so while this possibly isn't code that's up to industry standards, it does what it's supposed to); what we ended up doing to prevent endless loops of calculating the effects of an identical collision was this: we stored all collisions in a java.util.List and whenever the next collision was being calculated, we checked whether both objects taking part in the collision were already part of the last collision that had been calculated; if that was the case, that object-pair was ignored.
This has an up- and a downside: upside is, you don't introduce inaccuracies by using a (minimal) offset movement so the objects won't touch anymore. Downside is, you can't have two collisions between two identical objects one after the other (for example, you can't have [(ball_1,ball_2); (ball_1, ball_2); ...], you can only have something like [(ball_1, ball_2); (ball_1, table); (ball_1, ball_2); ... ] as your list of collisions).
每个对象都应该有一个大小,当它们从彼此的边缘弹起时,它们会从将它们之间的线平分的线弹起。您是否假设所有物体都具有相同的质量?
Each object should have a size and when they bounce off the edges of each other they bounce off a line which bi-secs the line between them. Are you assuming all the objects have the same mass?