[ad_1]
I’m attempting to rotate a dice utilizing a quaternion matrix with WGPU. Nevertheless, every time I attempt to rotate it, I find yourself with
I’m utilizing the matrix from https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation.
I’ve additionally tried multiplying the matrix and the vector within the unsuitable order, which in some events, leads to one thing wanting like a dice, however with a nook oddly stretched out.
My shader code is
fn vs_main(
mannequin: VertexInput,
entity: EntityInput,
) -> VertexOutput {
let rotation = mat4x4<f32> (
vec4<f32>(entity.rotation_one, 0.0),
vec4<f32>(entity.rotation_two, 0.0),
vec4<f32>(entity.rotation_three, 0.0),
vec4<f32>(0.0,0.0,0.0,1.0),
);
let translation = mat4x4<f32> (
vec4<f32>(1.0,0.0,0.0,0.0),
vec4<f32>(0.0,1.0,0.0,0.0),
vec4<f32>(0.0,0.0,1.0,0.0),
vec4<f32>(0.0,0.0,5.0,1.0),
);
let transformation = translation * rotation;
var out: VertexOutput;
out.tex_pos = mannequin.tex_pos;
out.clip_position = camera_mat * transformation * vec4<f32>(mannequin.place, 1.0);
return out;
}
and the rotation matrix I’m utilizing is
let w = (self.angle.to_f32().unwrap().to_radians() / 2.0).cos();
let angle = self.angle.to_f32().unwrap().to_radians();
let x = self.axis.x.to_f32().unwrap() * (angle / 2.0).sin();
let y = self.axis.y.to_f32().unwrap() * (angle / 2.0).sin();
let z = self.axis.z.to_f32().unwrap() * (angle / 2.0).sin();
// https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
[
[
w.powi(2) + x.powi(2) - y.powi(2) - z.powi(2),
2.0 * x * y + 2.0 * w * z,
2.0 * x * z - 2.0 * w * y,
],
[
2.0 * x * y - 2.0 * w * z,
w.powi(2) - x.powi(2) + y.powi(2) - z.powi(2),
2.0 * y * z + 2.0 * w * x,
],
[
2.0 * x * z + 2.0 * w * y,
2.0 * y * z - 2.0 * w * x,
w.powi(2) - x.powi(2) - y.powi(2) + z.powi(2),
],
]
The WGSL shader code is column-major.
EDIT: I’ve corrected the errors in my matrix as identified by ratchet freak.
[ad_2]