Add gravity scaling to rigid-bodies.
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user