【发布时间】:2023-03-13 11:13:01
【问题描述】:
编辑:我刚刚删除了我们验证正确的其他方法,因为问题似乎有点长,而且这些方法似乎是无关的。
我有一个圆形类,它具有以下属性:中心、半径、旧位置、加速度、质量和恢复原状。
然后我根据此链接应用脉冲分辨率: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 时,如果脉冲Scalar 方法正确,则固定圆肯定会改变位置(看起来是这样(因为它遵循该链接上所说的内容)。
即使我让它运行更长时间,静止的圆圈仍然不动。
【问题讨论】:
-
AABBintersection在您期望它返回 False 时返回 True 的问题?如果是这样,您应该显示它的代码。如果不是,你能澄清确切的问题吗? -
我可能也应该将它包含在主要部分中,但是碰撞检测工作正常,例如,如果原点有一个半径为 1 的静止圆,另一个半径为 3从某个高度落下,它表示当落下圆的中心大致在 (0,4) 时有一个交点,但第二个圆没有从碰撞中获得任何速度,而是停留在 (0,0)。跨度>
-
看来问题出在
impulseScalar?当你看到问题时,自我和他人的价值观是什么?在这种情况下,预期的结果是什么?实际结果如何? -
你有没有发现我的错误?
-
别担心,我已经解决了。关键是在使 oldPos = currentPos 和 currentPos = newPos 之后,在调用 doVerletVelocity 方法之前放置脉冲标量方法。
标签: python collision-detection physics