use between_vectors from cgmath rotation_matrix_from_dir_to_dir for
pub(crate) use cgmath::ortho;
pub(crate) use cgmath::perspective;
pub use cgmath::prelude::*;
-use cgmath::{Deg, Matrix2, Matrix3, Matrix4, Point3, Quaternion, Rad, Vector2, Vector3, Vector4};
+use cgmath::{
+ Basis3, Deg, Matrix2, Matrix3, Matrix4, Point3, Quaternion, Rad, Vector2, Vector3, Vector4,
+};
pub type Vec2 = Vector2<f32>;
pub type Vec3 = Vector3<f32>;
@@ -111,34 +113,5 @@ pub fn radians(v: f32) -> Radians {
}
pub fn rotation_matrix_from_dir_to_dir(source_dir: Vec3, target_dir: Vec3) -> Mat4 {
- let c = source_dir.dot(target_dir);
- if c > 0.99999 {
- return Mat4::identity();
- }
- if c < -0.99999 {
- return Mat4::from_scale(-1.0);
- }
- let axis = source_dir.cross(target_dir).normalize();
-
- let s = (1.0 - c * c).sqrt();
- let oc = 1.0 - c;
- return Mat4::new(
- oc * axis.x * axis.x + c,
- oc * axis.x * axis.y - axis.z * s,
- oc * axis.z * axis.x + axis.y * s,
- 0.0,
- oc * axis.x * axis.y + axis.z * s,
- oc * axis.y * axis.y + c,
- oc * axis.y * axis.z - axis.x * s,
- 0.0,
- oc * axis.z * axis.x - axis.y * s,
- oc * axis.y * axis.z + axis.x * s,
- oc * axis.z * axis.z + c,
- 0.0,
- 0.0,
- 0.0,
- 0.0,
- 1.0,
- )
- .transpose();
+ Mat4::from(Mat3::from(Basis3::between_vectors(source_dir, target_dir)))
}
