SDK Question How do I find my Prograde vector in local coordinates?

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
So I'm trying to find my vessel's current orientation relative to it's prograde vector so that this data can be fed to an autopilot.

This is what I have so far...

Code:
//Vector Calculations for Guidance 
VECTOR3 rVel, rPos;

GetRelativePos(GetGravityRef(), rPos);
normalise(rPos);
GetRelativeVel(GetGravityRef(), rVel);
normalise(rVel); 

VECTOR3 horizon = crossp(rVel,rPos);
VECTOR3 NoseForwardVector = {0,0,1};
VECTOR3 RollVector = {1,0,0};
VECTOR3 ThrustVector = (GetThrusterDir(th_main));
	
MATRIX3 rotationMatrix;
GetRotationMatrix(rotationMatrix);

noseForwardVector = mul(rotationMatrix,NoseForwardVector);
double vPitch = asin(dotp(horizon,NoseForwardVector));

RollVector = mul(rotationMatrix,RollVector);
double vRoll = asin(dotp(horizon,RollVector));

// sprintf(oapiDebugString(),"Pitch %0.3f Roll %0.3f", vPitch*DEG, vRoll*DEG);

VECTOR3 input = (rVel);
input = mul(rotationMatrix,input);
normalise (input);
sprintf_s(oapiDebugString(), 255, "X, %0.3f Y %0.3f, Z %0.3f", input.x, input.y, input.z); // Debuggery

Now in theory when my vessel's nose is pointed prograde the currently active 'oapiDebugString()' should return "0.000, 0.000, 1.000".

It doesn't

Instead I get "-0.951, 0.000, 0.307". Now obviously X and Z axes got transposed somewhere but that doesn't explain why X wouldn't be +/- 1.000. What vector am I measuring if not prograde, and how do I find prograde?

ETA: The eventual purpose of this routine is to determine the offset between prograde and the vessel's thrust vector. Once I have the offset I can feed that data into the autopilot and have it align the two.
 
Last edited:

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
You're overthinking the problem :)

Consider that the prograde vector is the velocity vector of the vessel relative to the parent body. So, you're just looking to get that velocity vector in the local frame.

This code is excerpted from http://www.orbiter-forum.com/showthread.php?t=5072:

Code:
planet = ves->GetSurfaceRef();
ves->GetGlobalPos(glob_vpos);
ves->GetRelativeVel(planet, glob_rvel);
ves->Global2Local((glob_rvel + glob_vpos), loc_rvel);

You'd probably then want to normalize(loc_rvel) for passing to an autopilot.

The addition of the global position glob_vpos to the velocity vector is necessary because Global2Local converts positions, not velocities, so you need to translate the velocity vector by the vessel's position so it'll be relative to the ship's velocity.

Make sense?
 
Last edited:

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
The addition of the global position glob_vpos to the velocity vector is necessary because Global2Local converts positions, not velocities, so you need to translate the velocity vector by the vessel's position so it'll be relative the the ship's velocity.

This was the missing piece of the puzzle.

Thank you

:tiphat:


ETA:

Code:
//Vector Calculations for Guidance 
VECTOR3 rVel, rPos, gPos;
OBJHANDLE rBody = GetGravityRef();	// Get current reference body (the celestial body currently being orbited) 

GetGlobalPos(gPos);		// Get position of vessel in global frame.
GetRelativePos(rBody, rPos);		// Get position of vessel relative to reference body.
GetRelativeVel(rBody, rVel);		// Get velocity of vessel relative to reference body.
VECTOR3 horizon = crossp(rVel, rPos);	// Use cross-product of 'rVel' and 'rPos' to find horizon reference.

VECTOR3 vPrograde;
Global2Local((rVel + gPos), vPrograde); // Get prograde vector in local coordinates.

VECTOR3 vNormal	= {( vPrograde.x ), ( vPrograde.y*cos(RAD*-90) - vPrograde.z*sin(RAD*-90) ), ( vPrograde.y*sin(RAD*-90) + vPrograde.z*cos(RAD*-90) )}; // Rotate prograde vector by -90 degrees to find normal vector

VECTOR3 ForwardVector = {0,0,1};
VECTOR3 RollVector = {1,0,0};
VECTOR3 ThrustVector = (LEM::GetThrusterDir(th_main));

It works!

:cheers:
 
Last edited:
Top