A time acceleration function

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Posts: 338
Joined: 10.02.2004
With us: 14 years 1 month

A time acceleration function

Post #1by Toti » 02.05.2004, 01:46

Let's suppose that in your script you need to compress X days into Y seconds. Then you convert these X days to seconds and divide this by Y seconds, obtaining a time factor F, that you can use to increase time speed.
But this "acceleration" is really a jump from 1 to F in zero time, and produces a not so pleasant effect.
The function below, instead, applies a smooth acceleration to time, assuring a much better result (that may be very suitable for certain visual purposes)
The script also includes two use examples.

Code: Select all

--              accTime()
--   A function to calculate smooth time acceleration
--            coded by Toti

--   constants (only used by the main program)
DELAY   = 5

--      the accTime() function
--  parameters:
--      DT: real time interval in days
--       dt: simulation time interval in seconds
--          (where DT will be compressed into)
function accTime (DT, dt)
   local st0      = celestia:getscripttime()
   local pi      = math.pi

   local k      = ((DT/dt * 43200) - 0.5) * pi    -- 43200 * 2 = seconds in a day

   local period   = pi / dt
   local dst      = 0         -- without this, 'repeat-until' does not work

      dst      = celestia:getscripttime() - st0
      local ts   = k * math.sin (dst * period) + 1
      celestia:settimescale (ts)

      -- you can comment this line:
      celestia:flash (string.format("Seconds: %3.0f" , dst), DELAY)   
   until dst >= dt 

   celestia:settimescale (1)   -- correct floating point error

--      use the function

upVector   = celestia:newvector(0,1,0)
obs      = celestia:getobserver()

earth      = celestia:find("Sol/Earth")
obs:gotolonglat(earth, 0, 0, 20000, DELAY)

celestia:flash ("Let's show an entire Earth day in ten seconds...", DELAY)
--Here is the function call
accTime (1, 10)
celestia:flash ("And now...", DELAY)

sol      = celestia:find("Sol")
obs:gotolonglat(sol, 0, 1.5707, 4*1e8, DELAY)

-- be sure to show planet's orbits and flags
oldrf   = celestia:getrenderflags()
oldlf   = celestia:getlabelflags()
oldof   = celestia:getorbitflags()

celestia:flash ("...let's watch ten Earth years in twenty seconds...", DELAY)
--Here is the function call
accTime (365*10, 20)

celestia:flash ("That was all!", DELAY)

-- restore user's defaults

Please post suggestions and bugs here.

Bye :)

Posts: 1709
Joined: 12.07.2003
With us: 14 years 8 months
Location: Colorado, USA (7000 ft)

Post #2by don » 03.05.2004, 07:38

Hi Toti,

This is a great function, to be able to specify the actual and accelerated time values, instead of merely guessing.

Thank you Toti! :D
-Don G.
My Celestia Scripting Resources page

Avatar: Total Lunar Eclipse from our back yard, Oct 2004. Panasonic FZ1 digital camera (no telescope), 36X digital zoom, 8 second exposure at f6.5.

Return to “Scripting”

Who is online