API Question Advice for Applying Orientation to Spawned Vessel

Mr Martian

Orbinaut/Addon dev/Donator
Addon Developer
Donator
Joined
Jun 6, 2012
Messages
288
Reaction score
67
Points
28
Location
Sydney, Australia, Earth, Sol
Website
www.orbithangar.com
Hi All,

I am creating a vessel which will jettison a component. This is simple enough, however I would like the spawned vessel to be rotated 90 degrees relative to the parent vessel. This is something I have always struggled to achieve, my guess is that I am improperly converting between relative/local/global.

If someone knows how to do this any advice would be appreciated. For reference, I have this in my code to create a vessel:

C++:
void Ship::Jettison (char* classname, char* ext, VECTOR3 ofs, VECTOR3 dir)
{
    VESSELSTATUS vs;
    char name[255];

    VECTOR3 rofs;
    double vel = 0.2;

    GetStatus(vs);
    Local2Rel (ofs, vs.rpos);
    GlobalRot (dir, rofs);
    vs.rvel += rofs*vel;

    strcpy (name, GetName()); strcat (name, ext);
    oapiCreateVessel (name, classname, vs);
}

I tried adding the following, but found that the created vessel was just rotated randomly. I have played around with all of the coordinate transformations in the VesselAPI to no avail:

C++:
    VECTOR3 rot = {0, 1, 0};
    VECTOR3 rrot;
    GlobalRot (rot, rrot);
    vs.arot = rot;

Has anyone managed to achieve what I am trying to? I would love to know if anyone knows how to do this, it's driving me crazy :ROFLMAO:

Thank you!
 

BrianJ

Addon Developer
Addon Developer
Joined
Apr 19, 2008
Messages
1,676
Reaction score
898
Points
128
Location
Code 347
Hi,
the only way I know how to do this is to calculate the spawned vessel X,Y,Z axes unit vectors in Global frame, then use them to calculate the Euler angles for the Status arot parameter.

If you know the spawned vessel axes vectors in parent vessel frame, you can use Orbiter's GlobalRot(...) to make life a bit easier.
The following code is to set the spawned vessel at 90deg "pitch up" relative to parent vessel:

Code:
// for spawned vessel axes vectors, global frame
    VECTOR3 xright;
    VECTOR3 yup;
    VECTOR3 zforward;
   
// spawned vessel axes vectors, parent vessel frame
    VECTOR3 xright_pv = _V(1,0,0); // same as parent
    VECTOR3 zforward_pv = _V(0,1,0); // pitched up 90, z axis coincides with parent y axis

// convert to Global frame
    GlobalRot (xright_pv, xright);
    GlobalRot (zforward_pv, zforward);
   
    yup = crossp(zforward, xright); // get y axis by taking cross product
   normalise(yup);
    
// global vector to Euler angle conversion
    double gamma = atan2( xright.x, yup.x);
    double beta = -1*(asin(zforward.x));
    double alpha = atan2(zforward.y, zforward.z);


  VESSELSTATUS vs;
  GetStatus(vs);
  vs.arot = _V(alpha, beta, (PI/2)-gamma); // set status arot
...........etc etc......................

Hope this helps,
BrianJ
P.S. I had to modify the code a little for posting here, so I hope I didn't make any typos etc.!
 

Mr Martian

Orbinaut/Addon dev/Donator
Addon Developer
Donator
Joined
Jun 6, 2012
Messages
288
Reaction score
67
Points
28
Location
Sydney, Australia, Earth, Sol
Website
www.orbithangar.com
Hi,
the only way I know how to do this is to calculate the spawned vessel X,Y,Z axes unit vectors in Global frame, then use them to calculate the Euler angles for the Status arot parameter.

If you know the spawned vessel axes vectors in parent vessel frame, you can use Orbiter's GlobalRot(...) to make life a bit easier.
The following code is to set the spawned vessel at 90deg "pitch up" relative to parent vessel:

Hope this helps,
BrianJ
P.S. I had to modify the code a little for posting here, so I hope I didn't make any typos etc.!
BrianJ that helps a lot that is exactly what I was trying to do! I can't thank you enough, this is something I could never get my head around!
 
Top