向一点移动时的圆碰撞反应
我有一些圆圈(可以有两个以上)正在向一个点移动。到目前为止,当它们接近时它们就会重叠。我怎样才能使它们不相交,但仍朝该点移动?
我正在使用Java。
- 圆圈不是遵循物理定律的球,它们 代表有机体(无关)。
- 我已经让碰撞检测工作了。
- 圆圈不会粘在一起,它们根本无法相交。
I have circles (there can be more than two) that are moving towards a point. As of now, they overlap when they are close. How can I make it so they do not intersect, but still move towards the point?
I am using Java.
- The circles are not balls that follow the laws of physics, they
represent organisms (irrelevant). - I already have collision detection working
- The circles do not stick together, they simply cannot intersect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是一个简单的问题。
我假设您有一些系统可以计算圆的坐标,渲染它们并重复。这些重复称为帧。
每个圆都有一个向量来描述它应该移动的方向。在每一帧上,您需要执行碰撞检测,并在必要时更改该矢量。
任意两个圆之间的碰撞可以通过检查它们之间的距离是否小于它们的半径之和来检测。碰撞圆中心之间的线给出了碰撞的方向。圆的碰撞矢量在碰撞方向上,指向圆心,其大小等于圆重叠单位数的一半,即半径之和减去圆的半径之和。它们之间的距离除以二。 (如果圆的大小可能不相等,则变得更加复杂,您需要计算出要划分多少。)
因此,解决问题的一种可能的解决方案是简单地将碰撞向量添加到圆的方向向量上,然后然后缩放方向向量,使其大小等于碰撞前的大小。 (这样只有它的方向会被调整,而不是它的大小。)
请注意,这仅适用于您给出的具体描述,您希望球粘在一起并继续向该点移动;如果你想要更真实的效果,你需要模拟台球类型的物理。
That's not an easy question.
I presume you have some system which computes the coordinates of the circles, renders them, and repeats. These repetitions are called frames.
Each circle has a vector which describes the direction in which it is supposed to move. On each frame you need to perform collision detection, and change that vector if necessary.
Collision between any two circles can be detected by checking to see whether the distance between them is smaller than the sum of their radii. The line between the centers of the colliding circles gives you the direction of the collision. The collision vector for a circle is on the direction of the collision, pointing towards the center of the circle, and its magnitude is equal to half the number of units by which the circles overlap, that is, to the sum of their radii minus the distance between them, divided by two. (If the circles can be of unequal sizes it becomes more complicated, you need to figure out by how much to divide.)
So, one possible solution to your problem would be to simply add the collision vector to the direction vector of the circle and then scale the direction vector so that its magnitude is equal to the magnitude that it had before the collision. (So that only its direction will be adjusted, not its magnitude.)
Note that this will only work for the specific description that you gave, where you want the balls to stick together and continue moving towards the point; if you wanted a more realistic effect, you would need to emulate billiard balls type of physics.
与其重新发明轮子,不如考虑使用现有的 2D 物理引擎。它将为您完成所有碰撞检测,速度很快,并且可能会免费为您提供许多很酷的功能。
JBox2D 是流行的开源 2D 物理引擎 Box2D。一个很好的起点。
Rather than reinvent the wheel, consider using an existing 2D physics engine. It will do all the collision detection for you, it will be fast and will probably provide you with a lot of cool functionality for free.
JBox2D is a Java port of the popular open source 2D physics engine Box2D. A good place to start.
我用 JQuery 做到了。
这是背后的数学——没有三角函数。
用一个球向您想要的方向迈出一小步。
检查所有其他球[i]是否发生碰撞。
现在 - 在穿过两个中心点的直线上 - 推动其他球(如果它们碰撞),以便它们彼此接触。
滑动位置已改变的每个球[i]。注意 x,y 是中点的坐标,而不是左上角的坐标。
$("#ball").offset({"left":Math.round(x[i]-r),"top":Math.round(y[i]-r)});
如果您想查看它的工作原理,请参阅此处
I did it with JQuery.
Here is the math behind - without trigonometric functions.
Make a small step with one Ball to the direction you want it to go.
Check all other balls[i] whether they collide.
Now - on a straight line through the two center point - push the other balls (if they collide) so that they just touch each other.
Slide each ball[i] whose position has changed. Aware that x,y are the coordinates of the middle point and not of the top left corner.
$("#ball").offset({"left":Math.round(x[i]-r),"top":Math.round(y[i]-r)});
If you want to see it working see here
OP 的解决方案。
感谢:https://web.archive.org/web/20120714122645/http://awesty.com/blog/2008/09/circle-collisions/
Solution by OP.
Thanks to: https://web.archive.org/web/20120714122645/http://awesty.com/blog/2008/09/circle-collisions/