r/programminghelp • u/Fresh-Weakness-3769 • Apr 03 '26
C# How do I use Impulse Collision with Verlet Integration
No matter what I try, the balls keep getting insane speeds that cause overflows in less than a second. I'm not sure if its my equation or if they're colliding multiple times per sub step or what.
public void SolveCollision(Ball ball2){
const float EPSILON = 1e-6f;
Vector2 dir = this.position - ball2.position;
float sqrDist = dir.x * dir.x + dir.y * dir.y;
float totalRadius = this.radius + ball2.radius;
if (sqrDist <= totalRadius * totalRadius && sqrDist > EPSILON)
{
Vector2 old = Velocity;
float dist = (float)Math.Sqrt(sqrDist);
Vector2 normal = dir / dist;
Vector2 relativeVelocity = this.Velocity - ball2.Velocity;
float separationVelocity = Vector2.Dot(relativeVelocity, normal);
if (separationVelocity >= 0) return;
float elasticity = (this.restitution + ball2.restitution) * 0.5f;
float impulse = ((1 + elasticity) * separationVelocity) / (this.mass + ball2.mass);
this.lastPosition -= normal * (impulse * ball2.mass);
ball2.lastPosition += normal * (impulse * this.mass);
float overlap = totalRadius - dist;
Vector2 correction = normal * ((overlap * 0.5f) + 0.001f);
this.position += correction;
this.lastPosition += correction;
ball2.position -= correction;
ball2.lastPosition -= correction;
}
}
1
Upvotes
1
u/OP_Sidearm Apr 09 '26 edited Apr 09 '26
Okay, I don't have a direct fix or anything, but here is how I would debug this.
Here are two things that I found a bit strange:
Maybe a double collision is possible, if two balls collide and then one collides with another ball, which then corrects them back into each other causing another collision? (depends on how you evaluate the collision pairs) In this case, it's a bit more tricky to fix. I have some ideas, but I have not implemented a physics sim in quite some time, so I don't know if they would work '
Edit: Small addition