Skip to content

Commit

Permalink
Add a standalone look_at function
Browse files Browse the repository at this point in the history
  • Loading branch information
h3r2tic committed Jun 1, 2022
1 parent baea884 commit 0276fb4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
17 changes: 3 additions & 14 deletions src/drivers/look_at.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::marker::PhantomData;

use glam::{Mat3, Quat, Vec3};
use glam::Vec3;

use crate::{
driver::RigDriver,
handedness::Handedness,
rig::RigUpdateParams,
transform::Transform,
util::{ExpSmoothed, ExpSmoothingParams},
util::{look_at, ExpSmoothed, ExpSmoothingParams},
};

/// Rotates the camera to point at a world-space position.
Expand Down Expand Up @@ -66,18 +66,7 @@ impl<H: Handedness> RigDriver<H> for LookAt {
},
);

let rotation = (target - params.parent.position)
.try_normalize()
.and_then(|forward| {
let right = H::right_from_up_and_forward(Vec3::Y, forward).try_normalize()?;
let up = H::up_from_right_and_forward(right, forward);
Some(Quat::from_mat3(&Mat3::from_cols(
right,
up,
forward * H::FORWARD_Z_SIGN,
)))
})
.unwrap_or_default();
let rotation = look_at::<H>(target - params.parent.position);

Transform {
position: params.parent.position,
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub mod handedness;
pub mod prelude;
pub mod rig;
pub mod transform;

mod util;
pub mod util;

pub use glam;
19 changes: 18 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use glam::{Quat, Vec3};
use glam::{Mat3, Quat, Vec3};

use crate::prelude::Handedness;

pub(crate) trait Interpolate {
fn interpolate(self, other: Self, t: f32) -> Self;
Expand Down Expand Up @@ -51,3 +53,18 @@ impl<T: Interpolate + Copy + std::fmt::Debug> ExpSmoothed<T> {
}
}
}

pub fn look_at<H: Handedness>(forward: Vec3) -> Quat {
forward
.try_normalize()
.and_then(|forward| {
let right = H::right_from_up_and_forward(Vec3::Y, forward).try_normalize()?;
let up = H::up_from_right_and_forward(right, forward);
Some(Quat::from_mat3(&Mat3::from_cols(
right,
up,
forward * H::FORWARD_Z_SIGN,
)))
})
.unwrap_or_default()
}

0 comments on commit 0276fb4

Please sign in to comment.