hi there long time no post
I have been doing a car physics game for some time now and i was not satisfied with the lacking accuracy and the annoying habit euler integration has to explode the simulation in your face.
so i googled abit and found a great tutorial on numerical integration using the Runge-Kutta method, but sadly it was for C++ and as C++ has the convenience of methods and such it took me quite a while to decipher as i have no previous experience with anything but DBP.
So i thought to share the deciphered DBP version of the RK¤ numerical integrator.
for those uninitiated in calculus, integration is finding the area under a function ( graph ), this might sound abstract but actually it has millions of uses in physics. Euler integration is for example this : position = position+velocity*change in time this might seem right and it will work fine for platformers and such but for accurate simulation of physics the error of that method is quite too much. SO there we have integrals! but as it is quite difficult to integrate symbolically for all of your problems and solving the primitive function is not necessarily even faster than a good numerical integrator like RK4
So to the point:
function RK4(p,v,t#,dt#)
dv = createvector2() : sv = createvector2() : sp = createvector2() : dp = createvector2() : f = createvector2()
RK4evaluate(p,v,t#,0,dp,dv,sp,sv,f)
ax# = x vector2(sv) : ay# = y vector2(sv) : avx# = x vector2(f) : avy# = y vector2(f)
RK4evaluate(p,v,t#,dt#*0.5,sv,f,sp,sv,f)
bx# = x vector2(sv) : by# = y vector2(sv) : bvx# = x vector2(f) : bvy# = y vector2(f)
RK4evaluate(p,v,t#,dt#*0.5,sv,f,sp,sv,f)
cx# = x vector2(sv) : cy# = y vector2(sv) : cvx# = x vector2(f) : cvy# = y vector2(f)
RK4evaluate(p,v,t#,dt#,sv,f,sp,sv,f)
dx# = x vector2(sv) : dy# = y vector2(sv) : dvx# = x vector2(f) : dvy# = y vector2(f)
set vector2 sp,1.0/6.0*(ax#+2*(bx#+cx#)+dx#),1.0/6.0*(ay#+2*(by#+cy#)+dy#)
set vector2 sv,1.0/6.0*(avx#+2*(bvx#+cvx#)+dvx#),1.0/6.0*(avy#+2*(bvy#+cvy#)+dvy#)
addvec(p,sp,dt#) : addvec(v,sv,dt#)
u = delete vector2(dv) : u = delete vector2(dp) : u = delete vector2(sp) : u = delete vector2(sv) : u = delete vector2(f)
ENDFUNCTION
function RK4evaluate(p,v,t#,dt#,dp,dv,sp,sv,f)
addvec2(sp,p,dp,dt#) : addvec2(sv,v,dv,dt#)
RK4accelleration(sp,sv,t#+dt#,f)
ENDFUNCTION
function RK4accelleration(sp,sv,t#,f)
k# = RK4k
b# = RK4b
set vector2 f,-k#*x vector2(sp)-b#*x vector2(sv),-k#*y vector2(sp)-b#*y vector2(sv)
ENDFUNCTION
function addvec(v1,v2,dt#)
set vector2 v1,x vector2(v1)+x vector2(v2)*dt#,y vector2(v1)+y vector2(v2)*dt#
ENDFUNCTION
function createvector2()
n = find free vector()
u = make vector2(n)
ENDFUNCTION n
THE RK4 function is the only one you have to call in your loop
THE RK4accelleration contains the simulation specific functions, here a dampened spring simulation
THE p and v vectors are position and velocity
THE t# is useless actually
THE dt# is timestep ( delta time )
the function will not return a value, it will change the p vector
this is cryptic i know, but thats because its copied straight from my porgram. IF someone is interested and have failed at deciphering my gibberish code, please ask and i will do my best to ansvear