SungAli

Sun Earth Moon emulator problem

May 16th, 2026
141
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.12 KB | Software | 0 0
  1. ---- Constants
  2. --- Common long function name shortcuts, etc.  Saves typing and makes code more compact and easier to read in my opinion
  3. local SLPPF = ll.SetLinkPrimitiveParamsFast
  4. local Z_NORMAL = vector(0,0,1)
  5.  
  6. --- Initial Placement and Scale Parameters.
  7. local links = { Sun = 2, Earth = 3, Moon = 4 } --Hard coded because this is a quick demo and there are only 3.
  8. local smallest_size, largest_size = 0.1, 3 -- lunar radius in the emulation, sun-earth distance in the emulation
  9.  
  10. --- Emulation parameters: Given size, position, and time values will be adjusted to emulation sizes and times during initialization.
  11. -- Since there are only three objects and each receives different treatment, there doesn't seem to be any point in collecting parameters into a common
  12. -- table like I might if I were dealing with larger sets of similar objects.
  13. function tilt2vec( axis, tilt ) return ll.Rot2Up( ll.AxisAngle2Rot(axis, tilt) ); end --tilt2vec Convenience function for setting parameters below.
  14. local Sun, Earth, Moon
  15. local Sun =   { radius = 695700, rot_period = 601.2 }                                                           --Km, hours
  16. local Earth = { radius =   6371, rot_period =  23.93, orbit = 149597871, orb_period = 8766.14,                  -- Km, hours, Km, hours,
  17.                 axial_tilt = tilt2vec(vector(1,0,0), 0.4091)                                                    -- direction vector (=dv)
  18.               }
  19. local Moon =  { radius =   1737, rot_period = 655.72, orbit =    384400, orb_period =  655.72,                  -- Km, hours, Km, hours,
  20.                 axial_tilt = tilt2vec(vector(-1,0,0), 0.1169), orbital_tilt = tilt2vec(vector(0,1,0), 0.0897) --dv, dv
  21.               }
  22. --Reference Frame Adjustments.
  23. Moon.axial_tilt *= ll.RotBetween(Z_NORMAL, Moon.orbital_tilt) --Moon tilt as given is relative to its orbital plane, convert to object reference frame.
  24. --Additional parameters that depend on other parameters.
  25. Earth.base =  { pos = vector(1,0,0), rot = ll.RotBetween(Z_NORMAL, Earth.axial_tilt) }
  26. Moon.base =   { pos = vector(1,0,0), rot = ll.RotBetween(Z_NORMAL, Moon.axial_tilt) }
  27. --[[Note: I haven't actually determined the relation of Moon's axial tilt relative to its orbit compared to its orbit's tilt relative to Earth's.  Since the
  28.     visible effect of libation is small anyway, and this emulation has far bigger inaccuracies, such as neglecting orbital eccentricities, I'm using a right
  29.     angle value as close enough for the purpose and getting on with my life.
  30. --]]
  31.  
  32. ---Timing Constants
  33. local tick, time_scale = 0.1, 16 -- emulation clock rate, emulation hours per second.
  34. local time_step = time_scale*tick -- emulation hours per tick
  35.  
  36. -----values collected for common power scaling.
  37. local sizes = { Sun.radius, Earth.radius, Earth.orbit, Moon.radius, Moon.orbit }
  38.  
  39. ---- Script Scope Variables
  40. Sun.cur =   {} -- Sun's current rotation in the emulation, pos is constant at ZERO_VECTOR
  41. Earth.cur = {} -- Earth's current postion and rotation
  42. Moon.cur =  {} -- And Moon's
  43. -- Note: It's a little weird, building the tables piecemeal this way, but there's no good reason to make separate tables of constants and variables.
  44. -- Think of it as being quasi-object oriented without going full-bore.  Perhaps I can come up with a better top level script organization.
  45.  
  46. local time = 0 -- Current Emulaton time.
  47.  
  48. ----Functions.
  49. --- Fractional Power Scaler
  50. --[[
  51. A common problem with emulating physical systems is the difference in sizes of the things emulated.  In this case the smallest size is the radius of Moon,
  52. a mere 1737 Km, compared to the radius of Earth's orbit, 149597871 Km, a ratio of over 86124 to 1.  This means if we did things to scale and made  Moon
  53.  the smallest we could make it, then our emulator would need a 4x4 square of sims to fit.  My solution to this is to raise all distances/sizes to a suitable
  54.  power between 0 and 1 (It works with powers greater than one too when you want to exaggerate differences), so that smaller distances are still smaller than
  55.  larger distances, but the difference between the smallest and largest is not so unmanagable.
  56.  The scaler takes an array of values to scale, and a table with two of three possible named arguments, Min, Max, and Ratio.  If all three are supplied,
  57.  Ratio will be set to Max/Min.  It returns an array with the values scaled as required.
  58.  For now limit everything to positive values.  It's possible scale negative values accordingly, and return 0 as 0, but that's beyond the scope of what
  59.  this function needs and I'm already way beyond the original intent of this supposedly simple demo. Same with more error checks and warnings.
  60.  Could add a smooshing factor too, something like a linear interpolation between this and a linear rescale, but not now. KISS.
  61.  The error checking below may be excessive in this context.
  62.  <<Yeah, I use comment blocks like this to keep myself from going overboard and to remind myself about possible future enhancements.>>
  63. --]]
  64.  
  65. local function fractional_power_scale( data, args )
  66.     local min, max, ratio = args.min, args.max, args.ratio
  67.     assert( (min and max) or (min and ratio) or (max and ratio),
  68.         "fractional_power_scale arguement error: at least two of min, max, and ratio must be provided.\n"..
  69.         `Values recieved: min = {min}, max = {max}, ratio = {ratio}`
  70.     )
  71.     min = min or max/ratio; max = max or min*ratio
  72.     if min > max then min, max = max, min; print("Warning: min and max found reversed: corrected"); end--if --Correctable error.
  73.     ratio = max/min
  74.     assert( ratio > 1 and min > 0 and max > 0 and min < max,
  75.         "fractional_power_scale arguement error: min may not be equal to max, ratio may not be 1, all must be > 0.\n"..
  76.         `Values computed from given arguments: min = {min}, max = {max}, ratio = {ratio}`
  77.     )
  78.     local data_min, data_max = math.huge, -math.huge -- find our working input range.
  79.     for i, v in ipairs(data) do
  80.         assert( v > 0, "fractional_power_scale data error: This version of the function only scales positive values.\n"..`Datum {i} = {v}` )
  81.         if v < data_min then data_min = v end--if
  82.         if v > data_max then data_max = v end--if
  83.     end--for
  84.     local power = math.log(ratio) / math.log( data_max/data_min )
  85.     local result, scale = {}, min / data_min^power
  86.     for i, v in ipairs(data) do result[i] = v^power * scale end--for
  87.     return result
  88. end--fractional_power_scale
  89.  
  90. ---Emulation Step
  91. local function emulation_step()
  92.     time += time_step
  93.     Earth.cur.pos =  Earth.base.pos * ll.AxisAngle2Rot(Z_NORMAL, time/Earth.orb_period)
  94.     Earth.cur.orb_rot = Earth.base.rot * ll.AxisAngle2Rot(Z_NORMAL, time/Earth.orb_period)
  95.     SLPPF( links.Earth, { PRIM_POS_LOCAL,Earth.cur.pos } )
  96.     SLPPF( links.Moon,  { PRIM_POS_LOCAL, Earth.cur.pos + (Moon.base.pos-Earth.base.pos) * ll.AxisAngle2Rot(Moon.orbital_tilt, time/Moon.orb_period) * Earth.cur.orb_rot } )
  97. end--emulation_step
  98.  
  99. ---- Initialization
  100. ---Adjust Constants to working values.
  101. local scaled_sizes = fractional_power_scale( sizes, { min = smallest_size, max = largest_size } )
  102. Sun.radius      = scaled_sizes[1]
  103. Earth.radius    = scaled_sizes[2]; Earth.orbit  = scaled_sizes[3]; Earth.base.pos *= Earth.orbit
  104. Moon.radius     = scaled_sizes[4]; Moon.orbit   = scaled_sizes[5]; Moon.base.pos = Earth.base.pos + Moon.base.pos*Moon.orbit
  105. --Periods in radians instead of full circles per unit time.
  106. Sun.rot_period      /= TWO_PI
  107. Earth.rot_period    /= TWO_PI; Earth.orb_period /= TWO_PI;
  108. Moon.rot_period     /= TWO_PI; Moon.orb_period  /= TWO_PI;
  109.  
  110. ---Set base positions and orientations and start rotations.
  111. SLPPF( links.Sun,   { PRIM_SIZE,   Sun.radius*vector(2,2,2), PRIM_OMEGA, Z_NORMAL, time_scale/Sun.rot_period, 1 } )
  112. SLPPF( links.Earth, { PRIM_SIZE, Earth.radius*vector(2,2,2), PRIM_POS_LOCAL, Earth.base.pos, PRIM_ROT_LOCAL, Earth.base.rot,
  113.                       PRIM_OMEGA, ll.Rot2Up(Earth.base.rot), time_scale/Earth.rot_period, 1 } )
  114. SLPPF( links.Moon,  { PRIM_SIZE,  Moon.radius*vector(2,2,2), PRIM_POS_LOCAL,  Moon.base.pos, PRIM_ROT_LOCAL,  Moon.base.rot,
  115.                       PRIM_OMEGA, ll.Rot2Up(Moon.base.rot), time_scale/Moon.rot_period, 1 } )
  116.  
  117.  
  118. ---Go
  119. LLTimers:every( tick, emulation_step )
  120.  
  121.  
Advertisement
Comments
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment