Handling circle collisions

Started by
12 comments, last by ketek 18 years, 2 months ago
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.
Advertisement
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.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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!
Dubito, Cogito ergo sum.
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
.
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.
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 ;)....}
.
Did I miss something, or why do you assume the big circle has its center at (0,0)?
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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 :)

Quote:Original post by DadleFish
You 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.
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 :)
.

This topic is closed to new replies.

Advertisement