RTS/simulation overhead camera function.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

RTS/simulation overhead camera function.

Post by agamemnus »

It's an overhead-type camera that I made for my game.

First it sets the X, Y value, then rotates individually on all three axes, and finally it zooms in. There is no gimbal lock in this camera; it allows full 360 degree rotation on each of the three axes.

Any tips or code are welcome!

Edit 4534534: Modified the code per randomMesh's optimization.

Code: Select all

/* ----------------------------------------------------------------------------
 Sets the camera to the desired rotx/roty/rotz/x/y/z values.
*/

void DLL_EXPORT IrrSetCamera (irr::scene::ICameraSceneNode* camera,
                              float rotx, float roty, float rotz,
                              float x, float y , float z) {

 quaternion tempQuaternion;

 // Work out the 3 axes for the camera:
 // Forward = target (0,0,0) - position -- the position at (x=0, y=0) is (0,0,1).
 // Right = -1 * cross-product of up.
 // Pos = -forward + x * right + y * up.
 vector3df up(0,1,0);
 vector3df forward(0,0,-1);
 vector3df pos(x,y,1);

 tempQuaternion.fromAngleAxis(rotz, forward);
 tempQuaternion.getMatrix().rotateVect(up);

 tempQuaternion.fromAngleAxis(rotx, up);
 tempQuaternion.getMatrix().rotateVect(forward);

 vector3df right = forward.crossProduct (up);
 tempQuaternion.fromAngleAxis(roty, right);
 tempQuaternion.getMatrix().rotateVect(forward);
 tempQuaternion.getMatrix().rotateVect(up);

 pos = pos + z * forward;
 camera->setUpVector (up);
 camera->setTarget (pos + forward);
 camera->setPosition (pos);
}
Last edited by agamemnus on Tue May 17, 2011 5:34 am, edited 7 times in total.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Post by REDDemon »

changing the up vector is the way to go for prevent gimbal lock, why didn't you put that in code snippets? of course, is much simpler changin only the "lookAt" and the "position" parameter, but there is no reason for not updating the up vector.

you don't need to optimize that too much. it will be called only once per frame. and it seems you are doing right math so it is almost the fastest possible solution.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Rate my camera function.

Post by randomMesh »

agamemnus wrote:not sure if any more optimizations can be made.

Code: Select all

 vector3df up = vector3df(0,1,0);
 vector3df forward = vector3df(0,0,-1);
 vector3df right = vector3df(1,0,0);

Code: Select all

vector3df up(0,1,0);
vector3df forward(0,0,-1);
vector3df right(1,0,0);
"Whoops..."
agamemnus
Posts: 283
Joined: Sun Jan 31, 2010 6:06 pm

Re: Rate my camera function.

Post by agamemnus »

REDDemon wrote:changing the up vector is the way to go for prevent gimbal lock, why didn't you put that in code snippets? of course, is much simpler changin only the "lookAt" and the "position" parameter, but there is no reason for not updating the up vector.
I didn't feel it was very good. When I initially posted it yesterday it was a lot worse. I had a few minor brainstorms and cleaned up a lot of the code. I am honored that you think the code should be in the code snippets section. :D

you don't need to optimize that too much. it will be called only once per frame. and it seems you are doing right math so it is almost the fastest possible solution.
Thanks... I think maybe I can modify the fromAngleAxis code a bit more. Edit: optimized it even further by simplifying the starting position variable!
Edit 2: there still might be something not quite kosher with this function. My sea texture coordinates "jump" when my rotation angles pass something like 30 degrees... but not the other textures. IDK.
randomMesh wrote:

Code: Select all

vector3df up(0,1,0);
vector3df forward(0,0,-1);
vector3df right(1,0,0);
Thanks. :)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Rate my camera function.

Post by hybrid »

randomMesh wrote:
agamemnus wrote:not sure if any more optimizations can be made.

Code: Select all

 vector3df up = vector3df(0,1,0);
 vector3df forward = vector3df(0,0,-1);
 vector3df right = vector3df(1,0,0);

Code: Select all

vector3df up(0,1,0);
vector3df forward(0,0,-1);
vector3df right(1,0,0);
Not an optimization, but just a different syntax, though.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Rate my camera function.

Post by randomMesh »

hybrid wrote:Not an optimization, but just a different syntax, though.
Mhh, i thought that the compiler first creates a temporary vector3df by invoking the constructor and then uses the copy constructor to initialize forward as a copy of that temporary. So wouldn't it be faster to contruct the vector3df by directly calling the constructor?

Anyway, i think it's more readable, so it's an optimization. :)

Btw, it's good style to do

Code: Select all

vector3df right(1.0f, 0.0f, 0.0f);
since the constructor of vector3df takes floats not ints and one should reflect that.
"Whoops..."
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

randomMesh wrote:Anyway, i think it's more readable, so it's an optimization.
C:\source\main.cpp||In function 'int main()':|
C:\\source\main.cpp|73|error: expected type qualifier before 'optimization'|

what exactly do you mean by optimization? :) i do not think making code easier to read would exactly mean a performance optimization.

remember

Code: Select all

void                     
ddddsdasfgv *(   const





 typa&                              nook


);;;                     ;;;;;
would be completely valid but not exactly readable
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

ChaiRuiPeng wrote:i do not think making code easier to read would exactly mean a performance optimization
It's an improvement nevertheless.
Richard Buckminster Fuller wrote:When I am working on a problem I never think about beauty. I only think about how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong.
"Whoops..."
ChaiRuiPeng
Posts: 363
Joined: Thu Dec 16, 2010 8:50 pm
Location: Somewhere in the clouds.. drinking pink lemonade and sunshine..

Post by ChaiRuiPeng »

beauty is in the eye of the beholder though.
ent1ty wrote: success is a matter of concentration and desire
Butler Lampson wrote: all problems in Computer Science can be solved by another level of indirection
at a cost measure in computer resources ;)
Post Reply