Sign in to follow this  
cannonicus

My first runge-kutta implementation

Recommended Posts

cannonicus    136
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

Share this post


Link to post
Share on other sites
Airo    197
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.

Share this post


Link to post
Share on other sites
cannonicus    136
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?

Share this post


Link to post
Share on other sites
Airo    197
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this