Request Gateway transport system

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
Huh... then something with that distance calculation is going seriously wrong. These positions lie far outside the visible universe on one axis. I thought you might be reading the uninitialised vector, but at this point in the code it should clearly contain the right values.

Try getting the global positions of both vessels instead, subtract them, and then get the length of that vector, to see if you get something more sensible.
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
Thanks.
Not sure if this is correct:
Code:
		if (hvessel != GetHandle())
		{
			VECTOR3 rpos, rvel,gpos1;
			oapiGetRelativePos(hvessel, GetHandle(), &rpos);
			double distance = length(rpos);
			oapiGetRelativeVel(hvessel, GetHandle(), &rvel);
			GetGlobalPos(gpos1);

after a couple times of continue I get this:
+ data 0x00aefc00 {9.881312916825e-324#DEN, 1.#QNAN00000000000, 1.400543224672e-312#DEN} double[3]

So this is what i get on the first break:
- gpos1 {data=0x00aefc00 {-1.4089627996560444e-015, -0.50000000000000078, 6.1232339957367660e-017} x=-1.4089627996560444e-015 ...} VECTOR3

then 2nd break:
- gpos1 {data=0x00aefc00 {4.940656458412e-324#DEN, 1.#QNAN00000000000, 1.400543224672e-312#DEN} x=4.940656458412e-324#DEN ...} VECTOR3
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
No, I meant getting the global position of both vessels and then calculating the distance by subtracting those positions, instead of using oapiGetRelativePos.

Also, your compiler settings seem to be weird... You're getting quiet NaNs and subnormals in there, both of which should not appear with default VisualStudio compiler settings, as far as I'm aware.
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
Thanks. Ok so Not sure what the code is going to look like?

Not sure about the settings. I had a hard time getting to debug.
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
Thanks

So do I need to initialize the vectors?

Or our the setting on the compiler wrong for debug?
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,588
Reaction score
2,312
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
So do I need to initialize the vectors?

Yes. always initialize your variables. Even if you are sure you will set them at their first use. One refactoring and you read them at the first use by accident.
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
Thanks. But what about Vectors like:
VECTOR3 rpos, rvel,gpos1?
Since it seems they seem to be out of range?
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
Thanks. Ok so Not sure what the code is going to look like?

Something like this:

Code:
VECTOR3 vesselpos = _V(0, 0, 0);
VECTOR3 gatepos = _V(0, 0, 0);

oapiGetGlobalPos(hvessel, &vesselpos);
oapiGetGlobalPos(hvessel, &gatepos);
VECTOR3 relpos = vesselpos - gatepos;
double distance = length(relpos);

Just to be clear, according to the documentation, the code you wrote should work.
This is more of a debugging exercise to determine where the problem might lie. Initialising the vectors in this context should not actually be necessary, but initialising your variables is never bad, especially in C++.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,588
Reaction score
2,312
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
This is more of a debugging exercise to determine where the problem might lie. Initialising the vectors in this context should not actually be necessary, but initialising your variables is never bad, especially in C++.

No, it is also something to reduce the possible causes for the bug to the unknown issues - if you initialize the variables properly, you can even see faster, if some value is outside bounds.
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
Ok. So i ran the debugger and get these:
- gatepos {data=0x00aefba0 {1.222289164943e-311#DEN, 0.33141357403559168, -0.38268327933110413} x=1.222289164943e-311#DEN ...} VECTOR3

- vesselpos {data=0x00aefb80 {9.3441112843944431e+107, 2.8940969698736104e+266, 1.223257801720e-311#DEN} x=9.3441112843944431e+107 ...} VECTOR3

distance 4.4390151428890942e-201 double
Code:
void WORMHOLE::clbkPostStep(double simt, double simdt, double mjd)
{
	for (UINT i = 0; i<oapiGetVesselCount(); i++)
	{
		OBJHANDLE hvessel = oapiGetVesselByIndex(i);

		if (hvessel != GetHandle())
		{
			//VECTOR3 rpos, rvel,gpos1;
			VECTOR3 vesselpos = _V(0, 0, 0);
			VECTOR3 gatepos = _V(0, 0, 0);

			oapiGetGlobalPos(hvessel, &vesselpos);
			oapiGetGlobalPos(hvessel, &gatepos);
			VECTOR3 relpos = vesselpos - gatepos;

			double distance = length(relpos);



			oapiGetRelativePos(hvessel, GetHandle(), &rpos);
			//double distance = length(rpos);
			oapiGetRelativeVel(hvessel, GetHandle(), &rvel);
			GetGlobalPos(gpos1);


			sprintf(oapiDebugString(), "DISTANCE %0.4f", distance);


			if ((distance<2) && (rpos.z>0) && (rvel.z<0))
			{

				VESSELSTATUS2 vs_vessel, vs_other_gate;
				memset(&vs_vessel, 0, sizeof(vs_vessel));
				memset(&vs_other_gate, 0, sizeof(vs_other_gate));
				vs_vessel.version = 2;
				vs_other_gate.version = 2;

				VESSEL *v;
				v = oapiGetVesselInterface(hvessel);
				v->GetStatusEx(&vs_vessel);
				OBJHANDLE h_other_gate;
				char myname[16];
				char GateA[16];
				sprintf(myname, GetName());
				sprintf(GateA, "Gate_A");
				if (strcmp(myname, GateA) == 0)
				{
					h_other_gate = oapiGetVesselByName("Gate_B");
				}
				else{
					h_other_gate = oapiGetVesselByName("Gate_A");
				}
				VESSEL *v_other_gate;
				v_other_gate = oapiGetVesselInterface(h_other_gate);
				v_other_gate->GetStatusEx(&vs_other_gate);
				vs_vessel.rbody = vs_other_gate.rbody;
				vs_vessel.rpos = vs_other_gate.rpos;
				vs_vessel.vrot = vs_other_gate.vrot;
				vs_vessel.arot = vs_other_gate.arot;

				VECTOR3 outvel = _V(rvel.x, rvel.y, rvel.z);
				VECTOR3 rofs;
				GlobalRot(outvel, rofs);
				vs_vessel.rvel.x = vs_other_gate.rvel.x + rofs.x;
				vs_vessel.rvel.y = vs_other_gate.rvel.y + rofs.y;
				vs_vessel.rvel.z = vs_other_gate.rvel.z + rofs.z;






				v->DefSetStateEx(&vs_vessel);
			}
		}
	}


}
When i run it in non debug. Distance is 0.0000 but nothing happens
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
distance 4.4390151428890942e-201 double

That's 0.(200 more zeroes)4, so as close to zero as as you'll ever care, so that seems to work. BUT:
This is not what the values of the vectors should result in. I strongly suspect that you're not seeing the right values in the debugger because you have some optimizations turned on. Please go to your project properties -> C++ -> Optimization and make sure that everything there is turned off. Compiler optimizations while debugging can lead to wrong output.

When i run it in non debug. Distance is 0.0000

Of course it is, you're rounding it to 4 digits behind the period for output. As I said, that result has 200 zeroes behind the period before the first non-zero digit appears. It's a common issue with doubles that they are very exact, so a result that would appear as zero to any reasonable person is actually non-zero for the computer (it's also got to do with some numbers actually not being precisely representable in binary, but that's not really an issue you have to worry about for your purposes).

That's also why it's a bad idea to use == on doubles unless you round them first. However, you don't do that, you're using < which is the propper way to go.

but nothing happens

I'm not surprised. Take a good look at that if statement and ask yourself what it really is you're checking for:

Code:
if ((distance<2) && (rpos.z>0) && (rvel.z<0))

You're saying "execute this code if:
distance is smaller than 2 meters (Ok) AND
the relative distance in the z-axis is positive (i.e. the vessel is "above" the gate in the global coordinate system. Undoubtedly not what you want!) AND
the vessel is moving "upward" in relation to its own coordinate system (also not what you want).
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,588
Reaction score
2,312
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
I'm not surprised. Take a good look at that if statement and ask yourself what it really is you're checking for:

Code:
if ((distance<2) && (rpos.z>0) && (rvel.z<0))
You're saying "execute this code if:
distance is smaller than 2 meters (Ok) AND
the relative distance in the z-axis is positive (i.e. the vessel is "above" the gate in the global coordinate system. Undoubtedly not what you want!) AND
the vessel is moving "upward" in relation to its own coordinate system (also not what you want).

I somehow remember this statement and the problems ... feels like Groundhog day. Was it one year ago?
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
I somehow remember this statement and the problems ... feels like Groundhog day. Was it one year ago?

The joys of copy-pasting code without understanding it :lol:

@gattispilot For what it's worth, you're a very productive member of this community, which is why I'm trying to help you out when I can. You're certainly not lazy, otherwise I wouldn't bother. But it would be very nice if you'd start to try to understand your code... ;)
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
Thanks:)

Here are the optimization settings. debug and release
6mYjetD.jpg


Thanks for all the help and compliments:)
So this now works. But In the DG If I move in the -z direction the ship moves into the gate.

Code:
if ((distance<2) && (rpos.z>0) && (rvel.z<0))

But all I really want is if the distance is less than 2 meters.
Code:
if (distance<2)
			{

So now execute the jump if the distance is less than 2 meters. I am assuming that is the center of both vessels.

Then I get this when it jumps:
Code:
BEGIN_SHIPS
Gate_A:WORMHOLE
  STATUS Orbiting Sun
  RPOS -1.#J -1.#J -1.#J
  RVEL -1.#IO -1.#IO -1.#IO
  AROT 0.00 -0.00 0.00
  AFCMODE 7
  NAVFREQ 0 0
  XPDR 0
END
Gate_B:WORMHOLE
  STATUS Orbiting Sun
  RPOS -1.#J -1.#J -1.#J
  RVEL -1.#IO -1.#IO -1.#IO
  AROT 0.00 -0.00 0.00
  AFCMODE 7
  NAVFREQ 0 0
  XPDR 0
END
dg:Deltaglider
  STATUS Orbiting Sun
  RPOS -1.#J -1.#J -1.#J
  RVEL -1.#IO -1.#IO -1.#IO
  AROT 0.00 -0.00 0.00
  AFCMODE 7
  PRPLEVEL 0:0.985416 1:1.000000
  NAVFREQ 0 0 0 0
  XPDR 0
  AAP 0:0 0:0 0:0
END
END_SHIPS

So we get the positions on the gate and the other vessel. And get how far away they are. It could be the same z position but above or below. left or right


So I am now trying to understand the code:
Code:
void WORMHOLE::clbkPostStep(double simt, double simdt, double mjd)
{
	for (UINT i = 0; i<oapiGetVesselCount(); i++)
	{
		OBJHANDLE hvessel = oapiGetVesselByIndex(i);

		if (hvessel != GetHandle())
		{
			//VECTOR3 rpos, rvel,gpos1;
			VECTOR3 vesselpos = _V(0, 0, 0);  //initialize vector:
			VECTOR3 gatepos = _V(0, 0, 0);   //initialize vector:

			oapiGetGlobalPos(hvessel, &vesselpos);
			oapiGetGlobalPos(hvessel, &gatepos);
			VECTOR3 relpos = vesselpos - gatepos;

			double distance = length(relpos);



		//	oapiGetRelativePos(hvessel, GetHandle(), &rpos);
			//double distance = length(rpos);
		//	oapiGetRelativeVel(hvessel, GetHandle(), &rvel);
		//	GetGlobalPos(gpos1);


			sprintf(oapiDebugString(), "DISTANCE %0.9f", distance);

So how it know which is the gate and other other vessel?
Code:
			oapiGetGlobalPos(hvessel, &vesselpos);
			oapiGetGlobalPos(hvessel, &gatepos);
 
Last edited:

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
So how it know which is the gate and other other vessel?

It doesn't. I screwed up when I wrote that piece of code on the quick. Of course one call to oapiGlobalPos should be with the handle of the vessel, the other with the handle of the gate. Like this, the result will always be zero.
 

gattispilot

Addon Developer
Addon Developer
Joined
Oct 17, 2007
Messages
8,636
Reaction score
2,613
Points
203
Location
Dallas, TX
That makes sense so that vesselpos is the vessel and gatepos is the gate position?
Code:
for (UINT i = 0; i<oapiGetVesselCount(); i++)
	{
		OBJHANDLE hvessel = oapiGetVesselByIndex(i);
So this gets all the handles

So something like if hvessel = Gate then oapiGetGlobalPos(hvessel, &gatepos);




then Gatepos equals
 
Top