圆之间的二维碰撞响应 [英] 2d collision response between circles

查看:85
本文介绍了圆之间的二维碰撞响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算2个碰撞球的新速度,但是在解决另一个问题之前我无法做到这一点.

I'm trying to calculate new velocities for 2 colliding balls, but can't really do that before I solve another problem.

由于在数字世界中几乎不会发生真正的碰撞,因此我们总是会遇到碰撞"的球重叠的情况.

Since in digital world a real collision almost never happens, we will always have a situation where the "colliding" balls overlap.

想象一下,有100个球随机运动,所以,如果我理解正确,程序应如下所示:

Imagine there is a 100 balls moving randomly so, if I understand it correctly, the procedure should be as follows:

  • 移动球(x += vx; y+= vy;)
  • 获得最好的重叠(或完全碰撞)的球
  • 将球移回"到那个时刻
  • 执行碰撞计算
  • Move the balls (x += vx; y+= vy;)
  • Get the lest overlapping (or perfectly colliding) balls
  • Move the balls "back in time" to that moment
  • Perform collision calculations

如果以上情况正确,那么如何将球倒退"到第一次碰撞点?已知数据:

If the above is correct, then, how could I move the balls "back in time" to the point of first collision? Known data:

  • 所有球的坐标(b[i].xb[i].y)
  • XY球的速度(b[i].vxb[i].vy)
  • 避免重叠的球之间的距离(dist)
  • All coordinates of the balls (b[i].x, b[i].y)
  • Ball X and Y velocities (b[i].vx, b[i].vy)
  • Distance between lest overlapping balls (dist)

我应该算出dist到碰撞的最佳距离的百分比,然后简单地将xy坐标移回相同的百分比vxvy?

Should I just calculate how many percent the dist is of the perfect distance to the collision and then simply move back x and y coordinates by the same amount of percent of vx and vy?

推荐答案

对于类似这样的碰撞,通常最容易从其中一个球的参考系中查看它.

For collisions like these, it's usually easiest to look at it from the reference frame of one of the balls.

假设您有ball1ball2.这些球的位置分别为p1p2,速度为v1v2.假设ball1相对于ball2的相对速度为v1-v2=v.

Let's say you have ball1 and ball2. These balls have positions p1 and p2 respectively, and velocities v1 and v2. Let the relative velocity of ball1 with respect to ball2 be v1-v2=v.

我们想知道||p1-p2||小于||r1||+||r2||的情况,其中r1是向量,其中向量中的第一个球的半径沿第二个球的方向,而r2则为-反之亦然.

We want to know when ||p1-p2|| is less than ||r1||+||r2||, where r1 is the vector with a length of the radius of the first ball in the direction towards the second ball, and r2 is vice-versa.

ball2的角度来看,ball1以速度v1+v2移动.在时间tball2在位置p2+(v1+v2)*t.

From ball2's perspective, ball1 is moving with velocity v1+v2. At time t, ball2 is at position p2+(v1+v2)*t.

在以下情况下,球发生碰撞:

The balls collide when:

(p1-(p2+vt)) = (r1+r2)
-(p2+vt) = (r1+r2)-p1
-p2-vt = (r1+r2)-p1
-vt = (r1+r2)-p1+p2
vt = (p1-p2)-(r1+r2)

||a|| = ||b||+||c||a = b+c以来,我们知道

||v||t = ||p1-p2|| - ||r1+r2||
t = (||p1-p2|| - ||r1+r2||)/||v||

例如:p1 = (7,5)p2=(4,1)||r1||=1||r1||=2,以及v1=(1,2)v2=(-2,-2)然后是v=(3,4).碰撞发生在:

For example: p1 = (7,5) and p2=(4,1), ||r1||=1 and ||r1||=2, and v1=(1,2) and v2=(-2,-2) then v=(3,4). The collision happens at:

t = (||(3,4)|| - 3)/||(3,4)||
t = (5-3)/(5) = 2/5 = 0.4

现在您有了碰撞的时间,弄清楚球在哪里很容易:-)

Now that you have the time of the collision, figuring out where the balls are is easy :-)

编辑以将vectormath转换为伪代码:

edit to put the vectormath into pseudocode:

p = p1-p2
v = v1-v2
t = (sqrt(p.x*p.x + p.y*p.y) - (r1+r2)) / sqrt(v.x*v.x + v.y*v.y)

这篇关于圆之间的二维碰撞响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