Velocity Verlet almost 4th order

Share your projects and Chipmunk enhancements.
Post Reply
User avatar
Posts: 24
Joined: Wed Jan 30, 2008 3:52 pm

Velocity Verlet almost 4th order

Post by juanpi »

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->v = cpvadd(body->v, cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), 0.5e0*dt));
	body->w = body->w + body->t*body->i_inv*0.5e0*dt;
	cpArray *bodies = space->bodies;
	cpBody* B;
	cpVect delta,f;
	Sing *aux=(Sing*)body->data;
	cpFloat r2;
	for(int i=0; i< bodies->num; i++)
	  Sing *aux2=(Sing*)B->data;
        // 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);
	       //Force applied to body A
		   delta = cpv(aux->sing[k].position.x,aux->sing[k].position.y);
	body->v = cpvadd(cpvmult(body->v,damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), 0.5e0*dt));
	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);
	dp = cpvadd(dp,cpvmult(cpvmult(body->f,body->m_inv),0.5e0*dt*dt));
	body->p = cpvadd(body->p, dp);

	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:

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 :lol:
Posts: 144
Joined: Tue Mar 04, 2008 2:21 am

Re: Velocity Verlet almost 4th order

Post by aisman »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests