Jump to content
  • Advertisement
Sign in to follow this  
Dirrewolf

2D circle - rect advanced collision detection

This topic is 4228 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

Ok I am currently working on an advanced version of Pong where the user controls the paddle with the mouse and it can move all over the playable area. I need to know exactly how to do collision detection between them AND response ie. calculating resultant velocities. I have done many tests with it and it just isnt coming out perfect so I am coming here for some help. Thx

Share this post


Link to post
Share on other sites
Advertisement
Im not looking for smthing that complex. How exactly do you do the vector reflection (with added velocity of the paddle hitting the ball)?
thx

Share this post


Link to post
Share on other sites
Conservation of momentum. A paddle would be considered to have infinite mass (so inverse mass = 0.0f). SO all energy is transfered back to the ball.

objects A[Pa, Va, ima] B[Pb, Vb, imb]. im is inverse mass of the object (1 / mass)
coeff of restitution [CoR]. in range (0...1) (say, 0.8)
coeff of friciton [CoF]. in range (0...1), usually very low, say, 0.05.
Normal of collision [Nab]. going from object A towards object B, normalised. For example, two balls colliding, Nab = (Pb - Pa).Normalised();


Vector Vab = (Vb - Va);
float vn = Vab.DotProduct(Nab); // velocity along normal, or impact velocity
if (vn > 0.0f) return false; // objects moving away from each other

//-------------------------------------------------
// Impulse
//-------------------------------------------------
float i = (-(1.0f + CoR) * vn / (ima + imb)); // collision impulse.
Vector Ir = Nab * i; // vector collision impulse
Va -= Ir * ima;
Vb += Ir * imb;

//-------------------------------------------------
// friction
//-------------------------------------------------
Vector Vt = Vab - (vn * Nab); // velocity projected in plane of collision
Vector If = Vt * (-CoF / (ima + imb)); // friction impulse
Va -= If * ima;
Vb += If * imb;




friction model is very basic though. Try that.

EDIT : naming conventions.

Share this post


Link to post
Share on other sites
well what I have done is split the collision possibilities into 8. first 2 are left and right, which are easy to tell and all i do is reflect the X of the velocity. second two are top and bottom where i reflect the Y velocity. other 4 are the corners where i calculate a vector from the corner to the center of the ball (dist) and do another collision check there using radius of ball (first collision check I have done is rect -> rect check). then im not sure if this is right but I calculate the normal of dist and save it as distNorm. Ok now is where i think i might need some help. to reflect the velocity, what i do is add 2 * Abs(Dot(ball.velocity, distNorm)) * distNorm. after this i just offset the ball so that it no longer collides with the paddle. I came up with this method from the top of my head and i couldnt find any tutorials which i understand which do this properly. please comment on my method and suggest a better way, im sure there is one.
heres the code btw for one of the corners (top, left):


Vector2 dist = new Vector2(ball.CenterX - rect.Left, ball.CenterY - rect.Top);
if (dist.Length() < ball.Radius)
{
Vector2 distNorm = Vector2.Normalize(dist);
ball.Velocity += 2f * Math.Abs(Vector2.Dot(ball.Velocity, distNorm)) * distNorm;
ball.Offset(distNorm * (ball.Radius - dist.Length()));
}



PS. for now I dont need to take mass and elasticity etc. into consideration, I just need to reflect the ball and regardless of mass, somehow add a velocity to the ball to make it go faster if the paddle hits it.

thanks

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!