#How would I Solve the Rolling Axis Issue For Rotation?

1 messages · Page 1 of 1 (latest)

tribal sand
#

I've tried everything to prevent this from happening, and I've been reading a ton of code and watching videos trying to learn quaternions over days but i still dont think I understand what I can actually do about this problem. I have a first person camera I want to rotate horizontally and vertically, but it innevitably rolls. If this was in Vec3s I could work with it reasonably but that doesnt appear to be an option, another engine let me lock the axis of rotation but that is also not an option, and in other engines i just used parenting to solve the issue but that is also not an option. Here is the code

    mut q_camera_transform: Query<&mut Transform, (With<Camera3d>, Without<Player>)>,
    mut q_player_transform: Query<&mut Transform, (With<Player>, Without<Camera3d>)>,
    mut mouse_motion: EventReader<MouseMotion>,
) {
    let mut camera_transform = q_camera_transform.single_mut();
    let mut player_transform = q_player_transform.single_mut();

    for ev in mouse_motion.iter() { //rewrite
        camera_transform.rotate_y(ev.delta.x * TAU * -0.001);
        camera_transform.rotate_x(ev.delta.y * TAU * -0.001);
    }
    println!("{:?}",camera_transform.rotation );

    camera_transform.translation = player_transform.translation; // this line is unrelated, just makes the camera attach to the player character
}``` I'm just looking for a line that will negate the roll or somehow lock the axis, ive spent several days on this one issue. 
Thank you
ivory sorrel
#

Try to use quaternions for rotation.

#

example:

#

// Order is important to prevent unintended roll
let Vec4_temp = Vec4::new(0.5, 0.0, 0.0, 0.5).normalize(); let q1 = Quat::from_axis_angle(Vec3::Z, delta_state.yaw) * Quat::from_axis_angle(Vec3::X, delta_state.pitch) * Quat::from_vec4(Vec4_temp);

tribal sand
#

the issue is I don't know how to apply quaternions here

#

I've been looking into it for days at this point. I can't exactly say I'm retaining anything on the actual math that makes quaternions work. I can create them in code, I just can't solve the math behind them