SDK Question Nested loop?

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
580
Reaction score
40
Points
43
Location
Happy Wherever
Help please.
Try as I may I haven't been able to display distances to bases in red if going further away, and in white if getting nearer.

Sounds simple and no doubt is, but can't crack it !

Standard display code is:
Code:
if(HUD2 == 1 && oapiGetHUDMode() == HUD_DOCKING &&oapiCockpitMode() == COCKPIT_VIRTUAL)  // Display Bases and Distances
{
	if(zoom < 100)
		
					zoom -= 0.25;
					{oapiCameraSetAperture (zoom*RAD);
					if(zoom < 16) zoom = 16;
					}

		double  line = 70;
		double linespacing =0;//= 0;
	oapi::Font *font = oapiCreateFont (15, false, "MyFont", FONT_NORMAL);
	skp->SetFont(font);
		char buf[256];
		int len = 84;
		int len2 = 4;
		int i;
		double dis2;
		int distance ;
		double lng, lat; 
		char basename[84];
	OBJHANDLE hPlanet = oapiGetGbodyByName("Earth");
		int nBase=oapiGetBaseCount(hPlanet);
		
		for (i=0; i <nBase; i++) {
				
		linespacing = linespacing + 11;
		OBJHANDLE Bases = oapiGetBaseByIndex(hPlanet, i);
		oapiGetObjectName(Bases, basename, len);
		oapiGetBaseEquPos (Bases, &lng, &lat);
		GetEquPos (longitude, latitude, radius);
			dis2 = sin((latitude)*RAD) * sin(lat*RAD) + cos((latitude)*RAD) * cos((lat)*RAD) * cos((longitude-lng)*RAD);
			dis2 = acos(dis2);
			dis2 = (dis2)*DEG;
		distance = dis2 * 6371;

		skp->SetTextColor( 0xffffff);
		
	if(i <= nBase)
		{
			if(units == 0) 
					{
					distance = distance*0.539957;
					}
	
	
		
		if (i <17)
			{
			line = 313;
			len = sprintf_s(buf,"                    %s",  basename);
			skp->SetTextAlign (oapi::Sketchpad::LEFT, oapi::Sketchpad::TOP);
			skp->Text(-140,((line) +linespacing), buf,  len);

			len2 = sprintf_s(buf,"     %i", distance);
			//skp->SetTextAlign (oapi::Sketchpad::RIGHT, oapi::Sketchpad::TOP);
			skp->Text(85,   ((line)+linespacing), buf,  len2);
				
				if(units == 0) 
					{
					skp->SetTextColor( 0x0000ff);//red
					skp->Text(180, 70, "Nautical Miles", 14);
					}
				else
				{
				skp->SetTextColor( 0x0000ff);//red
				skp->Text(190, 70, "Kilometers", 10);
				}
				
			}

		
			{
		if(i >= 17 && i <34)
				{
				line = 300;
			len = sprintf_s(buf,"                    %s",  basename);
			skp->SetTextAlign (oapi::Sketchpad::LEFT, oapi::Sketchpad::TOP);
			skp->Text(30,((line-175) +linespacing), buf,  len);
			skp->SetTextAlign (oapi::Sketchpad::RIGHT, oapi::Sketchpad::TOP);
			len2 = sprintf_s(buf,"     %i", distance);
			skp->Text(340,((line-175)+linespacing), buf,  len2);
			
				}
			}

		 {
				if(i >= 34)
				{
				line = 313;
			len = sprintf_s(buf,"                    %s",  basename);
			skp->SetTextAlign (oapi::Sketchpad::LEFT, oapi::Sketchpad::TOP);
			skp->Text(220,((line-375) +linespacing), buf,  len);
			skp->SetTextAlign (oapi::Sketchpad::RIGHT, oapi::Sketchpad::TOP);
			len2 = sprintf_s(buf,"     %i", distance);
			skp->Text(500,((line-375)+linespacing), buf,  len2);
			
				}
			}
			
		}
								
							}
		oapiReleaseFont (font);
}
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,304
Reaction score
1,503
Points
203
Location
between the planets
if(zoom < 100)

zoom -= 0.25;
{oapiCameraSetAperture (zoom*RAD);
if(zoom < 16) zoom = 16;
}

What this effectively means is:

Code:
if(zoom < 100) {
    zoom -= 0.25;
}
oapiCameraSetAperture (zoom*RAD);
if(zoom < 16) zoom = 16;

I'm not sure, but I think this is not intended. If it is, then your code formatting is horrible... :shifty:
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
580
Reaction score
40
Points
43
Location
Happy Wherever
your code formatting is horrible...

You got it in one !

