Jump to content
  • Advertisement
Sign in to follow this  

My first runge-kutta implementation

This topic is 4970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

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:


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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!