#### Archived

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

# Gravity

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

## Recommended Posts

I am haveing problems creating gravity for my program. Dose anyone have a realy fast way of doing this. Give me the math or point me to a website that will give me a good idea of how to accomplish this. A man walks into a bar.....ouch!?! Ya get it, do ya huh huh well....awww I give up.

##### Share on other sites
Gravity is just a form of acceleration. If you keep the fps at constant speed will you avoid a lot of trouble.

##### Share on other sites
Where is a good place to learn how to keep your fps at a constant rate. Nate''s page used to have it but he took it down.

##### Share on other sites
  #define gravity 9.8program_speed = (1/frames_rendered)*time_since_start_in_seconds;speed.y -= gravity*program_speed;object.y += speed.y;

If your model is scaled so that one unit is one meter, then I think that is right. Things fall at a rate of 9.8 metres per second on earth. If you don't use metric, then a) you'll need to convert gravity to some other number and b) you must have a hard time with opengl (kind of hard to use fractions, feet, and inches). You can add speed*program_speed to something to make it move at a rate of speed units per second.

Edited by - £~{ÿùÖ² on ƒUƒP£ÖÿÜ -2342112, 25223 64:123:21 £m

Edited by - smart_idiot on February 1, 2001 1:25:31 PM

##### Share on other sites
sorry but i think you''re wrong....

s = 1/2 * a * t^2

s = the distance...
a = 9.81m(eter)/s^2 //const.
t = time in seconds

i won''t explain this becaus my english isn''t good enough... :-)
but this works only for the case, that the object hasn''t speed in the -y(or what ever...) direction...
else:
s = v * t + 1/2 * a * t^2

v = velocity in the same direction wich the object has already...
hope you could understand me.... :-)

##### Share on other sites
I think that the code smart_idiot showed is OK if the framerate is constant. If you start with Physiks formulas will you end in something like that code.
Right, you can remove program_speed.

Several of the NeHe tutorials is timed like lesson 23.

##### Share on other sites
Small tutorial with full source code and a demo including gravity detection & response: Physics - Gravity

(ps: It''s about a "perfect" animation - not a fast approximation)

##### Share on other sites
Thanks for all your suggestions they were quite helpfull. I have been reading quite alot about physics and I have a very basic routine set up. It should do for now ounce I refine the engin a little I will add an increaseing velocity (mine is currently constant).

##### Share on other sites
It could be wrong, but I looks right to me.

In my programs after 100 frames I calculate the speed, average it with the old speed, then I reset the start time and elapsed frame counters.

It seems to work. The idea is that if you add up the sum after rendering all the frames for one second, it adds up to one. I did it this strange way because I couldn''t figure out how to get a timer that was more accutate than 1/18th of a second. There was probably an easier way, but this works for me. You may have different needs.

##### Share on other sites
Acceleration is not linear so it is not that easy. Perhaps your test program did have an almost constant speed?

• 39
• 15
• 9
• 23
• 10