# Thread: Difficulty setting up the viewmatrix rotation

1. ## Difficulty setting up the viewmatrix rotation

Hello again,

as a continuation of my previous thread where I fought with getting the models to actually render on the screen, I implemented the joml math library and conitnued on. Now I have a following case:

- In my scene I have a model of a dragon and a camera.
- I can rotate and move the dragon in any direction I want, and the forward vector of that model changes accordingly with the rotation.
- Then I have my camera that is defined by the viewMatrix to which I would like to apply the same actions as to that model.
- There are two problems with this:
- When I rotate, the forward vector diverges in the opposite direction to the direction of rotation. Ex. Lets assume that I start of facing in the X direction. My forward vector is also facing in the X direction. Then if I rotate my camera to face the Z direction, my forward vector is not Z but -Z. As the forward vector is calculated based on the rotation quaternion, and is correct for the dragon model, I believe the issue is with the viewMatrix (I should be facing in the -Z direction)
- Once I move the camera, any rotation that I apply to it is around the world origin and world axis. I know this is a trivial problem, however, I find it difficult to get this working as well.
- Those problems occur even though I apply exactly the same methods as I do for the dragon model.

I believe, the issue is not with the rotation methods or the forward vector calculation (although I found that the JOML function for this did not give me the desired results), but with my viewMatrix.

I define the viewMatrix as follows:

Code :
```public static Matrix4f createViewMatrix(Camera camera)
{

Matrix4f viewMatrix = new Matrix4f();
Quaternionf rotation = new Quaternionf(camera.getTransform().GetRot());
Vector3f position = new Vector3f(camera.getTransform().GetPos());

position.mul(-1);

Vector3f up = new Vector3f(camera.getTransform().getUp());
Vector3f forward = new Vector3f(camera.getTransform().getForward());
Vector3f pos = new Vector3f(camera.getTransform().GetPos());

viewMatrix.rotate(rotation);
viewMatrix.translate(position);

return viewMatrix;

}```

From my testing, I know that the up and forward vectors are calculating correctly.
Any insight would be greatly apprciated.

PS
In JOML there is a quaternion method for roate:
rotateAxis(angle, axis). I thought that in the axis I should input the new local axis (forward, up and right) however, through trial and error the dragon model works using world axis instead.

2. Do you have any ideas guys?

3. Originally Posted by TomaszNaglik
Do you have any ideas guys?
yes:

1. use a math library instead of coding your own (that's faster and libs are in general tested for errors)
2. the "view matrix" is the inverse of the camera's "transformation matrix"

http://www.songho.ca/opengl/gl_camera.html
https://learnopengl.com/#!Getting-started/Camera

note: the original "forward" vector doesnt point in the looking direction, but in the opposite direction

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•