Some improvements for camera box in map hud
by Jeff "Ddraig Goch" Parry · in RTS Starter Kit · 02/03/2005 (5:23 pm) · 5 replies
I loved the camera box in the map hud, but it always bugged me that it was square, which is only true at 90 degrees. As the pitch angle is lowered the box should expand at the top and elongate into a trapezoid in order to more realistically reflect the camera's actual view.
Also, when you rotated the camera the box did some weird movement in the hud but never actually rotated.
Here are some changes I made that add the trapezoid effect which changes along with changes in pitch and also rotates the box when the camera is rotated.
I didn't submit this as a resource because I know my coding sucks, but at least this will give you an idea of how to do it. If you can make the code cleaner and more elegant please have at it.
Just thought I should contribute back something since I have gained so much from these forums and resources. If I missed this somewhere else in the forums, I apologize in advance.

All the changes are in game/RTS/guiMapHudRender.cc
Go to around line 75 and find this code:
Just replace it with:
That's it. Hope it is useful.
Note my comments on changing the line color to yellow and increasing the line width to 2.
Edit: forgot to add the picture...doh
Edit2: Added comments per suggestion from Josh
Also, when you rotated the camera the box did some weird movement in the hud but never actually rotated.
Here are some changes I made that add the trapezoid effect which changes along with changes in pitch and also rotates the box when the camera is rotated.
I didn't submit this as a resource because I know my coding sucks, but at least this will give you an idea of how to do it. If you can make the code cleaner and more elegant please have at it.
Just thought I should contribute back something since I have gained so much from these forums and resources. If I missed this somewhere else in the forums, I apologize in advance.
All the changes are in game/RTS/guiMapHudRender.cc
Go to around line 75 and find this code:
glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINE_LOOP); glVertex2f(screenCenter.x - squareOffset.x, screenCenter.y - squareOffset.y); glVertex2f(screenCenter.x + squareOffset.x, screenCenter.y - squareOffset.y); glVertex2f(screenCenter.x + squareOffset.x, screenCenter.y + squareOffset.y); glVertex2f(screenCenter.x - squareOffset.x, screenCenter.y + squareOffset.y); glEnd();
Just replace it with:
//Get camera rotation F32 yaww = mDegToRad(camera->getYawAngle()); //Calculate Sin and Cos of camera rotation angle F32 syaw = mSin(yaww); F32 cyaw = mCos(yaww); //Arbitrary adjustment to create trapezoid, you might want to play with these numbers //to get it to look like you want. Trapezoid sizing and elongation are simply an inverse incremental //change based proportionately on the pitch change. It should probably be an exponential //or logarithmic relationship (still better than a square though, IMHO). You can adjust the effect by //playing with the multiplication factor in this line. F32 adjusttop = ((90-camera->getPitchAngle())*0.5); //Change square to trapazoid to more accurately show effect of change in pitch angle Point2F botLeft = Point2F(screenCenter.x - squareOffset.x, screenCenter.y - squareOffset.y); Point2F botRight = Point2F(screenCenter.x + squareOffset.x, screenCenter.y - squareOffset.y); Point2F topRight = Point2F(screenCenter.x + squareOffset.x + adjusttop, screenCenter.y + squareOffset.y - adjusttop); Point2F topLeft = Point2F(screenCenter.x - squareOffset.x - adjusttop, screenCenter.y + squareOffset.y - adjusttop); //Translate square to a z origin of 0,0 botRight -= screenCenter; botLeft -= screenCenter; topLeft -= screenCenter; topRight -= screenCenter; //Calculate rotation around origin of 0,0 Point2F nbotRight = Point2F(botRight.x*cyaw - botRight.y*syaw, botRight.y*cyaw + botRight.x*syaw); Point2F nbotLeft = Point2F(botLeft.x*cyaw - botLeft.y*syaw, botLeft.y*cyaw + botLeft.x*syaw); Point2F ntopLeft = Point2F(topLeft.x*cyaw - topLeft.y*syaw, topLeft.y*cyaw + topLeft.x*syaw); Point2F ntopRight = Point2F(topRight.x*cyaw - topRight.y*syaw, topRight.y*cyaw + topRight.x*syaw); //Translate rotated square back to original origin :) nbotRight += screenCenter; nbotLeft += screenCenter; ntopLeft += screenCenter; ntopRight += screenCenter; //Draw box glColor3f(1.0, 1.0, 0.0);//Yellow changed from 0.0, 1.0, 0.0 which is Green glLineWidth(2.0); //Delete to go back to default glBegin(GL_LINE_LOOP); glVertex2f(nbotRight.x, nbotRight.y); glVertex2f(nbotLeft.x, nbotLeft.y); glVertex2f(ntopLeft.x, ntopLeft.y); glVertex2f(ntopRight.x, ntopRight.y); glEnd();
That's it. Hope it is useful.
Note my comments on changing the line color to yellow and increasing the line width to 2.
Edit: forgot to add the picture...doh
Edit2: Added comments per suggestion from Josh
#2
Good snippet, thanks!
02/04/2005 (9:58 am)
As an FYI, there isn't anything wrong with the coding at all--in fact, it's better documented than most! I think the only "critique" I myself could make would be to be more consistent with your capitalization/variable naming--you tend to sometimes capitalize all but the first letter in your words within a variable name, but not always (botRight vs nbotRight for example).Good snippet, thanks!
#3
Brings up an interesting question though. Is there a standard naming convention we should be using when submitting code? For that matter, is there a standard formatting convention we should try to stick to for submitted code (e.g. indents, comment placement, bracket placement)? I did a quick search on the forums but didn't find anything. Maybe it's not that big of an issue. More likely I am just so old and gone through so many languages and coding conventions, that I am hopelessly out-of-date and unable to remember which one to use...ha. (I am in my mid-forties and still playing games, tsk, tsk. I'm probably the oldest fart in the forums, would love to hear if anyone else here is also an old curmudgeon.)
On a different topic. You might notice that the box does not rotate exactly around its "center". This is because the camera does not rotate exactly around the screen center. I need to find the offset in order to adjust either the box center or the camera, I think, but haven't located it yet. Also, the trapezoid sizing and elongation are simply an inverse incremental change based proportionately on the pitch change. It should probably be an exponential or logarithmic relationship (still better than a square though, IMHO). You can adjust the effect by playing with the multiplication factor in
02/04/2005 (11:27 am)
Thanks and yep, you are right, I was inconsistent. Sad part is I thought I cleaned it up. Seems I sometimes can't make up my mind when the multi-word variable name starts with a letter rather than an actual word (e.g. nbotRight). The n is just a convention I have always used to indicate a modified version of the original variable (e.g. botRight). For some reason I don't like working with "nBotRight" and "botRight" they just look too "different" to me. If I was smart I would just stop being so lazy and make them "origBotRight" and "newBotRight". Brings up an interesting question though. Is there a standard naming convention we should be using when submitting code? For that matter, is there a standard formatting convention we should try to stick to for submitted code (e.g. indents, comment placement, bracket placement)? I did a quick search on the forums but didn't find anything. Maybe it's not that big of an issue. More likely I am just so old and gone through so many languages and coding conventions, that I am hopelessly out-of-date and unable to remember which one to use...ha. (I am in my mid-forties and still playing games, tsk, tsk. I'm probably the oldest fart in the forums, would love to hear if anyone else here is also an old curmudgeon.)
On a different topic. You might notice that the box does not rotate exactly around its "center". This is because the camera does not rotate exactly around the screen center. I need to find the offset in order to adjust either the box center or the camera, I think, but haven't located it yet. Also, the trapezoid sizing and elongation are simply an inverse incremental change based proportionately on the pitch change. It should probably be an exponential or logarithmic relationship (still better than a square though, IMHO). You can adjust the effect by playing with the multiplication factor in
((90-camera->getPitchAngle())*0.5);
#4
02/05/2005 (7:42 pm)
Looks good to me Jeff. Throw in some comments about the box size and rotation as you've just done in the post above, and people can configure it however they want for their game. Nice job and thanks! :)
#5
09/18/2005 (5:36 am)
OK you asked for it!!! My associate at ATOMIX Productions (Maylock) is 54 and I am 53 (Shon) and we are writing Video games and Composing music and creating art even at our refined age. Age is experience and we are it. I have been programming for 29 years on more systems than I care to think about and am deluged in syntax to the degree of living in a haze and losing a marriage to it. So we understand. Ohh you got me carried away. Great post by the way.
Torque 3D Owner Martin "Founder" Hoover