Jump to content
  • Advertisement
Sign in to follow this  

2d ball collision - balls sometimes stick together

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

I have to write an animation program in MFC for class so I decided to do 2 balls bouncing around a screen. I got all my collision detection/response set up, but if the balls go to fast, they sometimes stick when they collide. I thought the problem was that the balls were going too fast at each time step and by the time my collision function had a chance to run, they were already stuck together. This of course causes them to infinitely collide. I tried moving each ball away from eachother by a small amount right when they collide but that didn't help. Anyone have any ideas as to why this is happening? Here is my collision function:
float distance;
float sumRadii;

distance = (float)sqrt( (b2CenterX - b1CenterX) * (b2CenterX - b1CenterX) +
		       (b2CenterY - b1CenterY) * (b2CenterY - b1CenterY) );
sumRadii = (b1Radius + b2Radius)+10; //added 10 because MFC draws a rectangle around circle. Needed to offset radius

if(distance < sumRadii)
	ballColor = RGB(rand() % 255,rand() % 255, rand() % 255);
	float pt1x, pt1y;
	float pt2x, pt2y;
	float velx;
         float vely;
         float velx2;
         float vely2;
	float tx, ty;
	float vix, viy, vin, vit, vfnmag, vftmag;
         float vix2, viy2, vin2, vit2, vfnmag2, vftmag2;
	float m = 0.0, m2 = 0.0, rc = 0.0, rc2 = 0.0;

	Vector2D N;

	pt1x = (float)b1CenterX+10;
	pt1y = (float)b1CenterY+10;
	pt2x = (float)b2CenterX+10;
	pt2y = (float)b2CenterY+10;
	N.Set( (pt1x - pt2x), (pt1y - pt2y) );

	tx = -N.GetY();
	ty = N.GetX();
	vix  = b1Velocity.GetX(); //Initial velocities
	viy  = b1Velocity.GetY();
	vix2 = b2Velocity.GetX();
	viy2 = b2Velocity.GetY();
	vin  = vix*N.GetX() + viy*N.GetY();  //converted to n-t coords
	vin2 = vix2*N.GetX() + viy2*N.GetY();
	vit  = vix*tx + viy*ty;
	vit2 = vix2*tx + viy2*ty;

	vftmag  = vit;
	vftmag2 = vit2;

	m  = b1Mass;   //Mass of ball 1
	m2 = b2Mass;   //Mass of ball 2

	rc  = b1RestitutionCoef;  //Restitution Coefficient
	rc2 = b2RestitutionCoef;

	vfnmag  = (float)((rc2+1.0) * m2 * vin2 + vin * (m-rc2 * m2)) /(m+m2);  //Magnitude of velocities after collision
         vfnmag2 = (float)((rc+1.0) * m * vin + vin 2 *(m-rc2 * m2)) / (m+m2);

	velx  = N.GetX()*vfnmag+tx*vftmag;   //Final velocities after collision
         vely  = N.GetY()*vfnmag+ty*vftmag;
	velx2 = N.GetX()*vfnmag2+tx*vftmag2;
	vely2 = N.GetY()*vfnmag2+ty*vftmag2;

	b1Velocity.Set(velx, vely);
	b2Velocity.Set(velx2, velx2);

Share this post

Link to post
Share on other sites

Well, I was looking over my post and saw an error in my coded ><. When I set the final velocities, I accidentally set the second balls y-component to x-component's velocity. I fixed that and it runs perfectly.

Share this post

Link to post
Share on other sites
This line seems fishy:

vfnmag2 = (float)((rc+1.0) * m * vin + vin 2 *(m-rc2 * m2)) / (m+m2);

I think it should be

vfnmag2 = (float)((rc+1.0) * m * vin + vin2 *(m2-rc1 * m)) / (m+m2);

I'm also not sure why you need two different coefficients of restitution? It's the same collision.


Share this post

Link to post
Share on other sites
don't apply collision response if the two objects are separating. That is, is the dot product of relative velocity and nromal of collision is positive. Else, you'll be adding an impulse that will push the balls towrads each other, hence the stickiness.

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!