fu5ha/ultraviolet

Mat4::from_euler_angles appears to be producing incorrect rotations

alexgladd opened this issue · 2 comments

As referenced here in #130, Mat4::from_euler_angles(roll, pitch, yaw) appears to actually be implementing Mat4::from_euler_angles(pitch, yaw, roll).

I'm seeing this same issue in something I'm working on where I'm using Mat4::from_euler_angles to build a rotation matrix as part of a larger computation for a camera view projection matrix. If I do a "roll", the scene appears as if the camera were pitched. If I do a "pitch", the scene appears as if the camera were yawed. If I do a "yaw" the scene appears as if the camera were rolled.

I'm calculating the VP matrix like:

// roll, pitch, and yaw are provided as radians
let transform = Mat4::from_translation(self.position) * Mat4::from_euler_angles(roll, pitch, yaw);
// invert the transform to get a camera view matrix
let view = transform.inversed();
// add the projection to get a camera view projection matrix, where projection is created
// using ultraviolet::projection::orthographic_wgpu_dx
let view_projection = projection * view;

Am I doing something wrong or is this a bug?

Note that if I use the individual rotations like Mat4f::from_rotation_z(roll) I get the expected results.

fu5ha commented

Sounds like it is indeed a bug :)

Thanks for the confirmation. I also made a super simple test that demonstrates the issue:

use std::f32::consts::PI;
use ultraviolet::Mat4;

fn main() {
    let roll = PI / 4.;
    let m1 = dbg!(Mat4::from_euler_angles(roll, 0., 0.));
    let m2 = dbg!(Mat4::from_rotation_z(roll));

    assert_eq!(m1, m2);
}

I'm not sure I understand enough of what's going on in the code to fix it, but I'll take a look later tonight.