Sign in to follow this  

Circle Collision Response and Updating Positions

Recommended Posts

Hey I'm making a simple 2D physics engine and I'm having some bother with the collision response not working properly I get random positions like 300,000 when ever I collide the circles in certain areas. Also, I'm having trouble trying to update all the positions(x, y) and velocitiy(x, y). They're being updated but they're not being drawn in new positions.

//Circles declared at the start of my renderscene class
circle dyamCircle(dyamCircleX, dyamCircleY, dyamCircleRadius, dyamCircleMass, dyamCircleVelocityX, dyamCircleVelocityY, dyamCircleFriction, 1, 0, 0, circleBody);
circle dyamCircle2(dyamCircle2X, dyamCircle2Y, dyamCircle2Radius, dyamCircle2Mass, dyamCircle2VelocityX, dyamCircle2VelocityY, dyamCircle2Friction, 0, 1, 0, circleBody);

//In my collisions class
bool circleCircleIntersection(circle c, circle c1)
//Collision detection code goes here
float circleDot = c.getCircleCentreX() * c1.getCircleCentreX() + c.getCircleCentreY() * c1.getCircleCentreY(); //Dot product
float circleDistance = (float) sqrt(circleDot); //Square root function - REMOVE in optimisation
float normal = dist / circleDistance; //Normal for the collision
float inverse1 = (c.getCircleMass() > 0.0f)? 1.0f / c.getCircleMass() : 0.0f; //Inversed masses
float inverse2 = (c1.getCircleMass() > 0.0f)? 1.0f / c1.getCircleMass() : 0.0f;
float separation_vector = normal * (depth / (inverse1 + inverse2));

c.setCircleCentreX(c.getCircleCentreX() - separation_vector * inverse1);
c.setCircleCentreY(c.getCircleCentreY() - separation_vector * inverse1);
c1.setCircleCentreX(c.getCircleCentreX() + separation_vector * inverse2);
c1.setCircleCentreY(c.getCircleCentreY() + separation_vector * inverse2); //Seperating the circles

float vCollX = c.getCircleVelocityX() - c1.getCircleVelocityX(); //Combine velocity
float vCollY = c.getCircleVelocityY() - c1.getCircleVelocityY();
float imSpeed = normal * vCollX + normal * vCollY; //Impact speed of the collision

float colImpulse = -(1.0f + c.getCircleFriction()) * (imSpeed) / (inverse1 + inverse2); //Collision Impulse of the collision
float impulse = colImpulse * normal; c.setCircleVelocityX(c.getCircleVelocityX() - impulse * inverse1);

c.setCircleVelocityY(c.getCircleVelocityY() + impulse * inverse1); //Changing the momentum of the the circles
c1.setCircleVelocityX(c1.getCircleVelocityX() - impulse * inverse2);
c1.setCircleVelocityY(c1.getCircleVelocityY() + impulse * inverse2);
return true;
} [/code]

Share this post

Link to post
Share on other sites
Keeping in mind that these forums aren't a free debugging service...

- What does "normal" mean here? I think of normals as a vector, not a float.
- What is "depth"?
- Why are you setting the X velocity for c1, but not for c?

Share this post

Link to post
Share on other sites
I wrote this article a while back which covers circle vs circle collision response:

Hope you find it useful!

Cheers, Paul.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this