🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Applying Impulse

Started by
0 comments, last by Bob Dylan 6 years, 8 months ago

I have a circle class which has the following attributes: center, radius, old position, acceleration, mass, and restitution.

I then apply impulse resolution as per this link: https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331.

Here is the code, implementing that, along with my velocity verlet implementation (this is necessary as it explains why I change the values of the old positions of the circles near the end of the impulseScalar method):

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)

It would help if you accepted the vector methods as correct on face value, and I think that they are named well enough to allow you to figure out what they do, however I can paste them in aswell.

My main problem is that when I run this, it registers that a collision has happened, yet the values position of the second circle do not change. How would I go about fixing this, as it seems that I am implementing the calculations correctly.

The values of the first and second circle is:

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

What it returns is here: (it returns the position of the centers)

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

So when it prints INTERSECTION, surely, the stationary circle must change position, if the impulseScalar method is correct, (as it seems to be (as it follows what is said on that link).

Even if I let it run for longer, the stationary circle still does not move.

This topic is closed to new replies.

Advertisement