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.
http://image.noelshack.com/fichiers/2013/08/1361287639-camera.png
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)
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)
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 ^^ !)
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 ?
Thank you for reading !
Sorry if I poorly explained what I mean, my english is not perfect ask me if there is something weird !
Also I am young and the only advanced math we saw at school is cos() and sin()… :emo: Fortunately all my other math knowledge is from the Internet. So if you answer me with ‘advanced’ math, please explain a bit what you’re posting ! ;D