API Question GetRelativePos function not returning correct value

Kamran

New member
Joined
May 29, 2013
Messages
22
Reaction score
0
Points
0
If I take the XR5_Vanguard and another vessel and write the output of their relative locations into the oapiDebugString, like so...

VESSEL* vanguard,v2;
vanguard=oapiGetVesselByName("XR5Vanguard");
v2=oapiGetVesselByName("Vessel2");

VECTOR3 relativePos;
vanguard->getRelativePos(v2->GetHandle(),relativePos);

sprintf(oapiDebugString(),"x=%.2 y=%.2 z=%.2",relativePos.x, relativePos.y, relativePos.z);

...Run orbiter and set my XR5Vanguard to v2's position in the scenario editor.
The relative position as printed on the oapiDebugString is (x=4.20 y=4.07 z=-1.97). Now granted that my v2 vessel's origin on the y axis is 0 (the ground), this should only affect the y axis value on the output, not the other two axis. Do I need to transform my vector using a matrix, and if so, what function should I use to do this? Any help is greatly appreciated. :hailprobe:
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,403
Reaction score
581
Points
153
Location
Vienna
If I take the XR5_Vanguard and another vessel and write the output of their relative locations into the oapiDebugString, like so...

VESSEL* vanguard,v2;
vanguard=oapiGetVesselByName("XR5Vanguard");
v2=oapiGetVesselByName("Vessel2");

VECTOR3 relativePos;
vanguard->getRelativePos(v2->GetHandle(),relativePos);

sprintf(oapiDebugString(),"x=%.2 y=%.2 z=%.2",relativePos.x, relativePos.y, relativePos.z);

...Run orbiter and set my XR5Vanguard to v2's position in the scenario editor.
The relative position as printed on the oapiDebugString is (x=4.20 y=4.07 z=-1.97). Now granted that my v2 vessel's origin on the y axis is 0 (the ground), this should only affect the y axis value on the output, not the other two axis. Do I need to transform my vector using a matrix, and if so, what function should I use to do this? Any help is greatly appreciated. :hailprobe:

First of all, this is not working the way you depicted it, because oapiGetVesselByName(1) returns an OBJHANDLE, not a VESSEL pointer. Second, there is no VESSEL::getRelativePos(2) function in the API, so I guess you mean VESSEL::GetRelativePos(2).

You have to be aware that the later function returns results in the global frame, so you'll have to rotate it with the vessel's rotation matrix to get vessel frame coordinates of the relative distance vector.

However, there is a much more convenient function for this: VESSEL::Global2Local(global, local). Take a look at /Orbitersdk/doc/API_Reference.pdf , page 455.
 
Last edited:

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
Also, shouldn't it be "%.2f" in the sprintf instead of "%.2"? I thought the "f" part was kind of important...
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,403
Reaction score
581
Points
153
Location
Vienna
Also, shouldn't it be "%.2f" in the sprintf instead of "%.2"? I thought the "f" part was kind of important...

Indeed. The format string "x=%.2 y=%.2 z=%.2" just prints "x= y= z=" for me, i.e. the incomplete format specifier gets ignored.
 

Kamran

New member
Joined
May 29, 2013
Messages
22
Reaction score
0
Points
0
Thanks Face,

You're right, I meant to use oapiGetVesselInterface(...) and my capitalization isn't as good as it should be. I managed to the position values I wanted by using a combination of GetGlobalPos and Global2Local functions and subtracting one vessel's vectors from the other.

Thanks for your super quick response! :cheers:
 

Kamran

New member
Joined
May 29, 2013
Messages
22
Reaction score
0
Points
0
I've got many many variables floating around from using my workaround and so I tried your suggestion and used...
v2->GetRelativePos(v1->GetHandle(),relativePos);
Global2Local(relativePos,relativePos);
...this didn't work, but gave me huge numbers that were counting down on the x and up on y and z. So I tried...
MATRIX3 m;
v2->GetRotationMatrix(m);
relativePos=mul(m,relativePos);
... I know I've overwritten relativePos a lot but I've also tried it with unique variables and it doesn't make a difference.

