Skip to content

Commit

Permalink
Use mint types in public API (#18)
Browse files Browse the repository at this point in the history
* Use mint types in public API

* Minor cleanup

---------

Co-authored-by: Tomasz Stachowiak <[email protected]>
  • Loading branch information
junglie85 and h3r2tic committed Feb 25, 2024
1 parent 105afc0 commit 48d621c
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 80 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ readme = "crates-io.md"
rust-version = "1.60"

[dependencies]
glam = ">=0.21, <=0.25"
glam = { version = ">=0.21, <=0.25", features = ["mint"] }
mint = "0.5.9"

[dev-dependencies]
macroquad = "0.3"
67 changes: 50 additions & 17 deletions examples/nested_driver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// Based on https://github.com/not-fl3/macroquad/blob/97a99d00155cb7531f4432a2eb5f3c587e22f9b3/examples/3d.rs

use dolly::{driver::RigDriver, prelude::*};
use macroquad::prelude::*;
use macroquad::{
prelude::{
draw_cube, draw_cube_wires, draw_grid, draw_sphere, info, is_key_down, is_key_pressed,
set_camera, set_default_camera, vec3, Camera3D, KeyCode, Vec3, BLACK, BLUE, DARKBLUE,
DARKGREEN, GRAY, LIGHTGRAY, WHITE, YELLOW,
},
time::get_frame_time,
window::{clear_background, next_frame},
};

/// A custom camera rig which combines smoothed movement with a look-at driver.
#[derive(Debug)]
Expand All @@ -15,10 +23,7 @@ impl<H: Handedness> RigDriver<H> for MovableLookAt<H> {
}

impl<H: Handedness> MovableLookAt<H> {
pub fn from_position_target(
camera_position: dolly::glam::Vec3,
target_position: dolly::glam::Vec3,
) -> Self {
pub fn from_position_target(camera_position: glam::Vec3, target_position: glam::Vec3) -> Self {
Self(
CameraRig::builder()
// Allow moving the camera
Expand All @@ -27,18 +32,18 @@ impl<H: Handedness> MovableLookAt<H> {
.with(Smooth::new_position(1.25).predictive(true))
// Smooth the predicted movement
.with(Smooth::new_position(2.5))
.with(LookAt::new(target_position + dolly::glam::Vec3::Y).tracking_smoothness(1.25))
.with(LookAt::new(target_position + glam::Vec3::Y).tracking_smoothness(1.25))
.build(),
)
}

pub fn set_position_target(
&mut self,
camera_position: dolly::glam::Vec3,
target_position: dolly::glam::Vec3,
camera_position: glam::Vec3,
target_position: glam::Vec3,
) {
self.0.driver_mut::<Position>().position = camera_position;
self.0.driver_mut::<LookAt>().target = target_position;
self.0.driver_mut::<Position>().position = camera_position.into();
self.0.driver_mut::<LookAt>().target = target_position.into();
}
}

Expand All @@ -48,8 +53,8 @@ async fn main() {
info!("{}", "Spacebar and LShift to go up and down");
info!("{}", "C to switch between player and camera");

let mut camera_position = dolly::glam::Vec3::new(4., 3., 8.);
let mut player_position = dolly::glam::Vec3::new(2., 1.01, 2.);
let mut camera_position = glam::Vec3::new(4., 3., 8.);
let mut player_position = glam::Vec3::new(2., 1.01, 2.);

// Create a camera rig with our custom nested `MovableLookAt` driver within.
let mut camera = CameraRig::builder()
Expand Down Expand Up @@ -93,8 +98,20 @@ async fn main() {
// the two different `glam` versions to talk to each other.
set_camera(&Camera3D {
position: camera_xform.position.d2m(),
up: camera_xform.up().d2m(),
target: (camera_xform.position + camera_xform.forward()).d2m(),
up: camera_xform.up::<glam::Vec3>().d2m(),
target: (glam::Vec3::from(camera_xform.position)
+ camera_xform.forward::<glam::Vec3>())
.d2m(),
..Default::default()
});

set_camera(&Camera3D {
position: <[f32; 3]>::from(camera_xform.position).into(),
up: <[f32; 3]>::from(camera_xform.up::<glam::Vec3>()).into(),
target: <[f32; 3]>::from(
glam::Vec3::from(camera_xform.position) + camera_xform.forward::<glam::Vec3>(),
)
.into(),
..Default::default()
});

Expand All @@ -121,18 +138,34 @@ trait DollyToMacroquad {
fn d2m(self) -> Self::Target;
}

impl DollyToMacroquad for dolly::glam::Vec3 {
impl DollyToMacroquad for glam::Vec3 {
type Target = Vec3;

fn d2m(self) -> Self::Target {
<[f32; 3]>::from(self).into()
}
}

impl DollyToMacroquad for mint::Vector3<f32> {
type Target = Vec3;

fn d2m(self) -> Self::Target {
<[f32; 3]>::from(self).into()
}
}

impl DollyToMacroquad for mint::Point3<f32> {
type Target = Vec3;

fn d2m(self) -> Self::Target {
<[f32; 3]>::from(self).into()
}
}

fn get_move_input() -> dolly::glam::Vec3 {
fn get_move_input() -> glam::Vec3 {
const SPEED: f32 = 0.05;

let mut delta_pos = dolly::glam::Vec3::ZERO;
let mut delta_pos = glam::Vec3::ZERO;

if is_key_down(KeyCode::D) {
delta_pos.x += SPEED;
Expand Down
20 changes: 16 additions & 4 deletions examples/orbit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
// Based on https://github.com/not-fl3/macroquad/blob/97a99d00155cb7531f4432a2eb5f3c587e22f9b3/examples/3d.rs

use dolly::prelude::*;
use macroquad::prelude::*;
use macroquad::{
prelude::{
draw_cube, draw_cube_wires, draw_grid, draw_sphere, is_key_pressed, set_camera,
set_default_camera, vec3, Camera3D, KeyCode, BLACK, BLUE, DARKBLUE, DARKGREEN, GRAY,
LIGHTGRAY, WHITE, YELLOW,
},
text::draw_text,
time::get_frame_time,
window::{clear_background, next_frame},
};

#[macroquad::main("dolly orbit example")]
async fn main() {
// Create a smoothed orbit camera
let mut camera: CameraRig = CameraRig::builder()
.with(YawPitch::new().yaw_degrees(45.0).pitch_degrees(-30.0))
.with(Smooth::new_rotation(1.5))
.with(Arm::new(dolly::glam::Vec3::Z * 8.0))
.with(Arm::new(glam::Vec3::Z * 8.0))
.build();

loop {
Expand All @@ -30,8 +39,11 @@ async fn main() {
// the two different `glam` versions to talk to each other.
set_camera(&Camera3D {
position: <[f32; 3]>::from(camera_xform.position).into(),
up: <[f32; 3]>::from(camera_xform.up()).into(),
target: <[f32; 3]>::from(camera_xform.position + camera_xform.forward()).into(),
up: <[f32; 3]>::from(camera_xform.up::<glam::Vec3>()).into(),
target: <[f32; 3]>::from(
glam::Vec3::from(camera_xform.position) + camera_xform.forward::<glam::Vec3>(),
)
.into(),
..Default::default()
});

Expand Down
19 changes: 15 additions & 4 deletions src/drivers/arm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

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

use crate::{
driver::RigDriver, handedness::Handedness, rig::RigUpdateParams, transform::Transform,
Expand All @@ -10,21 +10,32 @@ use crate::{
#[derive(Debug)]
pub struct Arm {
///
pub offset: Vec3,
pub offset: mint::Vector3<f32>,
}

impl Arm {
///
pub fn new(offset: Vec3) -> Self {
pub fn new<V>(offset: V) -> Self
where
V: Into<mint::Vector3<f32>>,
{
let offset = offset.into();

Self { offset }
}
}

impl<H: Handedness> RigDriver<H> for Arm {
fn update(&mut self, params: RigUpdateParams<H>) -> Transform<H> {
let parent_position: Vec3 = params.parent.position.into();
let parent_rotation: Quat = params.parent.rotation.into();
let offset: Vec3 = self.offset.into();

let position = parent_position + parent_rotation * offset;

Transform {
rotation: params.parent.rotation,
position: params.parent.position + params.parent.rotation * self.offset,
position: position.into(),
phantom: PhantomData,
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/drivers/look_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct LookAt {
pub smoothness: f32,

/// The world-space position to look at
pub target: Vec3,
pub target: mint::Point3<f32>,

// The scale with which smoothing should be applied to the target position
output_offset_scale: f32,
Expand All @@ -29,7 +29,12 @@ pub struct LookAt {

impl LookAt {
///
pub fn new(target: Vec3) -> Self {
pub fn new<P>(target: P) -> Self
where
P: Into<mint::Point3<f32>>,
{
let target = target.into();

Self {
smoothness: 0.0,
output_offset_scale: 1.0,
Expand Down Expand Up @@ -57,16 +62,19 @@ impl LookAt {

impl<H: Handedness> RigDriver<H> for LookAt {
fn update(&mut self, params: RigUpdateParams<H>) -> Transform<H> {
let other: Vec3 = self.target.into();

let target = self.smoothed_target.exp_smooth_towards(
&self.target,
&other,
ExpSmoothingParams {
smoothness: self.smoothness,
output_offset_scale: self.output_offset_scale,
delta_time_seconds: params.delta_time_seconds,
},
);

let rotation = look_at::<H>(target - params.parent.position);
let parent_position: Vec3 = From::from(params.parent.position);
let rotation = look_at::<H, _, _>(target - parent_position);

Transform {
position: params.parent.position,
Expand Down
28 changes: 23 additions & 5 deletions src/drivers/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,38 @@ use crate::{
};

/// Directly sets the position of the camera
#[derive(Default, Debug)]
#[derive(Debug)]
pub struct Position {
pub position: Vec3,
pub position: mint::Point3<f32>,
}

impl Default for Position {
fn default() -> Self {
Self {
position: Vec3::default().into(),
}
}
}

impl Position {
///
pub fn new(position: Vec3) -> Self {
pub fn new<P>(position: P) -> Self
where
P: Into<mint::Point3<f32>>,
{
let position = position.into();

Self { position }
}

/// Add the specified vector to the position of this component
pub fn translate(&mut self, move_vec: Vec3) {
self.position += move_vec;
pub fn translate<V>(&mut self, move_vec: V)
where
V: Into<mint::Vector3<f32>>,
{
let position: Vec3 = From::from(self.position);
let move_vec: Vec3 = move_vec.into().into();
self.position = (position + move_vec).into();
}
}

Expand Down
19 changes: 16 additions & 3 deletions src/drivers/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@ use crate::{
};

/// Directly sets the rotation of the camera
#[derive(Default, Debug)]
#[derive(Debug)]
pub struct Rotation {
pub rotation: Quat,
pub rotation: mint::Quaternion<f32>,
}

impl Default for Rotation {
fn default() -> Self {
Self {
rotation: Quat::default().into(),
}
}
}

impl Rotation {
pub fn new(rotation: Quat) -> Self {
pub fn new<Q>(rotation: Q) -> Self
where
Q: Into<mint::Quaternion<f32>>,
{
let rotation = rotation.into();

Self { rotation }
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/drivers/smooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ impl Smooth {

impl<H: Handedness> RigDriver<H> for Smooth {
fn update(&mut self, params: RigUpdateParams<H>) -> Transform<H> {
let parent_position = From::from(params.parent.position);
let parent_rotation = From::from(params.parent.rotation);

let position = self.smoothed_position.exp_smooth_towards(
&params.parent.position,
&parent_position,
ExpSmoothingParams {
smoothness: self.position_smoothness,
output_offset_scale: self.output_offset_scale,
Expand All @@ -91,7 +94,7 @@ impl<H: Handedness> RigDriver<H> for Smooth {
);

let rotation = self.smoothed_rotation.exp_smooth_towards(
&params.parent.rotation,
&parent_rotation,
ExpSmoothingParams {
smoothness: self.rotation_smoothness,
output_offset_scale: self.output_offset_scale,
Expand All @@ -100,8 +103,8 @@ impl<H: Handedness> RigDriver<H> for Smooth {
);

Transform {
position,
rotation,
position: position.into(),
rotation: rotation.into(),
phantom: PhantomData,
}
}
Expand Down
Loading

0 comments on commit 48d621c

Please sign in to comment.