# 2D circle - rect advanced collision detection

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

## 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 on other sites
how complicated you want this to be?

circle / polygon collision

polygon polygon collision

as for the response, I'd use a basic velocity reflection, with added friction and elasticity, and move from there.

##### Share on other sites
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 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 velocityif (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 impulseVa -= Ir * ima; Vb += Ir * imb;//-------------------------------------------------// friction//-------------------------------------------------Vector Vt = Vab - (vn * Nab); // velocity projected in plane of collisionVector If = Vt * (-CoF / (ima + imb)); // friction impulseVa -= If * ima;Vb += If * imb;

friction model is very basic though. Try that.

EDIT : naming conventions.

##### 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015335
×