I just keep jiggling things around till it works.
Takes hours, but I'm amazed at what I've managed to get to work in the last ten years ! :lol: (with a little help from my friends and the internet) :tiphat:

If that's your opinion of my first few lines well....:facepalm:

Anyway, any help gratefully received chaps. :cheers:
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,304
Reaction score
1,503
Points
203
Location
between the planets
I just keep jiggling things around till it works.

That's ok, even professionals do that... :lol:
But you should really look out for your formatting. It's amazing how many problems you can avoid just by keeping your code easily readable. ;)
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
580
Reaction score
40
Points
43
Location
Happy Wherever
Maybe if you know what you're doing it helps :yes:

But, is there anyone (including you jedidia) that could please help with my problem?
:sos:
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,304
Reaction score
1,503
Points
203
Location
between the planets
Maybe if you know what you're doing it helps

It helps even more if you don't, since at least you'll more easily see in the code what you *want* it to do, even if it doesn't.

For example, I can't realy help you with your problem, because the formatting gives me no suggestion about what is actually supposed to go where. I can't spot obvious mistakes because the formatting offers no hints at what's supposed to be here or there.

Like, I can't even find where you are making the check to change the text color if the base is moving away. It seems not to be in there at all. But I can't be quite sure, because it might be in a completely illogical place somewhere in there and I just can't spot it.
 
Last edited:

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,407
Reaction score
341
Points
83
Website
orbit.medphys.ucl.ac.uk
Regarding code formatting: most C++ aware text editors (VS editor, emacs, etc.) should be able to do that for you. Hint: I just copied and pasted your code snippet into Visual Studio, and it makes a lot more sense now (structurally, not necessarily logically ;) )

Regarding the code:
  • You have a loop that iterates i from 0 to nBase-1
  • Inside the loop you have a check for i<=nBase which is redundant (although you may have meant i<51, see below)
  • You distinguish cases i<17, 17<=i<34, and i>=34, presumably to fill 3 columns in your display (question: what happens if i>=51?)
  • You only change text colour for case i<17 (why - I thought this was supposed to be based on approach vector?), but only after all text printing has occurred, so it doesn't have any effect.

A small side note: For mutually exclusive if statements, like
Code:
if (i < 17) {
}
if (i >= 17 && i < 34) {
}
if (i >= 34) {
}
it is a bit more efficient (and a bit cleaner) to write
Code:
if (i < 17) {
}
else if (i < 34) {
}
else {
}
because it avoids the evaluation of any more conditions after the first hit, and the conditions themselves are shorter, because they take into account what has been checked before.

Edit: sorry, my mistake. The printing of the units (kilometers, nautical miles) is done after setting the text colour, so this should show up.
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
580
Reaction score
40
Points
43
Location
Happy Wherever
Thank you for your comments - they're appreciated.

The code presented does not include a part for colour change according to approach/retreat vectors.
It was just to show my method for display.

I've tried various methods but can't get any to reliably work across the whole list of bases to differentiate colour according to direction.

eg: Can oapiGetRelativeVel (Bases, hRef, &rvel) be used for this ?

I was hoping someone could put me out of my misery and provide a solution or point me in the right direction.
So far it's cost me many hours, loss of hair, of sleep and almost of wife.
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,407
Reaction score
341
Points
83
Website
orbit.medphys.ucl.ac.uk
eg: Can oapiGetRelativeVel (Bases, hRef, &rvel) be used for this ?

Yes. The sign of the cosine between relative velocity and direction vector should give you the info you need (i.e. dot product of unit vectors). This is unless you want spherical rather than linear geometry, in which case it's a bit more complicated. You'd need to calculate the great circle connecting your ship's and the target base's positions, and project the velocity vector onto that.

I have a feeling all this has been discussed in the past. Searching the forum could help here.
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,304
Reaction score
1,503
Points
203
Location
between the planets
I was hoping someone could put me out of my misery and provide a solution or point me in the right direction.

Weeeell... if your code snippet doesn't contain the part of the code that does the actual thing that doesn't work it's a wee bit difficult...
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
580
Reaction score
40
Points
43
Location
Happy Wherever
Thank you martins for your interest and help.

I have tried searching, particularly for oapiGetRelativeVel but didn't come up with anything I could understand.

jedidia, I'll try putting something together again and get back.

Sadly rl will take over for rest of week and bit of next.

Thanks
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,407
Reaction score
341
Points
83
Website
orbit.medphys.ucl.ac.uk
I knew I gave the same answer before, in response to a question by ... you! Any progress between then and now? Did any of the suggestions in that thread help? Wouldn't it have made more sense to continue that thread instead of starting from scratch? We seem to be going in circles ...
 
Top