From 407a04b2d6fdf521391a8fa16b9d003a2247e5d0 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 1 Aug 2024 21:50:09 -0700 Subject: [PATCH] Make Motor config serializable --- examples/vehicle/config.rs | 15 ++++++---- examples/vehicle/data/raceFuture.ron | 13 ++++---- examples/vehicle/main.rs | 44 +++++++++------------------- src/config.rs | 7 +++++ src/lib.rs | 21 ++++++++----- 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/examples/vehicle/config.rs b/examples/vehicle/config.rs index bed76bf9..5728fa42 100644 --- a/examples/vehicle/config.rs +++ b/examples/vehicle/config.rs @@ -10,11 +10,10 @@ pub struct Wheel { pub collider: blade::config::Collider, } -#[derive(Default, serde::Deserialize)] +#[derive(serde::Deserialize)] pub struct Motor { - pub limit: f32, - pub stiffness: f32, - pub damping: f32, + pub visual: blade::config::Visual, + pub collider: blade::config::Collider, } #[derive(serde::Deserialize)] @@ -26,9 +25,13 @@ pub struct Axle { /// Forward offset from the body. pub z: f32, #[serde(default)] - pub suspension: Motor, + pub max_steering_angle: f32, + #[serde(default)] + pub max_suspension_offset: f32, + #[serde(default)] + pub suspension: blade::config::Motor, #[serde(default)] - pub steering: Motor, + pub steering: blade::config::Motor, } fn default_additional_mass() -> blade::config::AdditionalMass { diff --git a/examples/vehicle/data/raceFuture.ron b/examples/vehicle/data/raceFuture.ron index 80b3c318..f276b0e7 100644 --- a/examples/vehicle/data/raceFuture.ron +++ b/examples/vehicle/data/raceFuture.ron @@ -40,25 +40,28 @@ x_wheels: [-0.5, 0.5], y: -0.1, z: 0.7, - suspension: ( - limit: 0.02, + max_steering_angle: 30, + max_suspension_offset: 0.02, + steering: ( stiffness: 100000, damping: 10000, + max_force: 1000, ), - steering: ( - limit: 30, + suspension: ( stiffness: 100000, damping: 10000, + max_force: 1000, ), ), ( x_wheels: [-0.5, 0.5], y: -0.1, z: -0.8, + max_suspension_offset: 0.02, suspension: ( - limit: 0.03, stiffness: 100000, damping: 10000, + max_force: 1000, ), ), ], diff --git a/examples/vehicle/main.rs b/examples/vehicle/main.rs index 160deb78..56a235ee 100644 --- a/examples/vehicle/main.rs +++ b/examples/vehicle/main.rs @@ -183,7 +183,7 @@ impl Game { let wheel_angular_freedoms = mint::Vector3 { x: Some(blade::FreedomAxis { limits: None, - motor: Some(blade::MotorDesc { + motor: Some(blade::config::Motor { stiffness: 0.0, damping: veh_config.drive_factor, max_force: 1000.0, @@ -193,10 +193,9 @@ impl Game { z: None, }; - vehicle - .wheels - .push(if ac.steering.limit > 0.0 || ac.suspension.limit > 0.0 { - let max_angle = ac.steering.limit.to_radians(); + vehicle.wheels.push( + if ac.max_steering_angle > 0.0 || ac.max_suspension_offset > 0.0 { + let max_angle = ac.max_steering_angle.to_radians(); let suspender_handle = engine.add_object( &suspender_config, blade::Transform { @@ -216,14 +215,10 @@ impl Game { }, linear: mint::Vector3 { x: None, - y: if ac.suspension.limit > 0.0 { + y: if ac.max_suspension_offset > 0.0 { Some(blade::FreedomAxis { - limits: Some(0.0..ac.suspension.limit), - motor: Some(blade::MotorDesc { - stiffness: ac.suspension.stiffness, - damping: ac.suspension.damping, - max_force: 1000.0, - }), + limits: Some(0.0..ac.max_suspension_offset), + motor: Some(ac.suspension), }) } else { None @@ -232,14 +227,10 @@ impl Game { }, angular: mint::Vector3 { x: None, - y: if ac.steering.limit > 0.0 { + y: if ac.max_steering_angle > 0.0 { Some(blade::FreedomAxis { limits: Some(-max_angle..max_angle), - motor: Some(blade::MotorDesc { - stiffness: ac.steering.stiffness, - damping: ac.steering.damping, - max_force: 1000.0, - }), + motor: Some(ac.steering), }) } else { None @@ -267,16 +258,8 @@ impl Game { vehicle.body_handle, wheel_handle, blade::JointDesc { - linear: mint::Vector3 { - x: Some(Default::default()), - y: Some(Default::default()), - z: Some(Default::default()), - }, - angular: mint::Vector3 { - x: Some(Default::default()), - y: Some(Default::default()), - z: Some(Default::default()), - }, + linear: blade::FreedomAxis::ALL_FREE, + angular: blade::FreedomAxis::ALL_FREE, ..Default::default() }, ); @@ -285,7 +268,7 @@ impl Game { object: wheel_handle, spin_joint: wheel_joint, suspender: Some(suspender_handle), - steer_joint: if ac.steering.limit > 0.0 { + steer_joint: if ac.max_steering_angle > 0.0 { Some(suspension_joint) } else { None @@ -315,7 +298,8 @@ impl Game { suspender: None, steer_joint: None, } - }); + }, + ); } } diff --git a/src/config.rs b/src/config.rs index dfab3c2e..0908f54c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -102,6 +102,13 @@ pub struct Object { pub additional_mass: Option, } +#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize)] +pub struct Motor { + pub stiffness: f32, + pub damping: f32, + pub max_force: f32, +} + fn default_time_step() -> f32 { 0.01 } diff --git a/src/lib.rs b/src/lib.rs index bd52dc4e..5054a109 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,17 +130,22 @@ pub enum JointHandle { Hard(#[doc(hidden)] rapier3d::dynamics::MultibodyJointHandle), } -#[derive(Clone, Debug, PartialEq)] -pub struct MotorDesc { - pub stiffness: f32, - pub damping: f32, - pub max_force: f32, -} - #[derive(Clone, Debug, Default, PartialEq)] pub struct FreedomAxis { pub limits: Option>, - pub motor: Option, + pub motor: Option, +} + +impl FreedomAxis { + pub const FREE: Self = Self { + limits: None, + motor: None, + }; + pub const ALL_FREE: mint::Vector3> = mint::Vector3 { + x: Some(Self::FREE), + y: Some(Self::FREE), + z: Some(Self::FREE), + }; } #[derive(Clone, Debug, PartialEq)]