5
\$\begingroup\$

I want to simulate a mechanic object like this:

init acceleration a;
init velocity v;
init position x;
loop {
  get delta time dt;
  v = v + a*dt;
  x = x + v*dt;
}

I use the velocity (which is the derivative of position) at the end of each time step to approximate the new position. So this should be exactly what the backward euler method does.

Do I have an error in reasoning?

\$\endgroup\$
10
  • \$\begingroup\$ Looks ok to me... \$\endgroup\$ Commented Dec 12, 2015 at 19:06
  • \$\begingroup\$ It looks to easy to me because forward euler (which tends to be unstable) would be nearly the exact same (with the velocity and position update flipped). \$\endgroup\$ Commented Dec 12, 2015 at 19:12
  • 1
    \$\begingroup\$ The is a correct forward Euler implementation, not backwards. \$\endgroup\$ Commented Dec 12, 2015 at 19:20
  • \$\begingroup\$ But why is it forward euler? I used the derivative of the position at the end of the timestep to update it. I thought that's the definition of backward euler.. \$\endgroup\$ Commented Dec 12, 2015 at 19:34
  • 1
    \$\begingroup\$ The novelty of an implicit method comes from the solution appearing on both sides of the differential equation. What you have here is simply two separate forward Euler quadrature rules. To develop an implicit method will require algebraically manipulating your specific differential equation. I will give you +rep so you can @ me in game-dev chat \$\endgroup\$ Commented Dec 12, 2015 at 23:52

2 Answers 2

2
\$\begingroup\$

Assuming your acceleration is constant, you should be taking the change in velocity into account when calculating your position and therefore you should also update your position first.

Update the position using the current position (xo), the current velocity(vo), and the change in velocity due to acceleration (0.5 * a * t^2):

x = xo + vo * t + 0.5 * a * t^2 (that's delta-time squared)

Then update your velocity (which you're already doing correctly).

v = vo + a * t

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is the correct answer. OP is not taking into account acceleration when calculating the new position. \$\endgroup\$ Commented Dec 15, 2015 at 5:15
1
\$\begingroup\$

This is correct mathematics, though a few things I want to point out.

A) your first position value and first velocity value from loop will always be off.

B)if the dt is near zero, or gets rounded to zero, you'll not get any correct vales.

C)you need a reimann sum, not an Euler. :-).

D) you need the program to use average velocity (dv/2) and average acceleration for correct calculations.

Try something like this:

`

Float accel, vel, pos;
Float da,dv,dx;
Loop {
Get(dT)
Get(newAccel);
da = (accel-newAccel)/dT; //FINDS da in m/s2
accel = newAccel;
dv = (da*dT)/2; //uses the average acceleration (IVT) to find dv
dx = (dv*dT)/2; //uses average change in vel (ivt) to find dx
Vel += dv;  //add change in vel
Pos += dx; //add change in pos

}
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.