Orbiter-Forum  

Go Back   Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter SDK
Register Blogs Orbinauts List Social Groups FAQ Projects Mark Forums Read

Orbiter SDK Orbiter software developers post your questions and answers about the SDK, the API interface, LUA, meshing, etc.

Reply
 
Thread Tools
Old 08-04-2018, 04:42 PM   #1
JMW
Aspiring Addon Developer
 
JMW's Avatar
Default Nested loop?

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);
}
JMW is offline   Reply With Quote
Old 08-04-2018, 06:35 PM   #2
jedidia
shoemaker without legs
 
jedidia's Avatar
Default

Quote:
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...
jedidia is offline   Reply With Quote
Old 08-04-2018, 07:03 PM   #3
JMW
Aspiring Addon Developer
 
JMW's Avatar
Default

Quote:
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 ! (with a little help from my friends and the internet)

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

Anyway, any help gratefully received chaps.
JMW is offline   Reply With Quote
Thanked by:
Old 08-04-2018, 09:51 PM   #4
jedidia
shoemaker without legs
 
jedidia's Avatar
Default

Quote:
I just keep jiggling things around till it works.
That's ok, even professionals do that...
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.
jedidia is offline   Reply With Quote
Thanked by:
Old 08-05-2018, 06:00 PM   #5
JMW
Aspiring Addon Developer
 
JMW's Avatar
Default

Maybe if you know what you're doing it helps

But, is there anyone (including you jedidia) that could please help with my problem?
JMW is offline   Reply With Quote
Old 08-06-2018, 08:04 AM   #6
jedidia
shoemaker without legs
 
jedidia's Avatar
Default

Quote:
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 by jedidia; 08-06-2018 at 08:15 AM.
jedidia is offline   Reply With Quote
Thanked by:
Old 08-06-2018, 10:19 AM   #7
martins
Orbiter Founder
Default

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.
martins is offline   Reply With Quote
Thanked by:
Old 08-06-2018, 09:58 PM   #8
JMW
Aspiring Addon Developer
 
JMW's Avatar
Default

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.
JMW is offline   Reply With Quote
Old 08-07-2018, 01:38 AM   #9
martins
Orbiter Founder
Default

Quote:
Originally Posted by JMW View Post
 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.
martins is offline   Reply With Quote
Thanked by:
Old 08-07-2018, 08:29 AM   #10
jedidia
shoemaker without legs
 
jedidia's Avatar
Default

Quote:
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...
jedidia is offline   Reply With Quote
Thanked by:
Old 08-07-2018, 08:38 AM   #11
JMW
Aspiring Addon Developer
 
JMW's Avatar
Default

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
JMW is offline   Reply With Quote
Thanked by:
Old 08-07-2018, 10:00 AM   #12
martins
Orbiter Founder
Default

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 ...
martins is offline   Reply With Quote
Old 08-07-2018, 11:33 AM   #13
JMW
Aspiring Addon Developer
 
JMW's Avatar
Default

Quote:
Originally Posted by martins View Post
 I knew I gave the same answer before
Apologies, continued in that thread....
JMW is offline   Reply With Quote
Reply

  Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter SDK


Thread Tools

Posting Rules
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump


All times are GMT. The time now is 09:11 AM.

Quick Links Need Help?


About Us | Rules & Guidelines | TOS Policy | Privacy Policy

Orbiter-Forum is hosted at Orbithangar.com
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2007 - 2017, Orbiter-Forum.com. All rights reserved.