【问题标题】:Calculating direction after a 2d circle collision计算二维圆碰撞后的方向
【发布时间】:2017-10-17 01:57:29
【问题描述】:

我知道如何计算与 2 个圆碰撞后的速度矢量的标量 (根据此链接:https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331

这些圆不能旋转,也没有摩擦,但可以有不同的质量,但是我似乎找不到任何方法来找到单位矢量,我需要将速度标量乘以得到粒子的新速度碰撞后。

我也知道如何检查 2 个圆是否在碰撞。

另外,我只是在纯粹的“数学意义上”处理这个问题(即圆圈有一个中心和一个半径),并且想知道如何在 python 3.0 的屏幕上表示这些圆圈。

向量类:

class Vector():
def __init__(self,x,y):
    self.x = x
    self.y = y

def add(self, newVector):
    return Vector(self.x+newVector.x, self.y+newVector.y)

def subtract(self,newVector):
    return Vector(self.x-newVector.x, self.y-newVector.y)

def equals(self, newVector):
    return Vector(newVector.x,newVector.y)

def scalarMult(self, scalar):
    return Vector(self.x*scalar, self.y*scalar)

def dotProduct(self, newVector):
    return (self.x*newVector.x)+(self.y*newVector.y

def distance(self):
    return math.sqrt((self.x)**2 +(self.y)**2)

圆类:

class Particles():
def __init__(self,currentPos, oldPos, accel, dt,mass, center, radius):
    self.currentPos = currentPos
    self.oldPos = oldPos
    self.accel = accel
    self.dt = dt
    self.mass = mass
    self.center = center
    self.radius = radius

def doVerletPosition(currentPos, oldPos, accel, dt):
    a = currentPos.subtract(oldPos)
    b = currentPos.add(a)
    c = accel.scalarMult(dt)
    d = c.scalarMult(dt)
    return d.add(b)

def doVerletVelocity(currentPos, oldPos, dt):
    deltaD = (currentPos.subtract(oldPos))
    return deltaD.scalarMult(1/dt)

def collisionDetection(self, center, radius):
    xCenter = (self.radius).xComponent()
    yCenter = (self.radius).yComponent()
    xOther = radius.xComponent()
    yOther = radius.yComponent()
    if ((xCenter - xOther)**2 + (yCenter-yOther)**2 < (self.radius + radius)**2):
        return True
    else:
        return False

我确实知道 AABB,但我现在只使用大约 10 个粒子,而且现在不需要 AABB。

【问题讨论】:

  • 听起来你想要做的是找到单位向量指向的方向。这是一个“弹性碰撞”,所以答案是同时应用动量守恒和能量守恒。
  • 但是我该怎么做呢?
  • 经过反思,我想你需要第三个约束,即冲量必须沿着连接两个圆盘中心的线,即碰撞的法线向量。如需更多帮助,请参阅physics.stackexchange.com/questions/79047/…

标签: python physics collision


【解决方案1】:

你知道两个圆盘之间传递的力必须沿着这个碰撞的“法向量”,这很容易得到 - 它只是沿着连接两个圆盘中心的线的向量。

您有四个约束:动量守恒(它适用于 x 和 y,因此算作两个约束)、能量守恒,以及这种“沿法线的力”约束。你有四个未知数,即最终速度的 x 和 y 分量。 四个方程和四个未知数,你可以解出你的答案。由于我的物理学背景,我用动量而不是速度来写这个,但希望这不会太难解析。 (请注意,例如,动能等于p**2/2m1/2 mv**2。)

## conservation of momentum
p_1_x_i + p_2_x_i = p_1_x_f + p_2_x_f ## p_1_x_i := momentum of disc _1_ in _x_ axis intially _i 
p_1_y_i + p_2_x_i = p_1_y_f + p_2_y_f

## conservation of energy
(p_1_x_i**2 + p_1_y_i**2)/(2*m_1) + (p_2_x_i**2 + p_2_y_i**2)/(2*m_2) = (p_1_x_f**2 + p_1_y_f**2)/(2*m_1) + (p_2_x_f**2 + p_2_y_f**2)/(2*m_2)

## impulse/force goes along the normal vector
tan(th) := (x_2-x_1)/(y_2-y_1) # tangent of the angle of the collision
j_1_x := p_1_x_i - p_1_x_f # change in momentum aka impulse
j_1_y := p_1_y_i - p_1_y_f
tan(th) = -j_1_x/j_1_y 

(我希望符号清晰。如果我可以使用乳胶会更清晰,但stackoverflow不支持它。)

希望这会有所帮助!

【讨论】:

  • 希望我在最后一个等式中得到了正确的符号。您应该能够很容易地仔细检查。
猜你喜欢
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多