SDK Question VESSELSTATUS2 surf_lat/surf_lng return weird results

  • Thread starter Thread starter Bj
  • Start date Start date

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
Using this;


Code:
DLLCLBK void opcPostStep(double simt, double simdt, double mjd)
{
    OBJHANDLE oh = oapiGetFocusObject();
    VESSEL * v = oapiGetVesselInterface(oh);
    VESSELSTATUS2 tmp;

    v->GetStatusEx(&tmp);

    sprintf (oapiDebugString(), "%f, %f", tmp.surf_lat,tmp.surf_lng);
}
with the API ref saying;

surf_lng longitude: vessel position in equatorial coordinates of rbody [rad]
surf_lat latitude: vessel position in equatorial coordinates of rbody [rad]
Shouldn't the results be...?
[math]-\pi < x > \pi[/math]

Why am I getting insane numbers like 1 trillion+?
 
That's because you're telling sprintf to parse two 64-bit doubles as two 32-bit floats. :) What you need is this:

Code:
sprintf (oapiDebugString(), "[COLOR="Red"]%lf[/COLOR], [COLOR="red"]%lf[/COLOR]", tmp.surf_lat,tmp.surf_lng);
 
I think you didn't initialize the VESSELSTATUS2 structure correctly.
Following test code produces expected results:

Code:
#include <stdio.h>

int main()
{
    char buffer[256];
    double PI = 3.14159265;
    sprintf(buffer, "%f", PI);
    printf("%s\n", buffer);
    scanf(" ");
    return 0;
}

VESSELSTATUS2 should be initialized with:
Code:
VESSELSTATUS2 vs;
memset (&vs, 0, sizeof(vs));
vs.version = 2;
 
Last edited:
Yup,

API Reference said:
• The version field of the VESSELSTATUSx structure must be set by the caller
prior to calling the method, to tell Orbiter which interface version is required.
• In addition, the caller must set the VS_FUELLIST, VS_THRUSTLIST and
VS_DOCKINFOLIST bits in the flag field, if the corresponding lists are
required. Otherwise Orbiter will not produce these lists.
Nevertheless, you really should use "%lf" on doubles, to make your code compiler-independent.
 
Code:
#include <orbitersdk.h>

DLLCLBK void opcPreStep(double simt, double simdt, double mjd)
{
	VESSEL * v = oapiGetFocusInterface();
        VESSELSTATUS2 tmp;
	memset (&tmp, 0, sizeof(tmp));
	tmp.version = 2;
        v->GetStatusEx(&tmp);
        sprintf (oapiDebugString(), "%f, %f", tmp.surf_lat,tmp.surf_lng);
}

Seems to work :D
 
Yes, but please don't place code with errors like "%f" on doubles, which is supposed to be an example code. Others may copy it and not realise why it doesn't work on their compiler, you know.
 
Yes, but please don't place code with errors like "%f" on doubles, which is supposed to be an example code. Others may copy it and not realise why it doesn't work on their compiler, you know.

Meh, I like using %f ;)

ps. You realize you are more likely going to come across a compiler that doesn't know what "lf" is then one that won't properly do the conversion of doubles with "f", right? And Orbiter libraries are only compatible with Microsoft's compiler series.

---------- Post added at 05:20 AM ---------- Previous post was at 05:07 AM ----------

BTW it turns out that "lf" isn't even a valid input for sprintf when giving doubles as arguments...
 
There's no excuse for using "MS C/C++". Sometimes a programmer has to go outside of the Orbiter, and even the Windows world.

Anyway it seems that you're right about that "lf"

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

The lowercase l applies only to integer and character types. There's also capital L, but it applies to "long double"

I swear it was "lf" for double back in the Old Days.

[EDIT]

If you think about it, the capital L instead of lower l for long double may be intentional, so that old code which used lower case l for doubles still compiles properly.
 
Last edited:
I swear it was "lf" for double back in the Old Days.

Like me, you are probably thinking about scanf, which does require "%lf" to correctly parse a double and "%f" to correctly parse a float. After some research, it turns out that the compiler does indeed auto-promote floats to doubles before passing them to functions that take variable arguments. In other words, you can get away with "%f" for doubles for printf, but not for scanf. More info here: http://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f
 
Back
Top