Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---- Constants
- --- Common long function name shortcuts, etc. Saves typing and makes code more compact and easier to read in my opinion
- local SLPPF = ll.SetLinkPrimitiveParamsFast
- local Z_NORMAL = vector(0,0,1)
- --- Initial Placement and Scale Parameters.
- local links = { Sun = 2, Earth = 3, Moon = 4 } --Hard coded because this is a quick demo and there are only 3.
- local smallest_size, largest_size = 0.1, 3 -- lunar radius in the emulation, sun-earth distance in the emulation
- --- Emulation parameters: Given size, position, and time values will be adjusted to emulation sizes and times during initialization.
- -- 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
- -- table like I might if I were dealing with larger sets of similar objects.
- function tilt2vec( axis, tilt ) return ll.Rot2Up( ll.AxisAngle2Rot(axis, tilt) ); end --tilt2vec Convenience function for setting parameters below.
- local Sun, Earth, Moon
- local Sun = { radius = 695700, rot_period = 601.2 } --Km, hours
- local Earth = { radius = 6371, rot_period = 23.93, orbit = 149597871, orb_period = 8766.14, -- Km, hours, Km, hours,
- axial_tilt = tilt2vec(vector(1,0,0), 0.4091) -- direction vector (=dv)
- }
- local Moon = { radius = 1737, rot_period = 655.72, orbit = 384400, orb_period = 655.72, -- Km, hours, Km, hours,
- axial_tilt = tilt2vec(vector(-1,0,0), 0.1169), orbital_tilt = tilt2vec(vector(0,1,0), 0.0897) --dv, dv
- }
- --Reference Frame Adjustments.
- 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.
- --Additional parameters that depend on other parameters.
- Earth.base = { pos = vector(1,0,0), rot = ll.RotBetween(Z_NORMAL, Earth.axial_tilt) }
- Moon.base = { pos = vector(1,0,0), rot = ll.RotBetween(Z_NORMAL, Moon.axial_tilt) }
- --[[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
- visible effect of libation is small anyway, and this emulation has far bigger inaccuracies, such as neglecting orbital eccentricities, I'm using a right
- angle value as close enough for the purpose and getting on with my life.
- --]]
- ---Timing Constants
- local tick, time_scale = 0.1, 16 -- emulation clock rate, emulation hours per second.
- local time_step = time_scale*tick -- emulation hours per tick
- -----values collected for common power scaling.
- local sizes = { Sun.radius, Earth.radius, Earth.orbit, Moon.radius, Moon.orbit }
- ---- Script Scope Variables
- Sun.cur = {} -- Sun's current rotation in the emulation, pos is constant at ZERO_VECTOR
- Earth.cur = {} -- Earth's current postion and rotation
- Moon.cur = {} -- And Moon's
- -- 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.
- -- Think of it as being quasi-object oriented without going full-bore. Perhaps I can come up with a better top level script organization.
- local time = 0 -- Current Emulaton time.
- ----Functions.
- --- Fractional Power Scaler
- --[[
- 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,
- 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
- 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
- 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
- larger distances, but the difference between the smallest and largest is not so unmanagable.
- 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,
- Ratio will be set to Max/Min. It returns an array with the values scaled as required.
- 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
- this function needs and I'm already way beyond the original intent of this supposedly simple demo. Same with more error checks and warnings.
- Could add a smooshing factor too, something like a linear interpolation between this and a linear rescale, but not now. KISS.
- The error checking below may be excessive in this context.
- <<Yeah, I use comment blocks like this to keep myself from going overboard and to remind myself about possible future enhancements.>>
- --]]
- local function fractional_power_scale( data, args )
- local min, max, ratio = args.min, args.max, args.ratio
- assert( (min and max) or (min and ratio) or (max and ratio),
- "fractional_power_scale arguement error: at least two of min, max, and ratio must be provided.\n"..
- `Values recieved: min = {min}, max = {max}, ratio = {ratio}`
- )
- min = min or max/ratio; max = max or min*ratio
- if min > max then min, max = max, min; print("Warning: min and max found reversed: corrected"); end--if --Correctable error.
- ratio = max/min
- assert( ratio > 1 and min > 0 and max > 0 and min < max,
- "fractional_power_scale arguement error: min may not be equal to max, ratio may not be 1, all must be > 0.\n"..
- `Values computed from given arguments: min = {min}, max = {max}, ratio = {ratio}`
- )
- local data_min, data_max = math.huge, -math.huge -- find our working input range.
- for i, v in ipairs(data) do
- assert( v > 0, "fractional_power_scale data error: This version of the function only scales positive values.\n"..`Datum {i} = {v}` )
- if v < data_min then data_min = v end--if
- if v > data_max then data_max = v end--if
- end--for
- local power = math.log(ratio) / math.log( data_max/data_min )
- local result, scale = {}, min / data_min^power
- for i, v in ipairs(data) do result[i] = v^power * scale end--for
- return result
- end--fractional_power_scale
- ---Emulation Step
- local function emulation_step()
- time += time_step
- Earth.cur.pos = Earth.base.pos * ll.AxisAngle2Rot(Z_NORMAL, time/Earth.orb_period)
- Earth.cur.orb_rot = Earth.base.rot * ll.AxisAngle2Rot(Z_NORMAL, time/Earth.orb_period)
- SLPPF( links.Earth, { PRIM_POS_LOCAL,Earth.cur.pos } )
- 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 } )
- end--emulation_step
- ---- Initialization
- ---Adjust Constants to working values.
- local scaled_sizes = fractional_power_scale( sizes, { min = smallest_size, max = largest_size } )
- Sun.radius = scaled_sizes[1]
- Earth.radius = scaled_sizes[2]; Earth.orbit = scaled_sizes[3]; Earth.base.pos *= Earth.orbit
- Moon.radius = scaled_sizes[4]; Moon.orbit = scaled_sizes[5]; Moon.base.pos = Earth.base.pos + Moon.base.pos*Moon.orbit
- --Periods in radians instead of full circles per unit time.
- Sun.rot_period /= TWO_PI
- Earth.rot_period /= TWO_PI; Earth.orb_period /= TWO_PI;
- Moon.rot_period /= TWO_PI; Moon.orb_period /= TWO_PI;
- ---Set base positions and orientations and start rotations.
- SLPPF( links.Sun, { PRIM_SIZE, Sun.radius*vector(2,2,2), PRIM_OMEGA, Z_NORMAL, time_scale/Sun.rot_period, 1 } )
- SLPPF( links.Earth, { PRIM_SIZE, Earth.radius*vector(2,2,2), PRIM_POS_LOCAL, Earth.base.pos, PRIM_ROT_LOCAL, Earth.base.rot,
- PRIM_OMEGA, ll.Rot2Up(Earth.base.rot), time_scale/Earth.rot_period, 1 } )
- SLPPF( links.Moon, { PRIM_SIZE, Moon.radius*vector(2,2,2), PRIM_POS_LOCAL, Moon.base.pos, PRIM_ROT_LOCAL, Moon.base.rot,
- PRIM_OMEGA, ll.Rot2Up(Moon.base.rot), time_scale/Moon.rot_period, 1 } )
- ---Go
- LLTimers:every( tick, emulation_step )
Advertisement