The easiest way to have one object orbit another is to use spherical co-ordinates instead of the usual cartesian.
Once the spherical co-ordinates are determined it is a simple task to convert them back to cartesian.
I have created this small bit of code that demonstrates a planet orbiting a sun and a moon orbit that planet.
`Orbit function
`======================
`©Scraggle
`======================
`27th July 2010
#constant cPLANET 1
#constant cMOON 2
#constant cSUN 3
make object sphere cPLANET, 20
make object sphere 2, 10
make object sphere 3, 50
color object cPLANET, rgb( 0, 255, 0 )
color object cMOON, rgb( 128, 128, 128 )
color object cSUN, rgb( 255, 255, 0 )
position object cSUN, 0, 0, 0
position camera 0, 0, 0, -200
point camera 0, 0, 0, 0
type vect
x as float
y as float
z as float
endtype
type satellite
theta as float
phi as float
radius as float
speed as float
endtype
global coords as vect
global planet as satellite
global moon as satellite
planet.Theta = 180.0
planet.Phi = 45.0
planet.Radius = 75.0
planet.Speed = 1.0
moon.Theta = 180.0
moon.Phi = 20.0
moon.Radius = 20.0
moon.Speed = 5.0
sync on
sync rate 60
do
//convert the spherical co-ordinates to cartesian
spherical2cartesian( planet.Theta, planet.Phi, planet.Radius )
//increase the orbit position
planet.Theta = wrapvalue(planet.Theta + planet.Speed)
//orbit the planet
position object cPLANET, object position x(cSUN) + coords.x, object position y(cSUN) + coords.y, object position z(cSUN) + coords.z
//similar code for the moon
spherical2cartesian( moon.Theta, moon.Phi, moon.Radius )
moon.Theta = wrapvalue(moon.Theta + moon.Speed)
position object cMOON, object position x(cPLANET) + coords.x, object position y(cPLANET) + coords.y, object position z(cPLANET) + coords.z
sync
loop
end
function spherical2cartesian( theta as float, phi as float, radius as float )
coords.x = radius * sin(theta) * cos(phi)
coords.y = radius * sin(theta) * sin(phi)
coords.z = radius * cos(theta)
endfunction
[Edited for clarity]