Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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();

P=B->Position+B->GetRadius()*Dir;

Q=A->Position-A->GetRadius()*Dir;

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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this