Jump to content
  • Advertisement
Sign in to follow this  

Circle Collision Response and Updating Positions

This topic is 2304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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;

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: http://www.wildbunny.co.uk/blog/2011/04/06/physics-engines-for-dummies/

Hope you find it useful!

Cheers, Paul.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!