Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (169) games submitted by our members Games in WIP (648) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Camera that rotate and move on local axes (Convert local to global.)  (Read 639 times) 0 Members and 1 Guest are viewing this topic.
AikoCraft

Junior Newbie

 « Posted 2013-02-19 16:05:55 »

Hi all !

This is my problem, I need to convert local axis translating/rotating to global axis. (I won't use matrix, so please don't ask me to use them. I will use them only if there is no other way to solve my problem.)

(X axis = right/left) (Y axis = up/down) (Z axis = front/back)
On this picture we can see that the Camera rotated on its Local Z axis, it's normal that now the Y and X global/local axis aren't parallel and the Z global/local axis are.

So right now, we can assume that if the camera goes up its y position will increase but also its x position.

To do this mathematically in the game I did this :

-Create an up Vector.
(Which store for each global axes the percentage that make them the axis nearest from the y Local)

this code show how the up vector is computed. (Don't search errors here, I'm (almost) sure that it doesn't come from here)

 1  2  3  4  5  6 `public void getUp() {    up.x = -Math.sin(Math.toRadians(zRot)) * Math.cos(Math.toRadians(yRot));    up.y = Math.cos(Math.toRadians(xRot)) * Math.cos(Math.toRadians(zRot));          up.z = -Math.sin(Math.toRadians(xRot)) * Math.cos(Math.toRadians(yRot));}    `

-Compute camera movement using up vector:

In this code you can see that I just took the previous code and integrate to it the up vector.
(I'm almost sure that there is a problem here)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28 `    public void moveForward() {                   // This code was used before, it was used for a freefly camera wich can't rotate its local z axis     //      pos.x += speed * Math.sin(Math.toRadians(yRot)) * Math.cos(Math.toRadians(xRot));    //      pos.y += speed * Math.sin(Math.toRadians(xRot)) * Math.cos(Math.toRadians(zRot));    //      pos.z += speed * Math.cos(Math.toRadians(yRot)) * Math.cos(Math.toRadians(xRot));           pos.x += speed *         (         ( (up.x * Math.sin(Math.toRadians(xRot))) + (up.y * Math.sin(Math.toRadians(yRot))) + (up.z * Math.sin(Math.toRadians(zRot))) )        *         ( (up.y * Math.cos(Math.toRadians(xRot))) + (up.z * Math.cos(Math.toRadians(yRot))) + (up.x *   Math.cos(Math.toRadians(zRot))) )        );             pos.y += speed *         (         ( (up.y * Math.sin(Math.toRadians(xRot))) + (up.z * Math.sin(Math.toRadians(yRot))) + (up.x * Math.sin(Math.toRadians(zRot))) )        *        ( (up.z * Math.cos(Math.toRadians(xRot))) + (up.x * Math.cos(Math.toRadians(yRot))) + (up.y * Math.cos(Math.toRadians(zRot))) )        );              pos.z += speed *         (         ( (up.x * Math.cos(Math.toRadians(xRot))) + (up.y * Math.cos(Math.toRadians(yRot))) + (up.z * Math.cos(Math.toRadians(zRot))) )        *        ( (up.y * Math.cos(Math.toRadians(xRot))) + (up.z * Math.cos(Math.toRadians(yRot))) + (up.x * Math.cos(Math.toRadians(zRot))) )        );    }    `

-Compute camera rotations using up vector:

The code speak better than me : (if you don't totally understand what i'm doing ask me ^^ !)

 1  2  3  4  5  6  7  8  9  10  11  12  13 `   public void turnLocalX(float dist) {      xGlobalRot += dist * up.y;      yGlobalRot += dist * up.z;      zGlobalRot += dist * up.x;   }      public void turnLocalY(float dist) {      xGlobalRot += dist * up.x;      yGlobalRot += dist * up.y;      zGlobalRot += dist * up.z;            }       // I still not did the turnLocalZ But it's the same principle.    `

But even after all of this, it doesn't work  !
Do someone have an idea of where it comes from ?