Add gravity scaling to rigid-bodies.

This commit is contained in:
Crozet Sébastien
2021-01-06 18:09:21 +01:00
parent cc60809afc
commit 2231d0f6ea

View File

@@ -75,6 +75,7 @@ pub struct RigidBody {
pub(crate) linacc: Vector<Real>, pub(crate) linacc: Vector<Real>,
pub(crate) angacc: AngVector<Real>, pub(crate) angacc: AngVector<Real>,
pub(crate) colliders: Vec<ColliderHandle>, pub(crate) colliders: Vec<ColliderHandle>,
pub(crate) gravity_scale: Real,
/// Whether or not this rigid-body is sleeping. /// Whether or not this rigid-body is sleeping.
pub activation: ActivationStatus, pub activation: ActivationStatus,
pub(crate) joint_graph_index: RigidBodyGraphIndex, pub(crate) joint_graph_index: RigidBodyGraphIndex,
@@ -102,6 +103,7 @@ impl RigidBody {
angvel: na::zero(), angvel: na::zero(),
linacc: Vector::zeros(), linacc: Vector::zeros(),
angacc: na::zero(), angacc: na::zero(),
gravity_scale: 1.0,
linear_damping: 0.0, linear_damping: 0.0,
angular_damping: 0.0, angular_damping: 0.0,
colliders: Vec::new(), colliders: Vec::new(),
@@ -129,7 +131,7 @@ impl RigidBody {
pub(crate) fn integrate_accelerations(&mut self, dt: Real, gravity: Vector<Real>) { pub(crate) fn integrate_accelerations(&mut self, dt: Real, gravity: Vector<Real>) {
if self.mass_properties.inv_mass != 0.0 { 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; self.angvel += self.angacc * dt;
// Reset the accelerations. // Reset the accelerations.
@@ -199,6 +201,21 @@ impl RigidBody {
&self.predicted_position &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. /// Adds a collider to this rigid-body.
pub(crate) fn add_collider(&mut self, handle: ColliderHandle, coll: &Collider) { pub(crate) fn add_collider(&mut self, handle: ColliderHandle, coll: &Collider) {
self.changes.set( self.changes.set(
@@ -552,6 +569,7 @@ pub struct RigidBodyBuilder {
position: Isometry<Real>, position: Isometry<Real>,
linvel: Vector<Real>, linvel: Vector<Real>,
angvel: AngVector<Real>, angvel: AngVector<Real>,
gravity_scale: Real,
linear_damping: Real, linear_damping: Real,
angular_damping: Real, angular_damping: Real,
body_status: BodyStatus, body_status: BodyStatus,
@@ -569,6 +587,7 @@ impl RigidBodyBuilder {
position: Isometry::identity(), position: Isometry::identity(),
linvel: Vector::zeros(), linvel: Vector::zeros(),
angvel: na::zero(), angvel: na::zero(),
gravity_scale: 1.0,
linear_damping: 0.0, linear_damping: 0.0,
angular_damping: 0.0, angular_damping: 0.0,
body_status, body_status,
@@ -595,6 +614,12 @@ impl RigidBodyBuilder {
Self::new(BodyStatus::Dynamic) 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. /// Sets the initial translation of the rigid-body to be created.
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
pub fn translation(mut self, x: Real, y: Real) -> Self { pub fn translation(mut self, x: Real, y: Real) -> Self {
@@ -825,6 +850,7 @@ impl RigidBodyBuilder {
rb.mass_properties = self.mass_properties; rb.mass_properties = self.mass_properties;
rb.linear_damping = self.linear_damping; rb.linear_damping = self.linear_damping;
rb.angular_damping = self.angular_damping; rb.angular_damping = self.angular_damping;
rb.gravity_scale = self.gravity_scale;
rb.flags = self.flags; rb.flags = self.flags;
if self.can_sleep && self.sleeping { if self.can_sleep && self.sleeping {