#### Archived

This topic is now archived and is closed to further replies.

# Physics problems: frame takes so long that calculations are incorrect

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

## Recommended Posts

I implemented planet gravity in a space simulator I'm making, and I calculate the gravitational force of the planet with the universal gravitation formula G*m1*m2/d*d or in other words simply Cte/(d*d), and everything for the formula etc... is correct. Then, I calculate what velocity I should have because of this acceleration, for example if the ship accelerator at 1 m/s² for 1 second, it should of course move at 1m/s after 1 second and for 2 m/s after 2 seconds. So if a frame took 0.01 seconds, the ship will move 0.01m/s faster than the previous frame because it has accelerated at 1m/s² during 0.01 seconds. For a constant acceleration, this doesn't give problemd. BUT the accelaration from the gravitational froce from a planet isn't a constant, but it depends on the distance from your spaceship to the planet. If the ship is very close to the planet, it will accelerate very fast (because the formula is force = cte/d², and close to the center d is very small). So then the ship accelerates very fast during the 0.01 seconds a frame takes, and my engine calculates the speed it should get if the ship would have moved at that high acceleration during these 0.01 seconds. But in reality, after MUCH LESS than 0.01 seconds the accelaration has changed already!! So the calculation I use isn't accurate, not at all even because sometimes if I'm close to center of the planet at slow speed, my ship suddenly gets shot away very far because the engine things it accelerated at 1000m/s² during 0.01 second while it was maybe only 0.000001 second in reality So in my case I can do the calculation only 85 times a second (even less on slower computers), but I still would like to get an ACCURATE result. How to solve this?? Thanks. BTW because I also like to speed up the gametime, this problem becomes even bigger if I speed it up, then it's also ***extremely*** inaccurate at larger distances of the planet. [edited by - Lode on July 30, 2002 2:55:39 PM]

##### Share on other sites
Hmm I found that I need a formula to calculate:

*accelaration of ship after time t
*velocity of ship after time t
*position of ship after time t

if only the following is known:

*velocity of the ship before time t
*position of the ship before time t
*position of the planet
*the constant C*m1*m2
*the length of time t (around 1/85 of a second here)

All these things are known, I just don''t know the formula, does anyone know it? Pretty please?

##### Share on other sites
I know what you mean. Crazy gravity simulations...

IMHO, what you need is an integral. For example, if you know that the 'acceleration of the acceleration' (is it called 'jerk'?) is constant, then position is:

x = x0 + v0*t + a0*t²/2 + j*t³/3

Integrating G * m1 * m2 / d² dt doesn't look trivial to me. You could do it numerically... Which boils down to splitting your time steps into smaller time steps when a is big, to increase the accuracy and circumvent the problems you mentionned.

You could also assume that the problem is unidimensional:

x = integral(integral(k / t² dt) dt), where k = G * m1 * m2
x = k * integral(-1 / t dt)
x = -k * ln(t)

Uhhh... I guess I should say that I almost failed my calculus course last session, and the preceding equations don't look quite right.

Anyway, hope this helps,

Cédric

EDIT: No, they're not right at all. Maybe...

d²p/dt² = G * m1 * m2 / (x² + y²)

That looks better. But I haven't worked with integrals of vectors, so I can't help you there.

[edited by - cedricl on July 30, 2002 4:30:03 PM]

##### Share on other sites
Hmm I know integrals, but I think I need some more help here

I was just thinking that not only the value but even the direction of the acceleration is changing during the time, that''s terrible

##### Share on other sites
Are you using vectors?

If so, imagine this: Create a vector in the direction you want your ship to travel and make the magnitude of that vector the speed of the ship. Also, use a vector field to represnt your gravitational body. You use the time that has passed between frames as a percentage to multiply the magnitude of the vectors by to get the new position. Multiply this percentage by the result of adding your velocity of the ship and the vector for the vector field.

I believe it should follow the same curve for different rendering speeds. Another genious of this method would be bullet time (like in max payne) Simply throw in a coefficient for the percentage. A coefficient of 1 is normal speed. A coefficient of .5 is 1/2 speed.

Also, try updating your frames only 30 times a second, while still drawing at the max speed.

##### Share on other sites
Same AP as above.

I just wanted to say I passed my Cal3 class with an ''A''. These ideas represnted above are from Calculus 3, or Vector Calculus, but they are relatively simple ideas to grasp (compared to Tangential and Normal Components of an acceleration vector). I''m sure you can use a vector field without ever knowing what a gradient is.

##### Share on other sites
hhmm... that a slow mass, coming really close to a great
mass gets suddenly damn fast seems very normal.
but if it looks jerky... dunno..

If I remember correctly, in André LaMothe''s
"Tricks of the Game Programming Gurus" he shows how to do
such a grav sim, in 2D. I haven''t this book accessible right now.

Maybe somebody just could scan and post this example?
(if it''s legal to put book fragments online)

Or maybe you even find this example on LaMothe''s page.
http://www.xgames3d.com/

It is now for sure you can throw away your computer

1. 1
2. 2
3. 3
4. 4
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632909
• Total Posts
3009172
• ### Who's Online (See full list)

There are no registered users currently online

×