Question ShiftCG or ShiftCentreofMass in Lua?

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
Is there a ShiftCG or ShiftCentreofMass capability in Lua? I saw a link to Shift_CentreofMass in the Help files but it doesn't link to anything.

EDIT: Below is an image that shows where these are found in the Orbiter Help dialogue, under Orbiter Scripting / Class Methods / Vessel Methods. The hypertext links don't connect to anything and so I don't know if these are actually implemented, and there is nothing saying how to use them. I tried:
Code:
vi:shift_centreofmass({1,2,3})
and
Code:
vi:shift_cg({1,2,3})
but in either case the script crashes Orbiter, and I can't think of how else these would be implemented.

Screenshot at 2023-11-08 20-00-34.png
 
Last edited:

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
try this:
vi:shiftCG({x=1,y=2,z=3})

You'll need this DLL: https://www.orbiter-forum.com/threads/orbiter-beta-lua-development.35459/
I see, I've been struggling with the limited 2016 version of that DLL that came with the Orbiter 2016 download. This is a great help! Thanks!

Now I have to figure out how to get an aircraft to roll about its longitudinal axis. The origin of the mesh isn't on that axis, so it rolls rather strangely with an offset. I am updating an old add-on from another developer and really don't want to edit the original mesh, and would like all the shifts to be done in my Lua code. It seems that I can move the CG, but the pitch and roll axes are still through the origin of the mesh.
 
Last edited:

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
ShiftCG or ShiftCentreofMass
Make sure you understand the difference between ShiftCG and ShiftCentreOfMass. I don't remember from the top of my head what it was, but I remember it being significant. One only moves the mesh, while the other one also moves thrusters and camera positions and stuff. Though I wouldn't expect that to result in the roll axes still going through the origin of the mesh, that's kind of what the center of gravity is all about. But it might not hurt checking in on that.
 

n72.75

Move slow and try not to break too much.
Orbiter Contributor
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
2,696
Reaction score
1,353
Points
128
Location
Saco, ME
Website
mwhume.space
Preferred Pronouns
he/him
Make sure you understand the difference between ShiftCG and ShiftCentreOfMass. I don't remember from the top of my head what it was, but I remember it being significant. One only moves the mesh, while the other one also moves thrusters and camera positions and stuff. Though I wouldn't expect that to result in the roll axes still going through the origin of the mesh, that's kind of what the center of gravity is all about. But it might not hurt checking in on that.
ShiftCG is the one that moves thrusters, clickspots etc. The API reference has good details on this.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
ShiftCG is the one that moves thrusters, clickspots etc. The API reference has good details on this.
That was my understanding as well.

What I am trying to do is set the origin of the local coordinate system to the location of the CG on the mesh so it can roll about its axis. Right now the origin is too high and so it rolls very strangely. I want it to roll about the centerline of the fuselage in line with the nose.

Screenshot at 2023-11-09 09-35-47.png
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
You might have to shift the mesh, then, if you don't want to edit it... I think there's a function for that, but I'm not sure if it's exposed to LUA. You'll also have to adjust thruster positions, animation reference points etc.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
So what I have done is to run shipedit to get the location of the center of mass in the vessel local coordinates and put that into Lua in the following table:

Code:
cm = {x=0, y=-0.65, z=-1.71} --center of mass determined from shipedit

In clbk_setclasscaps I have the following code to load the mesh and to shift it

Code:
vi:add_mesh('K33/K-Mirage2000')
vi:shift_mesh(0,{-cm.x, -cm.y, -cm.z})

The script runs and the mesh loads, but the location of the origin (indicated by the visual helpers) doesn't seem to be changing. There is only the one mesh so it should be index 0.
 
Last edited:

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
For whatever reason this didn't work:

Code:
vi:shift_mesh(0,{-cm.x, -cm.y, -cm.z})

but this did?

Code:
vi:shift_mesh(0,vec.mul(-1,cm))

Screenshot at 2023-11-10 15-29-59.png
Had to update the touchdown points, but otherwise everything worked out. All the animations still work and rotate around the appropriate axes.

