Gravity on round object ';..;'

Blitz3D Forums/Blitz3D Beginners Area/Gravity on round object ';..;'

For the sake of simplicity, you can assume the center of mass is at the center of an object.

Oxyz = Object center of mass
Pxyz = Planet center of mass
Gvxyz = Gravity vector
Gdxyz = Gravity direction
Gm = Gravity magnitude

; Units per meter.
; The number of units in a meter in your game.
; I like a distance of 1 to equal a meter, but in a space game that would be far too large a number to use for one meter.
UnitsPerMeter# = 1.0

; Set gravity to 9.8 meters per second squared.
Gravity# = 9.8

; Set this to the number of seconds which has passed since the last frame. This number is most likely to be less than 1. If you are running at a constant 60 frames per second, 1.0 / 60.0 = 0.0166 seconds per frame. You will want to calcualte this each frame probably if the frame times are different.
Time_Delta_Sec# = 0.166

; Calculate vector from object to planet.
Gvx# = Px# - Ox#
Gvy# = Py# - Oy#
Gvz# = Pz# - Oz#

; Calculate legth of vector.
; This is the distance between object and planet.
; The m stands for magnitude.
; A vecotr has a direction, and a magnitude. The magnitude is the length of the vector.
Gm# = Sqr(Gvx#*Gvx# + Gvy#*Gvy# + Gvz#*Gvz#)

; Divide vector by length of vector, to make it a normal.
; A normal is a vector with a length of 1.
; This is the DIRECTION in which gravity is pulling. It is also the gravity's "normal" or "normal direction".
Gdx# = Gvx# / Gm#
Gdy# = Gvy# / Gm#
Gdz# = Gvz# / Gm#


;Now you have a normal indicating in which DIRECTION gravity
;is pulling.

;Now you just need to calculate how much gravity is pulling
;on your object.

; Calculate how much velocity to add this frame.
; Ie, 9.8 meters per second, * 10 units per meter = 98.0 units per second. Then multiply by the number of seconds which have passed this frame, which we will say is 0.01, which would be 100 frames per second, and the result would be that we should move the object 0.98 units this frame.

Gm# = (Gravity# * UnitsPerMeter#) * Time_Delta_Sec#

; Multiply this velocity by the how much the gravity is pulling in each direction, and add the resulting velocity vector to your object's veloicity vector.

Ovx# = Ovx# + Gdx#*Gm#
Ovy# = Ovy# + Gdy#*Gm#
Ovz# = Ovz# + Gdz#*Gm#

; Finally, move your object according to it's velocity, and how many seconds the last frame took to render.

Ox# = Ox# + Ovx#*Time_Delta_Sec#
Oy# = Oy# + Ovy#*Time_Delta_Sec#
Oz# = Oz# + Ovz#*Time_Delta_Sec#

; Make sure you store Oxyz and Ovxyz with your object... you must retain it from one frame to the next!