Jump to content

  • Log In with Google      Sign In   
  • Create Account

Circle Collision Response and Updating Positions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 ZombieLyle   Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 April 2012 - 06:06 AM

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;
}
}


Sponsor:

#2 jefferytitan   Crossbones+   -  Reputation: 2155

Like
0Likes
Like

Posted 25 April 2012 - 04:05 PM

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?

#3 wildbunny   Members   -  Reputation: 550

Like
0Likes
Like

Posted 27 April 2012 - 04:43 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS