PDA

View Full Version : looking for help/tools for 3d trnansformations



gandolf23
08-28-2015, 06:12 PM
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,view Point.x,viewPoint.y,viewPoint.z,_upVector.x,_upVec tor.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.l ocation.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.l ocation.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).

1) can anyone suuggest a half ways decent and light weight matrix library for java to do this
2) 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 :-).

GClements
08-28-2015, 09:31 PM
1) can anyone suuggest a half ways decent and light weight matrix library for java to do this

No idea; I don't do much Java. But a matrix library is so trivial that you can probably write one quicker than you can find, download and install an existing one.

The matrices generated by the legacy OpenGL and GLU functions can be found in the reference pages (http://www.opengl.org/sdk/docs/man2/).

Apart from that, you just need functions for multiplication and possibly inverse (although a generalised inverse is rarely necessary).



2) Will this actualy solve my problem?

Probably.

The obvious method of implementing "flight" motion is to maintain the object's current transformation matrix then update it each tick by post-multiplying an incremental transformation corresponding to the linear and rotational velocity (in turn, that transformation may be similarly updated by an incremental transformation for the acceleration).

The camera transformation is the inverse of the object's transformation (for the object to which the camera is attached). So with a camera attached to an object with transformation A, when drawing an object with transformation B, the overall transformation would be A-1.B.

Assuming that the object transformations consist of a rotation plus a translation, the inverse can be simplified:
the rotation component (upper-left 3x3 submatrix) is transposed, and the translation is transformed by the transposed rotation and negated.

gandolf23
08-29-2015, 03:12 PM
Ok. All if that is good to know. I will look at coding that this weeken. Thanks :-).

gandolf23
08-31-2015, 10:16 AM
I took your suggestions over the weekend, firmed up and completed my understanding of how the matrix stack works and everything works swimmingly. Thanks for your help :-).