After 2 weeks of reading many math formulas and such I know what is a Quaternion, an Axis Angles and Matrices. I have made my own math libary (Java) to use on my game (LWJGL). But I'm really confused about all this.
I want to have a 3D first person camera. The move (translation) is working fine but the rotation isnt working like I need. I need a camera to rotate arround world Axis and not about its own axis. But even using Quaternions, this doesnt work and no matter how much I read about Euler Angles, everybody says to me dont touch on it!
This is a little piece of code that i'm using to make the rotation:
Quaternion qPitch = Quaternion.createFromAxis(cameraRotate.x, 1.0f, 0.0f, 0.0f);
Quaternion qYaw = Quaternion.createFromAxis(cameraRotate.y, 0.0f, 1.0f, 0.0f);
this.multiplicate(qPitch.toMatrix4f().toArray());
this.multiplicate(qYaw.toMatrix4f().toArray());
Where this
is a Matrix4f view matrix and cameraRotate is a Vector3f that just handle the angles to rotate obtained from mouse move. So I think I'm doing everything right:
- Translate the view Matrix
- Rotate the Move Matrix
So, after reading all this, I just want to know: To obtain a correct first person camera rotate, I must need to use Quaternios to make the rotations, but how to rotate around world axis?
EDIT:
OK, i'm following @Sean Middleditch solution and this should be good, but I think i've missed something...here is the code:
public Matrix4f getView() {
if (!viewChanged) return viewMatrix;
if (position == null) position = Vector3f.createEmpty();
if (rotation == null) rotation = Vector3f.createEmpty();
viewMatrix = Matrix4f.createIdentity();
viewMatrix.rotate(rotation);
Vector4f forward = Vector4f.multiplicate(viewMatrix, new Vector4f(0, 0, -1, 0));
Vector4f up = Vector4f.multiplicate(viewMatrix, new Vector4f(0, 1, 0, 0));
Vector4f right = Vector3f.cross(forward.toVector3f(), up.toVector3f()).toVector4f();
viewMatrix = new Matrix4f(new float[]{
right.x, up.x, -forward.x, 0,
right.y, up.y, -forward.y, 0,
right.z, up.z, -forward.z, 0,
-Vector4f.dot(right, position.toVector4f()), -Vector4f.dot(up, position.toVector4f()), -Vector4f.dot(forward, position.toVector4f()), 1
});
setViewChanged(false);
return viewMatrix;
}
Everything seems to be right, but the "Camera" is rotating now over the origin axis...and I think the problem is because I didnt understand the following:
construct a regular rotation matrix, and then multiply the standard camera vectors by that matrix
Thanks for reading it.
Best regards,
Afonso Lage
multiplicate
? \$\endgroup\$mul
? \$\endgroup\$