Sunday, October 9, 2022
HomeGame Developmentc# - Yaw rotation not translating to Quaternion

c# – Yaw rotation not translating to Quaternion


I am attempting to implement the gimbal lock repair described in this query, however I find yourself getting bizarre conduct the place once I rotate on the X axis (or attempt to at the least) it rotates on each the X and Y axis on the identical time, however when i try to rotate on the Y axis nothing occurs in any respect.

I am retaining complete orientation in a quaternion and (attempting to) replace it with a rotation matrix constructed by ahead, proper, and up vectors. I am deriving these vectors like so:

// From https://learnopengl.com/Getting-started/Digicam

_forward.X = (float) (MathHelper.Cos(MathHelper.DegreesToRadians(_rotation.Y)) *
                      MathHelper.Cos(MathHelper.DegreesToRadians(_rotation.X)));

_forward.Y = (float)  MathHelper.Sin(MathHelper.DegreesToRadians(_rotation.X));

_forward.Z = (float) (MathHelper.Sin(MathHelper.DegreesToRadians(_rotation.Y)) *
                      MathHelper.Cos(MathHelper.DegreesToRadians(_rotation.X)));

_forward.Normalize();
_right = Vector3.Cross(_forward, _worldUp).Normalized();
_up    = Vector3.Cross(_right, _forward)

and updating the _rotation values like this:

public void RotateX(float rotation) {
    _rotation.X += rotation * _sens;
}

public void RotateY(float rotation) {
    _rotation.Y += rotation * _sens;
}

To get the rotation matrix I am simply placing the three vectors right into a Matrix3 object:

var r1 = new Vector3(_right.X, _up.X, _forward.X);
var r2 = new Vector3(_right.Y, _up.Y, _forward.Y);
var r3 = new Vector3(_right.Z, _up.Z, _forward.Z);

var mat = new Matrix3(r1, r2, r3);

if (mat != _check) {
    _orientation = Quaternion.FromMatrix(mat) * _orientation;
}
            
_orientation.Normalize();

after which I replace the orientation quaternion.

The _check property is only a matrix that represents what ought to be created if there is no such thing as a change in rotation so _orientation is not modified when it would not need to be.

This can be a gif of what occurs once I simply use the RotateX perform, so it simply rotates on some bizarre axis. Any clue what I am doing flawed? Am I initializing the rotation matrix flawed or is it one thing else completely?

enter image description here

Previous articleArray of Strings in C
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments