What would be an easy way to calculate the future position of a body (assuming it doesn't collide with anything). So far, I've tried something like:
future_pos = current_pos + ( linear_velocity * interval_seconds )
But the results are not accurate enough. Do I need to take gravity/dampnig/forces into account? How would that work?
Yes, you need to take any non-zero acceleration into account also, just add a third term like this:
future_pos =
current_pos +
( linear_velocity * interval_seconds ) +
( linear_acceleration * interval_seconds^2 ) / 2
If only gravity is present then linear_acceleration = g ~ (0, -9.82). If other forces also act on the body in question then linear_acceleration is the sum of all these forces, divided by the mass of the body: linear_acceleration = F_sum / m.
Thanks for the reply, one question though, why do you need to divide by 2 here:
( linear_acceleration * interval_seconds^2 ) / 2
Also, I checked how damping affects the linear velocity. After you integrate all forces acting on the body, damping is applied to the resulting linear velocity as follows:
damping = Clamp ( 1 - interval_seconds * linear_damping, 0, 1 )
linear_velocity = linear_velocity * damping
It is a consequence of two steps of integration.
The expression for the body's position is derived like this (lax formalism):
We know the following...
x0 = initial position (current_pos)
v0 = initial velocity (linear_velocity)
a = acceleration (eg gravity)
...and we look for the position (future_pos) at time t (interval_seconds)
We get...
Acceleration: a(t) = a
Velocity; integrate a wrt t: v(t) = a*t + v0
Position; integrate v wrt t: x(t) = a*t^2/2 + v0*t + x0
Set t = interval_seconds, shuffle the terms around and voilá:
future_pos =
current_pos +
( linear_velocity * interval_seconds ) +
( linear_acceleration * interval_seconds^2 ) / 2
The key here is that a is assumed to be constant. If it isn't (which is usually the case in a multibody system) the integration gets more complicated, and worse -- it requires information about future states of a and v that we don't know yet 😕 They can be approximated but it's a tricky business, and so for realtime applications it is, at least as far as I know, common to assume that all forces (that is -- the accelerations) are always constant within sufficiently small intervals.
As for damping, according to your expression for it; as long as interval_seconds and linear_damping is constant then so is damping. Just include it in the above expression by multiplying it with linear_velocity.