Skip to content

Commit

Permalink
Make Motor config serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Aug 2, 2024
1 parent df1f036 commit 407a04b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 49 deletions.
15 changes: 9 additions & 6 deletions examples/vehicle/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions examples/vehicle/data/raceFuture.ron
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
],
Expand Down
44 changes: 14 additions & 30 deletions examples/vehicle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
},
);
Expand All @@ -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
Expand Down Expand Up @@ -315,7 +298,8 @@ impl Game {
suspender: None,
steer_joint: None,
}
});
},
);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ pub struct Object {
pub additional_mass: Option<AdditionalMass>,
}

#[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
}
Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ops::Range<f32>>,
pub motor: Option<MotorDesc>,
pub motor: Option<config::Motor>,
}

impl FreedomAxis {
pub const FREE: Self = Self {
limits: None,
motor: None,
};
pub const ALL_FREE: mint::Vector3<Option<Self>> = mint::Vector3 {
x: Some(Self::FREE),
y: Some(Self::FREE),
z: Some(Self::FREE),
};
}

#[derive(Clone, Debug, PartialEq)]
Expand Down

0 comments on commit 407a04b

Please sign in to comment.