My first runge-kutta implementation

Started by
2 comments, last by Airo 19 years, 5 months ago
Hello Ive been working on a numerical simulation of a trebuchet for a school project lately. Ive created the throwing arm as a thin rod with some inertia. The heavy counterweight is a pointmass on the short side of the arm and the sling is a spring string with large spring constants to make it resemble stiff rope attached to the longer side of the arm. Till now im using the euler method to integrate velocity from acceleration over time etc. This method requires very small timesteps or else it will screw up. Because of this it takes very much computorpower to simulate one swing. Therefor I ve desided to change to the runge-kutta method. Problem is, I simply dont understand how this method is implemented. Youre supposed to have one function f(t,x) that in my case would describe the nodes acceleration and then you get the velocity by some formula (I understood the formula, but not the f(t,x) part). What am I supposed to put into this function f(t,x) and what is it supposed to do? //Grateful for all answers
Emil Jonssonvild
Advertisement
f(x,t) is the force function, you use it to get the derivative of the velocity. I've posted a very small snippet here for RK4.

Euler makes one jump using f(x,t) for acceleration, RK4 makes 4 jumps to get a more accurate acceleration.
yeah I gathered that. what puzzeles me is the actual code to do just that.

what i do now is

void updatenode(node *n, float deltaTime)
{
/*calculate the acceleration (acc) based on distance to adjacent nodes and spring constant*/

n->vel += acc*deltaTime;
n->pos += n->vel*deltaTime;
}

how would i convert this to runge-kutta?
Emil Jonssonvild
http://www.gamedev.net/community/forums/topic.asp?topic_id=275009&whichpage=1�

here's the thread.

basically, you just do something like this:

xtemp = x;
vtemp = v;

getforce(); //calculates force based on xtemp

k1 = v*dt/2 jump halfway
l1 = a*dt/2

xtemp = x + k1
vtemp = v + l1

getforce() //calculate a new force based on xtemp halfway

k2 = ..
l2 = ..

bla bla

x = x + 1/6(k1+2*k2+2*k3+k4)*dt //weighted velocity

you calculate the force 4 times, and use a weighted average to update

in Euler you do:

getforce()

x = x + v*dt
v = v + a*dt

This topic is closed to new replies.

Advertisement