Onward!
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
For whatever reason this didn't work:

Code:
vi:shift_mesh(0,{-cm.x, -cm.y, -cm.z})
but this did?

Code:
vi:shift_mesh(0,vec.mul(-1,cm))
Now you made me curious enough to have a look into the API docs. The C++ implementation that lua is binding to expects a reference to a vector3. I imagine that this jives badly with the inline declaration in LUA for whatever reason...
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
Now you made me curious enough to have a look into the API docs. The C++ implementation that lua is binding to expects a reference to a vector3. I imagine that this jives badly with the inline declaration in LUA for whatever reason...
But it's OK with vec.mul(-1,cm)? That doesn't produce a reference.
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,877
Reaction score
2,131
Points
203
Location
between the planets
But it's OK with vec.mul(-1,cm)? That doesn't produce a reference.
It really depends on how the LUA interpreter does stuff, which I know nothing about. It does seem odd, but not unimaginable. Could also be that I'm completely wrong.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,286
Reaction score
3,255
Points
203
Location
Toulouse
For whatever reason this didn't work:

Code:
vi:shift_mesh(0,{-cm.x, -cm.y, -cm.z})

but this did?

Code:
vi:shift_mesh(0,vec.mul(-1,cm))

View attachment 35638
Had to update the touchdown points, but otherwise everything worked out. All the animations still work and rotate around the appropriate axes.

Onward!

Be sure to note that information in the LUA thread, good find !
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
Be sure to note that information in the LUA thread, good find !
Which one? There are a couple of things that I ran across that should be documented somewhere. Lua does seem rather usable, but the documentation needs to be brought up to the level of the Orbiter API reference somehow. That may be challenging with the various Orbiter forks that are popping up. I'm still on Orbiter 2016 which may not be what the cool kids are using these days, or in the future.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,286
Reaction score
3,255
Points
203
Location
Toulouse
Which one? There are a couple of things that I ran across that should be documented somewhere. Lua does seem rather usable, but the documentation needs to be brought up to the level of the Orbiter API reference somehow. That may be challenging with the various Orbiter forks that are popping up. I'm still on Orbiter 2016 which may not be what the cool kids are using these days, or in the future.

I'm still on 2016 too. What are the 'cool kids' using today, and what are the improvements done ? I must say I'm a bit lost.
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
I'm still on 2016 too. What are the 'cool kids' using today, and what are the improvements done ? I must say I'm a bit lost.
I believe the good Dr. Schweiger made Orbiter open source, and there are a couple of forks that I am aware of, but I haven't done much investigating. I don't know what the "official" version is anymore.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,286
Reaction score
3,255
Points
203
Location
Toulouse
I believe the good Dr. Schweiger made Orbiter open source, and there are a couple of forks that I am aware of, but I haven't done much investigating. I don't know what the "official" version is anymore.

Same here. And probably the same for 99% of users...
 

Gondos

Well-known member
Joined
Apr 18, 2022
Messages
233
Reaction score
270
Points
78
Location
On my chair
Not sure if you're still banging your head on this one but your issue is with the vector definition :
Code:
vi:shift_mesh(0,{-cm.x, -cm.y, -cm.z})
A vector is a table with x,y and z attributes, but here you define an array of 3 elements
You should do this instead :
Code:
vi:shift_mesh(0,{x=-cm.x, y=-cm.y, z=-cm.z})
 

Thunder Chicken

Fine Threads since 2008
Donator
Joined
Mar 22, 2008
Messages
4,367
Reaction score
3,302
Points
138
Location
Massachusetts
Not sure if you're still banging your head on this one but your issue is with the vector definition :
Code:
vi:shift_mesh(0,{-cm.x, -cm.y, -cm.z})
A vector is a table with x,y and z attributes, but here you define an array of 3 elements
You should do this instead :
Code:
vi:shift_mesh(0,{x=-cm.x, y=-cm.y, z=-cm.z})
Thanks for checking back, but I did finally figure out the Orbiter vector definition as a keyed Lua table.
 
Top