next up previous
Next: Visualization Up: How To Implement a Previous: Pressure Force Distribution

Integrate Newton's Equations

We finished force accumulation procedure and we are now able to start integration of equations of motion for all particles. To make the solution as simple as possible I decided to show procedure which does integration with simple Euler 1st order integrator. In appendix B you will find description of how to implement second orded semi-implicit predictor-corrector Heun integrator which allows to set bigger time steps or add more elasticity to simulated objects. However, for two dimensional simulation even first order Euler is enough to do working simulation.

For our purposes instead of integrating second order ODE11:


\begin{displaymath}
\frac{d^2\vec{r}_i}{dt^2} = \frac{\vec{F}_i}{m_i}
\end{displaymath} (4)

We use relation:


\begin{displaymath}
\frac{d\vec{r}_i}{dt} = \vec{v}_i
\end{displaymath} (5)

And integrate first order ODE:


\begin{displaymath}
\frac{d\vec{v}_i}{dt} = \frac{\vec{F}_i}{m_i}
\end{displaymath} (6)

Let us take a look on the Euler integration procedure which integrate above two equations for all $i=1,2, \ldots, NUMP$ points:

void IntegrateEuler()
{
  int i;
  float dry; 

  for(i=1 ; i <= NUMP ; ++i)
  {
    /* x */
     myPoints[i].vx = myPoints[i].vx + 
      ( myPoints[i].fx / MASS )* DT;
     myPoints[i].x = myPoints[i].x + 
        myPoints[i].vx * DT;

     /* y */
     myPoints[i].vy = myPoints[i].vy + 
                   myPoints[i].fy * DT;

     dry = myPoints[i].vy * DT;

     /* Boundaries  Y */
     if(myPoints[i].y + dry < -SCRSIZE)
     {
       dry = -SCRSIZE - myPoints[ i ].y;
       myPoints[i].vy = - 0.1 *myPoints[i].vy;
     }

     myPoints[i].y = myPoints[i].y + dry;
  }
}

As we can see in above code in an integration procedure we also do simple collision test with an enviroment. We just test point $y$ component if it is less than $-SCRSIZE$ then we reflect $y$ component of point velocity and we reflect point movement. To make solution more stable and accurate I propose you to use different kind of integrator - see Appendix B, where semi-implicit Heun predictor-corrector scheme has been described.


next up previous
Next: Visualization Up: How To Implement a Previous: Pressure Force Distribution
Maciej Matyka 2004-03-30