-- Title: Orbit an object -- Author: Chris Laurel -- constants km = 1.0/9460730.4725808 sec = 1.0/86400 deg = math.pi / 180 -- Smoothstep function is 0 at x = 0, 1 at x = 1 -- derivative is zero at both x = 0 and x = 1 function smoothstep(x) return (3 - 2 * x) * x * x end -- Smoothstep2 is exactly like smoothstep, except that -- the derivative is 1 at x=1 function smoothstep2(x) return (2 - x) * x * x end -- Circularly orbit the observer frame center. The period of the -- orbit is given in seconds. function orbit_object(period) local obs = celestia:getobserver() local obsFrame = obs:getframe() local center = obsFrame:getrefobject() -- If there's no followed object, use the current selection if not center then center = celestia:getselection() end if not center then return end -- v1 is the vector from the viewer to the center -- up is the camera up direction -- v2 is normal to both v1 and up -- Orbit in the plane containing v1 and normal to the up direction local now = celestia:gettime() local v1 = obs:getposition() - center:getposition(now) local distance = v1:length() v1 = v1:normalize() local up = obs:getorientation():transform(celestia:newvector(0, 1, 0)) local v2 = v1 ^ up v2 = v2:normalize() start = celestia:getscripttime() while true do local t = (celestia:getscripttime() - start) / period if t < 1 then t = smoothstep2(t) end local theta = 2 * math.pi * t local v = math.cos(theta) * v1 + math.sin(theta) * v2 obs:setposition(center:getposition() + v * distance) obs:lookat(center:getposition(), up) wait(0) end end orbit_object(30)