# Handling circle collisions

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

## Recommended Posts

Hey, I'm making a 2d ping pong kind of game except its in a circle instead of a rectangular court. I need some help, I have gotton the circle to stop when it hits the large circle but I don't know what to change to velocity to.

##### Share on other sites
Are you using vectors in your program?

Then the normal vector of the collision will be n = (circle.Center - ball.Pos).Normalize()

I'm in a hurry now but I guess googling for bounce, collision normal, velocity vector or something will do the rest.

##### Share on other sites
You need two parameters - angle and velocity.

In order to represent "friction" between the two circles, you can multiply the ball current velocity by 0.90 for example (just play with the exact constant or maybe add some random behavior here, and see what is more "fun").

In order to calculate the angle the ball will bounce back, you should calculate the tangent of the big circle at the point of contact. When you have the tangent, you can calculate the angle between the tangent and the direction vector in which the ball was travelling, and reverse the vector.

Hope this helps!

##### Share on other sites
So you want to make a collision response right?

When the smaller ball hits the bigger one, you must get a vector called collision normal. Since the big ball certer is at the origin, it will be the small ball position vector normalized which points out the origin( in the truth, this collision normal vector should be negated. don't do it cuz due to the next computations, it will not be necessary). Now compute the dot between the collision normal and the small ball velocity vector. I don't know but I think you don't want the ball speed to vary, do you? If no, just subtract to the small ball velocity vector 2 times the collision normal times the dot and its all done.

I hope it helps and I also hope it is correct and works! :P

cya

##### Share on other sites
So far I have this:
 xd = ballPosition.x;yd = ballPosition.y;ld = largeCircleRadius - ballRadius;xd *= xd;yd *= yd;ld *= ld;	if( xd+yd >= ld ) {	dst = sqrt(xd+yd);	xd /= dst;	yd /= dst;			vel = sqrt(ballVelocity.x*ballVelocity.x + ballVelocity.y*ballVelocity.y);			ballVelocity.x = xd*vel;	ballVelocity.y = yd*vel;}

Unfortunately, the bounce only works on the bottom left of the circle.

##### Share on other sites
I'll try to give you some simple code...

if(float dist = ballPosition.x*ballPosition.x+ballPosition.y*ballPosition.y > (largeCircleRadius-ballRadius)*(largeCircleRadius-ballRadius))//if it tests true, means the ball has a distance from origin greater than the //largeCircleRadius-ballRadius meaning it is in contact with the large circle or //out of it//so we must handle this collision{    //compute collision normal vector    VECTOR2 n = Vec2Normalize(ballPosition);    //compute the dot between n and the ball velocity vector    float dot = Vec2Dot(n, ballVelocity);    //now just subtract from the velocity....    ballVelocity -= n * dot * 2.f;    //......I think it works ;)....}

##### Share on other sites
Did I miss something, or why do you assume the big circle has its center at (0,0)?

##### Share on other sites
Quote:
 vel = sqrt(ballVelocity.x*ballVelocity.x + ballVelocity.y*ballVelocity.y);

That's not a velocity, its a speed. Similary, sqrt(xd + yd) is always positive, so your distance is always positive, so it always thinks the collision normal is pointing upright, so it only works when the collision is in the bottom left :)

##### Share on other sites
Quote:
 Original post by DadleFishYou need two parameters - angle and velocity. ...Hope this helps!

no it doesnt. please dont confuse anybody with angles: they only overcomplicate what is at the core a very simple problem.

##### Share on other sites
Are you talking with me Enselic?!? If yes, I just assume that the big circle is at (0,0) cuz it seems to be at the origin here

And there's no mean to put it off-center :P....I would put at the center :)

##### Share on other sites
If savagebeastx doesn't use some weird graphics API, then the origin is most likley at the upper left corner of the window.

##### Share on other sites
No, the origin is in the center. I'm using OpenGL. The big circle is at the center.

##### Share on other sites
Oh, ok. It looked like some window API graphics.

##### Share on other sites
this is the function i use to detect collision beetween spheres
this function uses the concept of midrange osculation plane
( i don't know how else to call it ).
Note that this function gives correct result even
with high speed for both spehres.

int C2DPhysicEngine::SphereSphereCollTest( CParticle *A,
CParticle *B )
{

Vector P,Q,Dir,MidPoint;

double eps = 0.000001f;

Dir = A->Position-B->Position;

Dir.Normalise();

MidPoint = ( Q + P ) *0.5f;

if ( ( MidPoint-P ) * Dir <= eps )
{

// length of vector

Vector W=Q-P;

// relative penetration depth

Vector RelDist=W.Length()*Dir;

// sphere collided , handle
// repositioning and collision response

return 1;
}

return 0;
}

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984335

• 23
• 11
• 10
• 13
• 14