Velocity Verlet almost 4th order

Share your projects and Chipmunk enhancements.
juanpi
Posts: 24
Joined: Wed Jan 30, 2008 3:52 pm
Contact:

Velocity Verlet almost 4th order

Hello Again,
Today was a productive day.
Here is the recipe to implement Velocity Verlet in Chipmunk.

1. Define Two new integration functions

Code: Select all

``````void ChargedBodyUpdateVelocityVerlet(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
body->w = body->w + body->t*body->i_inv*0.5e0*dt;

body->f=cpvzero;
body->t=0.0e0;

cpArray *bodies = space->bodies;
cpBody* B;
cpVect delta,f;
Sing *aux=(Sing*)body->data;
cpFloat r2;
for(int i=0; i< bodies->num; i++)
{
B=(cpBody*)bodies->arr[i];
Sing *aux2=(Sing*)B->data;
if(!(B==body))
{
// Calculate the forces between the singularities of different bodies
for (int j=0; j<aux->Nsing; j++)
{
for (int k=0; k<aux2->Nsing; k++)
{
delta = cpvsub(aux2->Gpos[k],aux->Gpos[j]);
r2 = cpvlengthsq(delta);
f=cpvmult(cpvnormalize(delta),
-aux->sing[j].value*aux2->sing[k].value/r2);
//Force applied to body A
delta = cpv(aux->sing[k].position.x,aux->sing[k].position.y);
cpBodyApplyForce(body,f,delta);
}
}
}
}
body->w = body->w*damping + body->t*body->i_inv*0.5e0*dt;

}
void ChargedBodyUpdatePositionVerlet(cpBody *body, cpFloat dt)
{
cpVect dp = cpvmult(cpvadd(body->v, body->v_bias), dt);

cpBodySetAngle(body, body->a + (body->w + body->w_bias)*dt
+ 0.5*body->t*body->i_inv*dt*dt);

// Update position of the singularities
aux = (Sing*)body->data;
for (int i=0; i<aux->Nsing; i++)
aux->sing[i].position.y), body->rot));

body->v_bias = cpvzero;
body->w_bias = 0.0f;
}
``````
1a. As you see the algorithm requires a little bit more of computation but not too much. I am sure it can be done in a better way. This one works (For more details look at the post "6 steps to long range interaction").

1b. The definition of the algorithm can be found here:
http://en.wikipedia.org/wiki/Verlet_integration

I checked it and the total energy is pretty well conserved (except in collisions). The order of the algortihm is something like O(dt^3.4778). It should be O(dt^4) but I guess the collisions add errors in the speed. So it must be O(dt^a, N^b), where N is some kind of average number of colliding shapes or the number of total collisions.

Enjoy
aisman
Posts: 144
Joined: Tue Mar 04, 2008 2:21 am
Contact:

Re: Velocity Verlet almost 4th order

Maybe it can be add 'nativ' to the Chipmunk 5.3.3 release?
Chipmunk4PB: The fastest way to write games together with PureBasic and the Chipmunk physics engine.

Who is online

Users browsing this forum: No registered users and 2 guests