diff options
| author | Crozet Sébastien <developer@crozet.re> | 2021-01-06 18:09:21 +0100 |
|---|---|---|
| committer | Crozet Sébastien <developer@crozet.re> | 2021-01-06 18:09:21 +0100 |
| commit | 2231d0f6ea994cbe768ea3a9d60ad29d9a383c9b (patch) | |
| tree | f5ecfa270eb7aa0bb25cc1bbf1e1ea13724d3f10 | |
| parent | cc60809afca7137bc192d56734ce12b385c32eda (diff) | |
| download | rapier-2231d0f6ea994cbe768ea3a9d60ad29d9a383c9b.tar.gz rapier-2231d0f6ea994cbe768ea3a9d60ad29d9a383c9b.tar.bz2 rapier-2231d0f6ea994cbe768ea3a9d60ad29d9a383c9b.zip | |
Add gravity scaling to rigid-bodies.
| -rw-r--r-- | src/dynamics/rigid_body.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/dynamics/rigid_body.rs b/src/dynamics/rigid_body.rs index 683cca8..1574100 100644 --- a/src/dynamics/rigid_body.rs +++ b/src/dynamics/rigid_body.rs @@ -75,6 +75,7 @@ pub struct RigidBody { pub(crate) linacc: Vector<Real>, pub(crate) angacc: AngVector<Real>, pub(crate) colliders: Vec<ColliderHandle>, + pub(crate) gravity_scale: Real, /// Whether or not this rigid-body is sleeping. pub activation: ActivationStatus, pub(crate) joint_graph_index: RigidBodyGraphIndex, @@ -102,6 +103,7 @@ impl RigidBody { angvel: na::zero(), linacc: Vector::zeros(), angacc: na::zero(), + gravity_scale: 1.0, linear_damping: 0.0, angular_damping: 0.0, colliders: Vec::new(), @@ -129,7 +131,7 @@ impl RigidBody { pub(crate) fn integrate_accelerations(&mut self, dt: Real, gravity: Vector<Real>) { if self.mass_properties.inv_mass != 0.0 { - self.linvel += (gravity + self.linacc) * dt; + self.linvel += (gravity * self.gravity_scale + self.linacc) * dt; self.angvel += self.angacc * dt; // Reset the accelerations. @@ -199,6 +201,21 @@ impl RigidBody { &self.predicted_position } + /// The scale factor applied to the gravity affecting this rigid-body. + pub fn gravity_scale(&self) -> Real { + self.gravity_scale + } + + /// Sets the gravity scale facter for this rigid-body. + pub fn set_gravity_scale(&mut self, scale: Real, wake_up: bool) { + if wake_up && self.activation.sleeping { + self.changes.insert(RigidBodyChanges::SLEEP); + self.activation.sleeping = false; + } + + self.gravity_scale = scale; + } + /// Adds a collider to this rigid-body. pub(crate) fn add_collider(&mut self, handle: ColliderHandle, coll: &Collider) { self.changes.set( @@ -552,6 +569,7 @@ pub struct RigidBodyBuilder { position: Isometry<Real>, linvel: Vector<Real>, angvel: AngVector<Real>, + gravity_scale: Real, linear_damping: Real, angular_damping: Real, body_status: BodyStatus, @@ -569,6 +587,7 @@ impl RigidBodyBuilder { position: Isometry::identity(), linvel: Vector::zeros(), angvel: na::zero(), + gravity_scale: 1.0, linear_damping: 0.0, angular_damping: 0.0, body_status, @@ -595,6 +614,12 @@ impl RigidBodyBuilder { Self::new(BodyStatus::Dynamic) } + /// Sets the scale applied to the gravity force affecting the rigid-body to be created. + pub fn gravity_scale(mut self, x: Real) -> Self { + self.gravity_scale = x; + self + } + /// Sets the initial translation of the rigid-body to be created. #[cfg(feature = "dim2")] pub fn translation(mut self, x: Real, y: Real) -> Self { @@ -825,6 +850,7 @@ impl RigidBodyBuilder { rb.mass_properties = self.mass_properties; rb.linear_damping = self.linear_damping; rb.angular_damping = self.angular_damping; + rb.gravity_scale = self.gravity_scale; rb.flags = self.flags; if self.can_sleep && self.sleeping { |
