From 2231d0f6ea994cbe768ea3a9d60ad29d9a383c9b Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Wed, 6 Jan 2021 18:09:21 +0100 Subject: Add gravity scaling to rigid-bodies. --- src/dynamics/rigid_body.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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, pub(crate) angacc: AngVector, pub(crate) colliders: Vec, + 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) { 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, linvel: Vector, angvel: AngVector, + 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 { -- cgit