First History :
I just started building a 3d space game. Big surprise I know. In said game, Im flying my ship in and out of asteroids in full 3d. This means I rotate on all 3 axis freely. My camera works awesome, as do my controls. I have absolutely no trouble flying in my 3d space what so ever. I won’t bore you with the code that does this other than it roughly looks like this :
rotateX(float angle) //rotate around “x axis”
{
xangle += angle;
upvector = upvector x sin(angle)
frontvector = frontvector x cos(angle)
frontvector = upvector + frontvector
normalize frontvector
upvector = frontvector cross rightvector
}
rotateY and rotateZ look similar.
my displayCamera function looks like :
viewPoint = location + frontvector
gluLookAt(_location.x,_location.y,_location.z,viewPoint.x,viewPoint.y,viewPoint.z,_upVector.x,_upVector.y,_upVector.z);
All of this works very very well.
So here is where I need help. like most space games, Im going to run into another ship…that someone else is piloting. Now I want to look at that ship from the outside. So , to recap, this is what I know about that ship:
I have the 3 orthogonal axis of the ship
I know how many degrees the ship has turned on all of its axis.
I made the mistake that to view the ship from the outside I could simply do :
display()
{
glTranslate(ship.location.x,ship.location.y,ship.location.z);
glrotate(ship.xrot,1,0,0);
glrotate(ship.yrot,0,1,0);
glrotate(ship.zrot,0,0,1);
drawship();
}
this didnt work of course because I was rotating around global coordinates. the ship heading visually from the outside did not match the ship heading from camera view (inside the ship).
Ah, I said, I need to rotate around my local axis. So here we go :
display()
{
glTranslate(ship.location.x,ship.location.y,ship.location.z);
glrotate(ship.xrot,ship.right.x,ship.right.y,ship.right.z);
glrotate(ship.yrot,ship.top.x,ship.toy.y,ship.top.z);
glrotate(ship.zrot,ship.front.x,ship.front.y,ship.front.z);
drawship()
}
I have discovered that this doesn’t work. Between order of matrix multipation and what not,anyoen who knwos my pain knows the ships rotation will not actually correspond to the way the ship is “heading”.
Evertyhing I have read talks about loading in a hand made matrix after creating my own for such a crazy thing, since I am trying to rotate round 3 indepedant axis’ and not 2.
So, here is my question(s).
- can anyone suuggest a half ways decent and light weight matrix library for java to do this
- Will this actualy solve my problem?
To summarise what Im trying to do. When I pilot a ship, and I Bank left then pull up, my camera does the right thing and the “pull up” is on a tilted axis. When I view it from teh outside world using 3 glrotates with an identity matrix the bank works fine but the “pull up” follows the normal 1,0,0 which makes sense. In otherwords, I am not facing the way I am heading.
using my second method does not give me good results either.
THanks for any assistance in this :-).