A time acceleration function

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Toti
Developer
Posts: 338
Joined: 10.02.2004
With us: 20 years 9 months

A time acceleration function

Post #1by Toti » 02.05.2004, 01:46

Hello,
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

   repeat
      wait()   
      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
end





--****************************************************
--      use the function
--****************************************************

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

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


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





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

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

celestia:setrenderflags{constellations=false,eclipseshadows=true,galaxies=true,grid=false,lightdelay=false,markers=false,
         nightmaps=true,orbits=true,planets=true,ringshadows=true,smoothlines=false,stars=true}
celestia:setlabelflags{asteroids=false,spacecraft=false,comets=false,constellations=false,locations=false,moons=false,
         planets=true,star=false}
celestia:setorbitflags{Planet=true,Moon=false,Comet=false,Asteroid=false,Spacecraft=false}
   
celestia:flash ("...let's watch ten Earth years in twenty seconds...", DELAY)
wait(DELAY)
--**************************
--Here is the function call
--**************************
accTime (365*10, 20)





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

-- restore user's defaults
celestia:setrenderflags(oldrf)
celestia:setlabelflags(oldlf)
celestia:setorbitflags(oldof)



Please post suggestions and bugs here.

Bye :)

don
Posts: 1709
Joined: 12.07.2003
With us: 21 years 4 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”