# problems with elastic collisions

• 12-14-2012
Tcc
problems with elastic collisions
I'm trying to replicate the movement of 2 masses, m1 and m2.

m1 has a free fall movement and will collide with m2, which is attached to a string with constant k. i'm using the euler-cromer method to solve the dif equations. The important part of the code is the following:

Code:

```  dt = tf/1000;   for(i=0; tf > i*dt; ++i)     {       //corpo1       v1 = v1 - g * dt;       y1 = y1 + v1 * dt;           //corpo2       v2 = v2 - (k/m2) * (y2-L0) * dt;       y2 = y2 + v2 * dt;             if(fabs(y1<y2))               {           //elastic collision           y1  =  y2 + fabs ( y1 - y2 );               v1 = ((m1-m2) / (m1+m2)) * v1 + ((2*m2) / (m1+m2)) * v2;           v2 = ((2*m1) / (m1+m2)) * v1 + ((m2-m1) / (m1+m2)) * v2;            }             fprintf(f1, "%10lf  %10lf  %10lf\n", i*dt, y1, y2);      }```
However, whenever m1 hits m2 they kind of stick together, being that y1 is always similar to y2, which is not at all the desired outcome. Any thoughts?
• 12-14-2012
gemera
ye cannae change the laws of physics Captain. :biggrin:
• 12-14-2012
grumpy
I haven't bothered to read further, since you haven't given useful information about your problem (context, representative values of variables, etc) but you might want to ask whether this line does what you intend. I will bet it does not.
Code:

`    if(fabs(y1<y2))`
If you want a more useful answer, you are better off providing a SMALL but COMPLETE sample of code that illustrates your problem. Also describe inputs and expected outputs.