Page 1 of 1

Adding a spacecraft dynamically in Lua script

Posted: 18.10.2018, 18:20
by NASA_SimGuy
I have a working Lua script but I want to add a new spacecraft to the scene. I want to add several spacecrafts and there states using the accessors similar to celestia:newframe("universal")

After adding the spacecraft to the scene, I want to update the states from inside the Lua script.

Is this possible? Are there any good examples of this spacecraft addition from inside the Lua script?

Posted: 19.10.2018, 11:31
by selden
Spacecraft are defined in SSC catalog files. All SSC definitions are loaded when Celestia first starts up. In other words, you can't define a spacecraft from scratch while Celestia is running.

However, you can define them with "Visible false" so they are invisible when they're loaded. You can change an object's Visible status using a Lua script while Celestia is running. 3D Meshes and their associated surface textures are not loaded until the first time they become visible, which causes Celestia to pause briefly (or not so briefly if they're large and complex).

You can write Lua routines which are used in CelX scripts or in SSC "ScriptedOrbit" and "ScriptedRotation" declarations to manipulate SSC objects while Celestia is running. See and

Some Addons which consist of SSC objects being controlled by CelX and ScriptedOrbit definitions are available at (interactive maps) (interactive notebook which includes spacecraft flying around it) (interactive model of the Hale telescope)

Not allowed to Navigate to Scripted Spacecraft

Posted: 19.10.2018, 19:10
by NASA_SimGuy
Thanks selden,

I created an SLS.ssc file defined below but I cannot Navigation|"Select Object" like I do for the ISS. I know the 3D model works because I substituted it in the ISS.scc and it showed up. I would have added the files if I knew how to attach a file to the forum. Thoughts?

# Space Launch System
"SLS vFS" "Sol/Earth"
Class "spacecraft"
Mesh "MPCV_EUS.cmod"
Radius 0.040

Module "MAVERIC"
Function "fetchSLSvFSpos"
Module "MAVERIC"
Function "fetchSLSvFSquat"
Orientation [ 90 0 0 1 ]

The following functions are in my MAV.celx file. The pos and quaternions are calculated in the MAV.celx and the setvisible is true.

function fetchSLSvFS(t)
-- Create a new table
local orbit = {};
-- Save the parameter list
orbit.params = t;

-- Set the required fields boundingRadius and position; note that position is actually a function
-- Compute the bounding radius from the amplitudes
orbit.boundingRadius = 1000.0

-- The position function will be called whenever Celestia needs the position of the object
function orbit:position(tjd)
local t = tjd - 2451545.0
local x = posx[vFS]
local y = posy[vFS]
local z = posz[vFS]
return x, y, z
return orbit

-- prototype for the wobble scripted rotation has default values for
-- any parameters that are omitted in the ssc file.
wobbleproto =
Amplitude = 0,
Period = 1,

-- constructor method
function wobbleproto:new(o)
o = o or {} -- create table if one not provided
setmetatable(o, self)
self.__index = self

-- set the period to whatever value was specified in the ssc file;
-- slightly confusing because Celestia is case sensitive--period must
-- be lowercase, but the field from the ssc file is capitalized.
o.period = o.Period

return o

-- The orientation function. This implementation produces a back and forth
-- wobble of Amplitude degrees about the z axis.
function wobbleproto:orientation_vFS(tjd)
local t = tjd - 2451545.0

-- fetch the global quaternion representing the orientation
return q4[vFS], q1[vFS], q2[vFS], q3[vFS]
function fetchSLSvFSquat(sscvals)
-- create a new wobble rotation object
return wobbleproto:new(sscvals)

Added after 1 hour 7 minutes:
I have an update after learning a few things. I learned that I can press ~ to get my errors on the screen. Too Cool.

I noticed that I have errors relating to my lua script not found. I placed the lua script in several files not knowing where to place it. I could not find the plcae in the documentation where it belongs. After disseminating it to several directories, I not get a segmentation fault right away as it was loading sls.scc (my new lua script).

My code in the previous post has not changed. I must have a problem with that code somewhere while loading.

Posted: 20.10.2018, 11:41
by selden
CelX scripts can be anywhere. They must not depend on any external lua files.

The lua files accessed by ScriptedOrbit and ScriptedRotation must be in the subdirectory named celxx which must be in the same directory as the .SSC catalog.

Errors in ScriptedOrbit and ScriptedRotation files usually crash Celestia instead of displaying error messages, so I usually debug their functions in CelX scripts first.

All of the code in a file defining a ScriptedOrbit (or ScriptedRotation) is executed when the ScriptedOrbit (or ScriptedOribit) is first encountered while Celestia is loading the SSC catalog. After that, only the function specified in that ScriptedOrbit (or ScriptedRotation) is called each time Celestia refreshes the screen.

When showing code in a post, it's best to surround it with the BBCode labels [ code] and [ /code] (without the spaces). It'll look like this:

Code: Select all

this is a line of code

One way to attach a file to a post is
1. select the button "Full Editor & Preview" (below the quick-edit pane)
2. select the button "Filename:" "Browse" (below the new editing pane)
3. find and select the desired zip file (on your local computer)
4. Optionally add a "File comment"
5. select the button "Filename:" "Add the file"
6. select the button "Preview" to see how your message looks, including the attachment
7. select the button "Submit" to post the message with the attachment

Posted: 20.10.2018, 12:18
by gironde
Warning :
I noticed while working on the module KeplerParamBox of Lut5 that if one compares the positions posx, posy, posz of Celx with those of a file .xyz one obtains:

posx = x position of the .xyz file
posy = z position of the .xyz file
posz = -y position of the .xyz file

I do not understand this inversion in the results but it can help you!


Adding a spacecraft dynamically in Lua script

Posted: 22.10.2018, 15:49
by NASA_SimGuy
I now have an SLS vehicle with a ScriptedPosition and Rotation :smile:

I need to use my ScriptedPosition Function "vFSPos" to set the SLS position. I can hard code the orbit:position(tjd)'s returned position to a float but I want to make calls like celestia:find("Earth") and use other lua calls to fill the position's values. When I execute Celestia, the '~' command shows that the ssc fails when I try to call celestia:find("Earth")

I think the lua script makes up a dll and I cannot make celestia:xxx calls. Any ideas? My goal is to eventually read a UDP socket to set the spacecraft position.

Posted: 22.10.2018, 16:13
by Croc
NASA_SimGuy, I don’t create scripts, but I’ve been developing the Lua Univercal Tools GUI. Both the scripts and Tools use the CELX Lua Methods.

Here is one example of a module for searching the Earth and moving to the Earth:

Code: Select all

panel143Button.Action = (function()
            local obs = celestia:getobserver()
            local obj = celestia:find("Earth")
    end) ()

I recommend to look at the files of the graphical interface.
I think there you will find the answers to your questions.

Scripted Positions/Quaternions

Posted: 26.10.2018, 14:04
by NASA_SimGuy
I want to learn about Celestia frames. Please don't just point me to the Wiki. Gironde wrote that I will have to swap y and z coordinates. Why is this? What coordinate system has huge z values w.r.t. x and y? I thought universal, ecliptic and universal were all the same.

I see where I can get the observer frame but my Lua script crashes when I try to get my scriptedObject frame. Why is this? I could learn more if I can get my scriptedObject frame . Then I could get myframe:getcoordinatesystem().

I have ECEF coordinate and I am trying to get them to show up properly in Celestia.

Posted: 26.10.2018, 17:21
by selden
I haven't used frames much; about all I know for certain is that the coordinate system used internally by Celestia (the one used within ScriptedOrbit Lua routines) is rotated by 90 degrees relative to what you'd expect.

As I mentioned previously, it's usually better to debug Lua functions in CelX scripts rather than trying to debug them in ScriptedOrbits. When malformed Lua functions are used in CelX scripts, they tend to provide error messages (either in popup windows or in the console log) rather than crashing Celestia.

Posted: 27.10.2018, 16:05
by onetwothree
NASA_SimGuy wrote:Gironde wrote that I will have to swap y and z coordinates. Why is this?

I suppose this is because Celestia is OpenGL application so it uses right-handed OpenGL's coordinate system.

Compare it with standard Cartesian coordinate system.

static Vector3d toStandardCoords(const Vector3d& v)
    return Vector3d(v.x(), -v.z(), v.y());

Posted: 29.10.2018, 11:53
by NASA_SimGuy
The y and Z might be swapped because Celestia uses a "Wandering frame" to reduce the changes of singularities when propagating.