3D Motion Simulation

Zav 101 Apr 08, 2009 at 05:39

i try to build a simulation of an object in 3d space.
the object is point object with properties like this:

p : current position of an object.
v : current velocity of an object.
f : current force applied on an object.
m : object’s mass (1 kg)
dt : time elapsed since previous step

start condition:
v = (0, 0, 10)
p = (50, 0, 0)

at every simulation step (about 0.1 sec) the following calculations are made:
dt = GetTimeSinceLastStep()
f = CalculateForce()
v = v + (f / m) * dt
p = p + v * dt

the function CalculateForce calculates the current force at every step.
let’s assume that this function always return force in zx plane, perpendicular to the current velocity toward (0, 0, 0).

in such case, object suppose to perform circular motion with constant speed about the origin in zx plane. but here is my problem:
[(f/m) * dt] vector (that added to current velocity) will always have some length. so after addition it to v, the new v vector will be longer than the previous one (hypotenuse is always greater than two other sides in triangle).

so in my simulation the object always increase it’s speed and instead of circular motion i have a spiral one.


where is an error ???????

thanx in advance

2 Replies

Please log in or register to post a reply.

Reedbeta 167 Apr 08, 2009 at 05:48

There isn’t necessarily any error. In simulations like this, using discrete timesteps always produces inaccurate results. You should be able to improve the accuracy by using smaller timesteps. (Mathematically, I think that as you let the timestep go to zero, the change in speed will go as the timestep squared, so it will vanish in the limit.)

Beyond that, more sophisticated integration methods are possible. What you are using is known as Euler integration, and is a simple but not particularly accurate method. You could upgrade to the midpoint method, or go all out with RK4. (Those links are to Wikipedia articles that discuss the methods in the context of general differential equations. You can probably find articles specialized to game physics simulations with a little googling.)

Rubicon 101 Apr 08, 2009 at 21:40

It increases speed because your model is increasing it’s speed, there’s no code bug. You’re adding a vector to another one, so the overall length will of course continue to change.

You need to work with direction and speed separately, which means normalise and scaling of the components as you go. If you’re only trying to change direction, then get the magnitude beforehand, add your vector then normalise the result and scale it back to the original magnitude.

Or make a better model. If you’re simulating gravity attraction, then do that. Add the velocity to the position, then subtract your gravity direction from the velocity