Am I naive in thinking that GetRelativePos should Get Relative Position? :hmm:
Thanks for your help :thumbup:
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,403
Reaction score
581
Points
153
Location
Vienna
I've got many many variables floating around from using my workaround and so I tried your suggestion and used...
v2->GetRelativePos(v1->GetHandle(),relativePos);
Global2Local(relativePos,relativePos);
...this didn't work, but gave me huge numbers that were counting down on the x and up on y and z. So I tried...

Did you read the PDF I referenced? There is stated that Global2Local(2) will do all the work for you already. You just have to plug in the global position of the other vessel as first argument, and will get the relative position in the type 3 coordinate system of the source vessel in the second argument.

MATRIX3 m;
v2->GetRotationMatrix(m);
relativePos=mul(m,relativePos);
... I know I've overwritten relativePos a lot but I've also tried it with unique variables and it doesn't make a difference.

Am I naive in thinking that GetRelativePos should Get Relative Position? :hmm:

No, you are not naive. I think you just don't understand what I mean with frames. I've once tried to explain that a bit in-depth here. Please take your time to read it thoroughly.
 

Kamran

New member
Joined
May 29, 2013
Messages
22
Reaction score
0
Points
0
I understand that I need to convert global coordinates to local ones. I don't know how this works, but do know how to fill in parameters to a function. I've read the api documentation and have tried to understand your explaination you gave to Evgheny. However, the api documentation says that to convert global coordinates to local coordinates you just need to plug in the global parameters to the Global2Local function. I believe that this works as I have used it to convert global parameters to local ones in my workaround. What I don't understand is why the GetRelativePos function doesn't return the same as vector2-vector1 where vector1 and vector2 can be found using...
vessel1->GetGlobalPos(vector1);
vessel2->GetGlobalPos(vector2);
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,403
Reaction score
581
Points
153
Location
Vienna
What I don't understand is why the GetRelativePos function doesn't return the same as vector2-vector1 where vector1 and vector2 can be found using...
vessel1->GetGlobalPos(vector1);
vessel2->GetGlobalPos(vector2);

So re-read the documentation in /Orbitersdk/doc/API_Reference.pdf, page 369. It clearly states that:
Documentation said:
This function returns the vessel’s position relative to the position of the object defined by handle hRef.

So if you do vessel1->GetRelativePos(vessel2->GetHandle(), vector1), vector1 will be the inverse vector you get with your vector2-vector1 example above. If you do vessel2->GetRelativePos(vessel1->GetHandle(), vector1), it should be just the same.
 

Kamran

New member
Joined
May 29, 2013
Messages
22
Reaction score
0
Points
0
Thanks Face, I understand this. But in practise it doesn't return the same values. Would you like me to send you a screenshot of my code and output results?
 
Last edited:

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,403
Reaction score
581
Points
153
Location
Vienna
Thanks Face, I understand this. But in practise it doesn't return the same values. Would you like me to send you a screenshot of my code and what I mean?

No. I tried it for myself when I posted my comments, and it worked perfectly. GetRelativePos(2) works exactly as postulated in the docs, and the test with GetGlobalPos(1) vector subtraction yields the exact same results for me. I don't need a "proof" of what you see, as I believe you that you see it. It is just no problem of the API in general, it must be some small mistake in your code and/or a specific behavior of your setup (XR5, other addons, etc.).

Therefore, it would be better if you post your complete code for review, as we already established that your snippets contained many mistakes that contradicted your reports, so only based on them I can't really help you.

Two questions, though:

  1. What Orbiter version do you test in?
  2. Are you working in a vanilla environment (except for needed add-ons)?
 

Kamran

New member
Joined
May 29, 2013
Messages
22
Reaction score
0
Points
0
Thanks Face,
I'm using Orbiter 2010 Build 30 Aug 2010 v.100830, and no, my installation isn't "Vanilla".

I'd like to close down this thread anyway as I have a suitable workaround that I'm happy with, and I think you're right in why I'm not getting the results I want from the function.
 
Top