圆圈 - 圆圈碰撞不准确

时间:2017-10-30 10:08:22

标签: python collision-detection physics

EDIt:我刚刚删除了我们验证的其他方法是正确的,因为问题似乎有点长,而且这些方法似乎是无关紧要的。

我有一个圆圈类,它具有以下属性:中心,半径,旧位置,加速度,质量和恢复原状。

然后根据此链接应用脉冲分辨率:https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331

这是代码,实现了这个,以及我的velocity verlet实现(这是必要的,因为它解释了为什么我改变了impulseScalar方法末尾附近圆圈的旧位置的值):

def doVerletPosition(self):
    diffPos = (self.center).subtract(self.oldPos)
    aggregatePos = diffPos.add(self.center)
    ATT = (self.accel).scalarMult(dt**2)
    e = ATT.add(aggregatePos)
    return e

def doVerletVelocity(self):
    deltaD = ((self.center).subtract(self.oldPos))
    return deltaD.scalarMult(1/dt)


def impulseScalar(self,other):                                                                           

    isCollision = self.collisionDetection(other)
    collisionNormal = isCollision[0]

    if(isCollision[1] == True):

        relativeVelocity = (other.doVerletVelocity()).subtract(self.doVerletVelocity())
        normDirecVel = relativeVelocity.dotProduct(collisionNormal)

        restitution = -1-(min(self.restitution,other.restitution))

        numerator = restitution * normDirecVel

        impulseScalar = numerator/(self.invMass + other.invMass)

        impulse = collisionNormal.scalarMult(impulseScalar)

        selfVel = (self.doVerletVelocity()) 
        otherVel = other.doVerletVelocity()

        selfVelDiff = impulse.scalarMult(self.invMass)
        otherVelDiff = impulse.scalarMult(other.invMass)

        selfVel = selfVel.subtract(selfVelDiff)
        otherVel = otherVel.subtract(otherVelDiff)

        self.oldPos = (self.center).subtract(selfVel)
        other.oldPos = (other.center).subtract(otherVel)

如果你在面值上接受了矢量方法是正确的,那会有所帮助,我认为它们的名字很好,可以让你弄清楚它们的作用,但是我也可以粘贴它们。

我的主要问题是,当我运行它时,它会记录发生碰撞,但第二个圆圈的值位置不会改变。我将如何解决此问题,因为我似乎正在正确地实施计算。

第一个和第二个圆圈的值是:

center = Vector(0,0)
radius = 3
oldPos = Vector(0,0)
accel = Vector(0,0)
mass = 1
restitution = 0.5

center2 = Vector(0,4.2)
radius2 = 1
oldPos2 = Vector(0,4.21)
accel2 = Vector(0,-1)
mass2 = 1
restitution2 = 0.7

它返回的是:(它返回中心的位置)

0.0      0.0     0.0     4.1896
0.0      0.0     0.0     4.178800000000001
0.0      0.0     0.0     4.167600000000001
0.0      0.0     0.0     4.1560000000000015
0.0      0.0     0.0     4.144000000000002
0.0      0.0     0.0     4.131600000000002
0.0      0.0     0.0     4.118800000000003
0.0      0.0     0.0     4.1056000000000035
0.0      0.0     0.0     4.092000000000004
0.0      0.0     0.0     4.078000000000005
0.0      0.0     0.0     4.063600000000005
0.0      0.0     0.0     4.048800000000006
0.0      0.0     0.0     4.033600000000007
0.0      0.0     0.0     4.018000000000008
0.0      0.0     0.0     4.002000000000009
0.0      0.0     0.0     3.9856000000000096
INTERSECTION
0.0      0.0     0.0     3.9688000000000105
INTERSECTION
0.0      0.0     0.0     3.9516000000000115
INTERSECTION
0.0      0.0     0.0     3.9340000000000126

因此,当它打印INTERSECTION时,如果impulseScalar方法是正确的,那么静止圆必须改变位置(因为它似乎是(因为它遵循在该链接上所说的)。

即使我让它运行更长时间,静止的圆圈仍然不会移动。

1 个答案:

答案 0 :(得分:0)

小心操作负数。也许你正在以一种你不想要的方式改变价值观。到处使用sqrt。

相关问题