0
\$\begingroup\$

I am trying to implement a simple camera mouse look.

if (Input.GetInstance().GetMouseKey(MouseButton.Middle))
{
    Vector2 d = Input.GetInstance().getMouseDelta() * 0.001f;
    camera.transform.AddRotation(new Vector3(-d.Y, d.X, 0));
}

Here is the camera matrix:

cameraMatrix = Matrix4.LookAt(myEntity.transform.Position, myEntity.transform.Position + myEntity.transform.GetForward(), myEntity.transform.GetUp());

I also tried to create camera matrix like this, but the results were the same:

Matrix4 t = Matrix4.CreateTranslation(myEntity.transform.Position);
Matrix4 r = myEntity.transform.RotationMatrix;
cameraMatrix = t * r;

Here is the rotation matrix of transform:

var x = Matrix4.CreateRotationX(rotation.X);
var y = Matrix4.CreateRotationY(rotation.Y);
var z = Matrix4.CreateRotationZ(rotation.Z);
rotationMatrix = x * y * z;

Add rotation of transform:

public void AddRotation(Vector3 rotation)
{
    if (rotation == Vector3.Zero) return;
    this.rotation += rotation;
    myEntity.ObserverOnRotationChanged();
}

Get forward method of transform:

public Vector3 GetForward()
{
    return new Vector3(RotationMatrix * new Vector4(0.0f,0.0f,1.0f,0.0f));
    //or just extract third column from matrix
}

Vertex shader:

#version 330

uniform mat4 transformMat;
uniform mat4 cameraMat;
uniform mat4 projectionMat;


layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 uv;

void main(){
    gl_Position = projectionMat * cameraMat * transformMat * vec4(position, 1.0);
}

It rotates the camera around global x axis instead of local x when i look at triangle from the side. Thanks.

front facing, z forward x right y up wrong rotation

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

When creating rotation matrix of transform it should be zyx and not xyz.

var x = Matrix4.CreateRotationX(rotation.X);
var y = Matrix4.CreateRotationY(rotation.Y);
var z = Matrix4.CreateRotationZ(rotation.Z);
rotationMatrix = z * y * x;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .