Jump to content

  • Log In with Google      Sign In   
  • Create Account


ZombieLyle

Member Since 24 Apr 2012
Offline Last Active Apr 27 2012 05:03 AM

Topics I've Started

Circle Collision Response and Updating Positions

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

PARTNERS