Jump to content
  • Advertisement
Sign in to follow this  
levjs

Total Conservation Problem![SOLVED]

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

Hello, This may seem dumb to some of you physics scholars out there, but I'm trying to create totally elastic collisions between spheres. In other words, no energy is lost or gained. I thought I had it, until I added up the forces to make sure it was being conserved. It turns out, each time a collision occurs, the total force is changed, but it seems to gain or lose energy from collisions randomly, and the total energy seems to stay within a certain range. I'm trying to figure out where in the world I made my mistake. Here is my source, but I don't see any mistakes in it, and I've been over it a million times. What is wrong!?
void DAC()
{
double gammav=0,gammaxy=0,dgamma=0, a=0,alpha=0,dist=0,mindist=0,dr=0,dvx2=0,pi2;
double x,y,x2,y2,vx1,vy1,vx2,vy2,r1,r2,m1,m2;
int l,l1;
for(l=0;l<B;l++)
{
	for(l1=0;l1<B;l1++)
	{	
		if(l!=l1)
		{
			x=ast[l].x;y=ast[l].y;vx1=ast[l].vx;vy1=ast[l].vy;m1=ast[l].m;r1=ast[l].r;
			x2=ast[l1].x;y2=ast[l1].y;vx2=ast[l1].vx;vy2=ast[l1].vy;m2=ast[l1].m;r2=ast[l1].r;
			pi2=2*acos(-1.0E0);
			dist=sqrt(pow(x2-x,2)+pow(y2-y,2));
			mindist=r1+r2;
			gammav=atan2(-(vy2-vy1),-(vx2-vx1));
			gammaxy=atan2(y2-y,x2-x);
			dgamma=gammaxy-gammav;
			if (dgamma>pi2)dgamma=dgamma-pi2;
			else if (dgamma<-pi2)dgamma=dgamma+pi2;
			dr=dist*sin(dgamma)/(r1+r2);
			if((fabs(dgamma)>pi2/4 && fabs(dgamma)<0.75*pi2) || fabs(dr)>1 )   
			{
			}
			else
			{
				alpha=asin(dr);
				a=tan( gammav +alpha);
				dvx2=-2*((vx2-vx1) +a*(vy2-vy1)) /((1+a*a)*(1+(m2/m1)));
				if(dist<mindist)
				{
					ast[l1].vx=vx2+dvx2;
					ast[l1].vy=vy2+a*dvx2;
					ast[l].vx=vx1-(m2/m1)*dvx2;
					ast[l].vy=vy1-a*(m2/m1)*dvx2;
				}
			}
		}
	}
}

}


[Edited by - levjs on January 5, 2006 3:55:55 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think your best bet would be to avoid angles at all. In this situation, you want to try to conserve momentum between collisions. That means (m1v1+m2v2)i = (m1v1+m2v2)f

An easy way to handle this is considering that objects of similiar mass tend to transfer momentum during a collision. That means that mv of object 1 before a collision will equal mv of object 2 after the collision.

Hope this helps

On a side note,
pi2=2*acos(-1.0E0);
is kind of wasteful. You should use
pi2=2*M_PI;

Share this post


Link to post
Share on other sites
Original post by erissian
I think your best bet would be to avoid angles at all. In this situation, you want to try to conserve momentum between collisions. That means (m1v1+m2v2)i = (m1v1+m2v2)f

An easy way to handle this is considering that objects of similiar mass tend to transfer momentum during a collision. That means that mv of object 1 before a collision will equal mv of object 2 after the collision.

Hope this helps

Ok, I understand that, but how would you use that formula to get the velocity(with direction and speed) after a collision?
And also, I've been working on this for soooo long on and off, and I'd like to figure out if something is wrong with my code or something else is wrong!
It is true, that when you add the total velocities together(x+x and y+y, of all the balls, and then add those together, that number should never change, right??????)
Thanks
Levi

Share this post


Link to post
Share on other sites
Quote:

Ok, I understand that, but how would you use that formula to get the velocity(with direction and speed) after a collision?


The equations for the velocities after collision can be easily found through combination of preservation of linear momentum and preservation of kinetic energy.
If you leave out any angular effects (kinetic energy = mu^2/2), the equations for the elastic collision of two objects, become:
v1f = v1i*(m1-m2)/(m1+m2) + v2i*2*m2/(m1+m2)
v2f = v1i*2*m1/(m1+m2) + v2i*(m2-m1)/(m1+m2)

(The 'i' and 'f' denote 'initial' and 'final')
Remember also, that these are vector equations. Therefore, a snapshot of each of these formulas must hold for each component of the velocity vectors. This means that you'll have to calculate these 2 or 3 times (depending on your simulation beaing 2d or 3d) for each component of the bodies' velocities.

Quote:

It is true, that when you add the total velocities together(x+x and y+y, of all the balls, and then add those together, that number should never change, right??????)

I'm not quite sure I understand what you mean. You don't discriminate anywhere between initial and final velocities.

edit start:
Do you mean the preservation of kinetic energy in elastic collision?
This is quite obvious, Sum[k=1,...,#bodies][mk*(vki)2]==Sum[k=1,...,#bodies][mk*(vkf)2]
:end edit

However, it holds true, that the relative velocities between the objects remain constant in magnitude but completely change direction. (if that's what you meant)
Thus: vi1-vi2 = -(vf1-vf2)

Share this post


Link to post
Share on other sites
Thanks for the equations, I'll give them a try. : )

What I mean is, say that I have two spheres in two dimensions. If I add the x components of the velocities of each spere to the x component of the other, and the same for the y. If I then add the numbers I got, I would come up with one number of the total force, right? And this number should not change no matter how many times the spheres collided? Is that any clearer?


My collisions as is look like they should. The spheres bounce off eachother correctly, with(it looks like) the right speed according to mass. The only thing I'm not getting is why the number I described above changes everytime any spheres collide.
Thanks alot.
Levi

Share this post


Link to post
Share on other sites
I get the question, but I don't see why something like that should hold, unless you impose such a constraint yourself. I guess I'll have to sleep on it...

Share this post


Link to post
Share on other sites
I guess because if there is a conservation of energy, then it would hold because the total energy should be the same?? I'm going to die if I find out that I've had it right, and this is just some stupid thing I made up!!!!
Here's the code that adds up the velocities. The total(number that changes is xtotal+ytotal).
xtotal=0.0;ytotal=0.0;
for(int l=0;l<B;l++)
{
xtotal+=ast[l].vx;
ytotal+=ast[l].vy;
}



So the code in my original post looks fine?? [grin]
Levi

Share this post


Link to post
Share on other sites
You could break this down componentwise, where you have two equations,
I. px,i = px,f
II. py,i = py,f
(where px = m*vx)

This should be fine for what you need. It could work something like this:

float vx1,vx2,m1,m2;
float px1,px2;

/* Collision! */

/* Calculate momentum */
px1 = vx1 * m1;
px2 = vx2 * m2;

/* Exchange momentum */
float a = px1;
px1 = px2;
px2 = a;

/* Find new velocities */
vx1 = px1 / m1;
vx2 = px2 / m2;


Where adding new dimensions is simplify repeating this process for each dimension.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The sum of velocities in all dimensions present should be constant if the masses of the objects are identical and all collisions are head on.

A non head on collision transferes accelerative force from one dimension to another in a non-linear manner.

The accelerative force propergation is dependant on the masses involved.

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!