### Gravity and deltas

closed account (2NywAqkS)
I've got a nice program I'm working on which simulates gravitational orbits however I'm having a lot of problems surrounding the use of time deltas. Where should they go, because where ever I put the * delta, adjusting it completely changes the orbit path(e.g. Flies of into space).

My Code:
 ``12345678910111213141516`` ``````double dx = -x; double dy = -y; double direction = atan2(dy, dx); double distanceSquared = (dx*dx + dy*dy) / 10; double force = 0.004 * ((mass * solarSystem->mass)/(distanceSquared)); double acceleration = force / mass; double xacceleration = acceleration * cos(direction); double yacceleration = acceleration * sin(direction); xVel += xacceleration; yVel += yacceleration; x += xVel * delta * 0.01; y += yVel * delta * 0.01;``````
`double distanceSquared = (dx*dx + dy*dy) / 10;` ¿why 10?
`x += xVel * delta * 0.01;` ¿why 0.01?

> Where should they go
Where there is time involved.

Also, you are considering that acceleration and velocity are constant in a chronon, that will introduce error.
closed account (2NywAqkS)
 ¿why 10?
to give the effect that it is closer.
 ¿why 0.01?
to slow down time in my program.

As for your last point, I have thought of that but I can't think of a solution. Is it the chronon problem that is cause the inconsistencies in relation to fps? or do I need to multiply more things by delta?
When I do these types of problems, I like to add dT as an argument. Then my callback timer will determine the value of dT when calling this function.

Now, in terms of where I´d put dT in this case: Certainly lines 12 and 13 need it. In those lines you are summing the acceleration. I think you mean to integrate it. remember:
`a = dv/dt;` so `dv = a*dt`

Likewise:
`v = dx/dt;` so `dx = v*dt`

That means that you certainly need it in lines 15 and 16 as well (which you already have).

Regarding the 0.01, don´t factor it in here. It isn´t part of the integration. Consider it only when calculating the dT so that no matter when you use delta, it will be correct. It certainly is not a problem to do this, especially since simulating the earth around the sun would otherwise take a year to compute, but you have to ensure that the same constant is applied everywhere and the only safe way to do that is at the source.
Last edited on
Topic archived. No new replies allowed.