racer home

Physics tips

 

Home Nice graphics are good to draw attention, but underneath, the physics do the real fun job.


Dolphinity Organiser - free planning, project management and organizing software for all your action lists

 

This page contains programming tips for your physics code.

2nd ORDER INTEGRATION ACCURACY FOR ALMOST FREE

What you often see is integration steps that go like:

F=CalcForce();
acc=F/m;
pos+=vel*t;
vel+=acc*t;

This is a Euler step. If you've read David Baraff's papers on integration, you may have noticed that the Euler integration is just the first part of an infinite Taylor series. The above formula has an error of O(h), meaning not too accurate in practical terms. It will explode quite quickly.

An alternative is RK4 (Runge-Kutta fourth order), which takes you to O(h^5), but that requires some work. A shortcut you can take to get to O(h^2) is realising you're calculating the 2nd order derivative of 'pos' with the force calculations. This means you can use more of the Taylor series for free. I won't outline the exact Taylor series, but here's the result in code:

F=CalcForce();
acc=F/m;
pos+=vel*t+0.5*acc*t*t;
vel+=acc*t;

In math, this is like: x=x+f(x)*dt+0.5*f'(x)*dt^2. Ofcourse it will be a good idea to precalculate t*t (even if you have variable timesteps).

This is used in some places in Racer v0.4.9.

 


Dolphinity Organiser - free planning, project management and organizing software for all your action lists

(last updated November 13, 2012 )