aboutsummaryrefslogtreecommitdiff
path: root/src/dynamics/rigid_body.rs
diff options
context:
space:
mode:
authorCrozet Sébastien <developer@crozet.re>2021-01-04 15:14:25 +0100
committerCrozet Sébastien <developer@crozet.re>2021-01-04 15:14:25 +0100
commitaa61fe65e3ff0289ecab57b4053a3410cf6d4a87 (patch)
treea2ab513f43d779e4eb1c0edcd2a6e734b3fa4470 /src/dynamics/rigid_body.rs
parenta1aa8855f76168d8af14244a54c9f28d15696342 (diff)
downloadrapier-aa61fe65e3ff0289ecab57b4053a3410cf6d4a87.tar.gz
rapier-aa61fe65e3ff0289ecab57b4053a3410cf6d4a87.tar.bz2
rapier-aa61fe65e3ff0289ecab57b4053a3410cf6d4a87.zip
Add support of 64-bits reals.
Diffstat (limited to 'src/dynamics/rigid_body.rs')
-rw-r--r--src/dynamics/rigid_body.rs122
1 files changed, 62 insertions, 60 deletions
diff --git a/src/dynamics/rigid_body.rs b/src/dynamics/rigid_body.rs
index 5128b6f..683cca8 100644
--- a/src/dynamics/rigid_body.rs
+++ b/src/dynamics/rigid_body.rs
@@ -2,7 +2,9 @@ use crate::dynamics::MassProperties;
use crate::geometry::{
Collider, ColliderHandle, ColliderSet, InteractionGraph, RigidBodyGraphIndex,
};
-use crate::math::{AngVector, AngularInertia, Isometry, Point, Rotation, Translation, Vector};
+use crate::math::{
+ AngVector, AngularInertia, Isometry, Point, Real, Rotation, Translation, Vector,
+};
use crate::utils::{self, WCross, WDot};
use num::Zero;
@@ -54,24 +56,24 @@ bitflags::bitflags! {
#[derive(Debug, Clone)]
pub struct RigidBody {
/// The world-space position of the rigid-body.
- pub(crate) position: Isometry<f32>,
- pub(crate) predicted_position: Isometry<f32>,
+ pub(crate) position: Isometry<Real>,
+ pub(crate) predicted_position: Isometry<Real>,
/// The local mass properties of the rigid-body.
pub(crate) mass_properties: MassProperties,
/// The world-space center of mass of the rigid-body.
- pub world_com: Point<f32>,
+ pub world_com: Point<Real>,
/// The square-root of the inverse angular inertia tensor of the rigid-body.
- pub world_inv_inertia_sqrt: AngularInertia<f32>,
+ pub world_inv_inertia_sqrt: AngularInertia<Real>,
/// The linear velocity of the rigid-body.
- pub(crate) linvel: Vector<f32>,
+ pub(crate) linvel: Vector<Real>,
/// The angular velocity of the rigid-body.
- pub(crate) angvel: AngVector<f32>,
+ pub(crate) angvel: AngVector<Real>,
/// Damping factor for gradually slowing down the translational motion of the rigid-body.
- pub linear_damping: f32,
+ pub linear_damping: Real,
/// Damping factor for gradually slowing down the angular motion of the rigid-body.
- pub angular_damping: f32,
- pub(crate) linacc: Vector<f32>,
- pub(crate) angacc: AngVector<f32>,
+ pub angular_damping: Real,
+ pub(crate) linacc: Vector<Real>,
+ pub(crate) angacc: AngVector<Real>,
pub(crate) colliders: Vec<ColliderHandle>,
/// Whether or not this rigid-body is sleeping.
pub activation: ActivationStatus,
@@ -125,7 +127,7 @@ impl RigidBody {
self.active_set_timestamp = 0;
}
- pub(crate) fn integrate_accelerations(&mut self, dt: f32, gravity: Vector<f32>) {
+ 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.angvel += self.angacc * dt;
@@ -184,7 +186,7 @@ impl RigidBody {
/// The mass of this rigid body.
///
/// Returns zero if this rigid body has an infinite mass.
- pub fn mass(&self) -> f32 {
+ pub fn mass(&self) -> Real {
utils::inv(self.mass_properties.inv_mass)
}
@@ -193,7 +195,7 @@ impl RigidBody {
/// If this rigid-body is kinematic this value is set by the `set_next_kinematic_position`
/// method and is used for estimating the kinematic body velocity at the next timestep.
/// For non-kinematic bodies, this value is currently unspecified.
- pub fn predicted_position(&self) -> &Isometry<f32> {
+ pub fn predicted_position(&self) -> &Isometry<Real> {
&self.predicted_position
}
@@ -311,13 +313,13 @@ impl RigidBody {
!self.linvel.is_zero() || !self.angvel.is_zero()
}
- fn integrate_velocity(&self, dt: f32) -> Isometry<f32> {
+ fn integrate_velocity(&self, dt: Real) -> Isometry<Real> {
let com = &self.position * self.mass_properties.local_com;
let shift = Translation::from(com.coords);
shift * Isometry::new(self.linvel * dt, self.angvel * dt) * shift.inverse()
}
- pub(crate) fn integrate(&mut self, dt: f32) {
+ pub(crate) fn integrate(&mut self, dt: Real) {
// TODO: do we want to apply damping before or after the velocity integration?
self.linvel *= 1.0 / (1.0 + dt * self.linear_damping);
self.angvel *= 1.0 / (1.0 + dt * self.angular_damping);
@@ -326,19 +328,19 @@ impl RigidBody {
}
/// The linear velocity of this rigid-body.
- pub fn linvel(&self) -> &Vector<f32> {
+ pub fn linvel(&self) -> &Vector<Real> {
&self.linvel
}
/// The angular velocity of this rigid-body.
#[cfg(feature = "dim2")]
- pub fn angvel(&self) -> f32 {
+ pub fn angvel(&self) -> Real {
self.angvel
}
/// The angular velocity of this rigid-body.
#[cfg(feature = "dim3")]
- pub fn angvel(&self) -> &Vector<f32> {
+ pub fn angvel(&self) -> &Vector<Real> {
&self.angvel
}
@@ -346,7 +348,7 @@ impl RigidBody {
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
- pub fn set_linvel(&mut self, linvel: Vector<f32>, wake_up: bool) {
+ pub fn set_linvel(&mut self, linvel: Vector<Real>, wake_up: bool) {
self.linvel = linvel;
if self.is_dynamic() && wake_up {
@@ -359,7 +361,7 @@ impl RigidBody {
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[cfg(feature = "dim2")]
- pub fn set_angvel(&mut self, angvel: f32, wake_up: bool) {
+ pub fn set_angvel(&mut self, angvel: Real, wake_up: bool) {
self.angvel = angvel;
if self.is_dynamic() && wake_up {
@@ -372,7 +374,7 @@ impl RigidBody {
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
#[cfg(feature = "dim3")]
- pub fn set_angvel(&mut self, angvel: Vector<f32>, wake_up: bool) {
+ pub fn set_angvel(&mut self, angvel: Vector<Real>, wake_up: bool) {
self.angvel = angvel;
if self.is_dynamic() && wake_up {
@@ -381,7 +383,7 @@ impl RigidBody {
}
/// The world-space position of this rigid-body.
- pub fn position(&self) -> &Isometry<f32> {
+ pub fn position(&self) -> &Isometry<Real> {
&self.position
}
@@ -394,7 +396,7 @@ impl RigidBody {
///
/// If `wake_up` is `true` then the rigid-body will be woken up if it was
/// put to sleep because it did not move for a while.
- pub fn set_position(&mut self, pos: Isometry<f32>, wake_up: bool) {
+ pub fn set_position(&mut self, pos: Isometry<Real>, wake_up: bool) {
self.changes.insert(RigidBodyChanges::POSITION);
self.set_position_internal(pos);
@@ -404,7 +406,7 @@ impl RigidBody {
}
}
- pub(crate) fn set_position_internal(&mut self, pos: Isometry<f32>) {
+ pub(crate) fn set_position_internal(&mut self, pos: Isometry<Real>) {
self.position = pos;
// TODO: update the predicted position for dynamic bodies too?
@@ -414,13 +416,13 @@ impl RigidBody {
}
/// If this rigid body is kinematic, sets its future position after the next timestep integration.
- pub fn set_next_kinematic_position(&mut self, pos: Isometry<f32>) {
+ pub fn set_next_kinematic_position(&mut self, pos: Isometry<Real>) {
if self.is_kinematic() {
self.predicted_position = pos;
}
}
- pub(crate) fn compute_velocity_from_predicted_position(&mut self, inv_dt: f32) {
+ pub(crate) fn compute_velocity_from_predicted_position(&mut self, inv_dt: Real) {
let dpos = self.predicted_position * self.position.inverse();
#[cfg(feature = "dim2")]
{
@@ -433,7 +435,7 @@ impl RigidBody {
self.linvel = dpos.translation.vector * inv_dt;
}
- pub(crate) fn update_predicted_position(&mut self, dt: f32) {
+ pub(crate) fn update_predicted_position(&mut self, dt: Real) {
self.predicted_position = self.integrate_velocity(dt) * self.position;
}
@@ -448,7 +450,7 @@ impl RigidBody {
* Application of forces/impulses.
*/
/// Applies a force at the center-of-mass of this rigid-body.
- pub fn apply_force(&mut self, force: Vector<f32>, wake_up: bool) {
+ pub fn apply_force(&mut self, force: Vector<Real>, wake_up: bool) {
if self.body_status == BodyStatus::Dynamic {
self.linacc += force * self.mass_properties.inv_mass;
@@ -459,7 +461,7 @@ impl RigidBody {
}
/// Applies an impulse at the center-of-mass of this rigid-body.
- pub fn apply_impulse(&mut self, impulse: Vector<f32>, wake_up: bool) {
+ pub fn apply_impulse(&mut self, impulse: Vector<Real>, wake_up: bool) {
if self.body_status == BodyStatus::Dynamic {
self.linvel += impulse * self.mass_properties.inv_mass;
@@ -471,7 +473,7 @@ impl RigidBody {
/// Applies a torque at the center-of-mass of this rigid-body.
#[cfg(feature = "dim2")]
- pub fn apply_torque(&mut self, torque: f32, wake_up: bool) {
+ pub fn apply_torque(&mut self, torque: Real, wake_up: bool) {
if self.body_status == BodyStatus::Dynamic {
self.angacc += self.world_inv_inertia_sqrt * (self.world_inv_inertia_sqrt * torque);
@@ -483,7 +485,7 @@ impl RigidBody {
/// Applies a torque at the center-of-mass of this rigid-body.
#[cfg(feature = "dim3")]
- pub fn apply_torque(&mut self, torque: Vector<f32>, wake_up: bool) {
+ pub fn apply_torque(&mut self, torque: Vector<Real>, wake_up: bool) {
if self.body_status == BodyStatus::Dynamic {
self.angacc += self.world_inv_inertia_sqrt * (self.world_inv_inertia_sqrt * torque);
@@ -495,7 +497,7 @@ impl RigidBody {
/// Applies an impulsive torque at the center-of-mass of this rigid-body.
#[cfg(feature = "dim2")]
- pub fn apply_torque_impulse(&mut self, torque_impulse: f32, wake_up: bool) {
+ pub fn apply_torque_impulse(&mut self, torque_impulse: Real, wake_up: bool) {
if self.body_status == BodyStatus::Dynamic {
self.angvel +=
self.world_inv_inertia_sqrt * (self.world_inv_inertia_sqrt * torque_impulse);
@@ -508,7 +510,7 @@ impl RigidBody {
/// Applies an impulsive torque at the center-of-mass of this rigid-body.
#[cfg(feature = "dim3")]
- pub fn apply_torque_impulse(&mut self, torque_impulse: Vector<f32>, wake_up: bool) {
+ pub fn apply_torque_impulse(&mut self, torque_impulse: Vector<Real>, wake_up: bool) {
if self.body_status == BodyStatus::Dynamic {
self.angvel +=
self.world_inv_inertia_sqrt * (self.world_inv_inertia_sqrt * torque_impulse);
@@ -520,7 +522,7 @@ impl RigidBody {
}
/// Applies a force at the given world-space point of this rigid-body.
- pub fn apply_force_at_point(&mut self, force: Vector<f32>, point: Point<f32>, wake_up: bool) {
+ pub fn apply_force_at_point(&mut self, force: Vector<Real>, point: Point<Real>, wake_up: bool) {
let torque = (point - self.world_com).gcross(force);
self.apply_force(force, wake_up);
self.apply_torque(torque, wake_up);
@@ -529,8 +531,8 @@ impl RigidBody {
/// Applies an impulse at the given world-space point of this rigid-body.
pub fn apply_impulse_at_point(
&mut self,
- impulse: Vector<f32>,
- point: Point<f32>,
+ impulse: Vector<Real>,
+ point: Point<Real>,
wake_up: bool,
) {
let torque_impulse = (point - self.world_com).gcross(impulse);
@@ -539,7 +541,7 @@ impl RigidBody {
}
/// The velocity of the given world-space point on this rigid-body.
- pub fn velocity_at_point(&self, point: &Point<f32>) -> Vector<f32> {
+ pub fn velocity_at_point(&self, point: &Point<Real>) -> Vector<Real> {
let dpt = point - self.world_com;
self.linvel + self.angvel.gcross(dpt)
}
@@ -547,11 +549,11 @@ impl RigidBody {
/// A builder for rigid-bodies.
pub struct RigidBodyBuilder {
- position: Isometry<f32>,
- linvel: Vector<f32>,
- angvel: AngVector<f32>,
- linear_damping: f32,
- angular_damping: f32,
+ position: Isometry<Real>,
+ linvel: Vector<Real>,
+ angvel: AngVector<Real>,
+ linear_damping: Real,
+ angular_damping: Real,
body_status: BodyStatus,
flags: RigidBodyFlags,
mass_properties: MassProperties,
@@ -595,7 +597,7 @@ impl RigidBodyBuilder {
/// Sets the initial translation of the rigid-body to be created.
#[cfg(feature = "dim2")]
- pub fn translation(mut self, x: f32, y: f32) -> Self {
+ pub fn translation(mut self, x: Real, y: Real) -> Self {
self.position.translation.x = x;
self.position.translation.y = y;
self
@@ -603,7 +605,7 @@ impl RigidBodyBuilder {
/// Sets the initial translation of the rigid-body to be created.
#[cfg(feature = "dim3")]
- pub fn translation(mut self, x: f32, y: f32, z: f32) -> Self {
+ pub fn translation(mut self, x: Real, y: Real, z: Real) -> Self {
self.position.translation.x = x;
self.position.translation.y = y;
self.position.translation.z = z;
@@ -611,13 +613,13 @@ impl RigidBodyBuilder {
}
/// Sets the initial orientation of the rigid-body to be created.
- pub fn rotation(mut self, angle: AngVector<f32>) -> Self {
+ pub fn rotation(mut self, angle: AngVector<Real>) -> Self {
self.position.rotation = Rotation::new(angle);
self
}
/// Sets the initial position (translation and orientation) of the rigid-body to be created.
- pub fn position(mut self, pos: Isometry<f32>) -> Self {
+ pub fn position(mut self, pos: Isometry<Real>) -> Self {
self.position = pos;
self
}
@@ -675,7 +677,7 @@ impl RigidBodyBuilder {
/// will depends on the initial mass set by this method to which is added
/// the contributions of all the colliders with non-zero density attached to
/// this rigid-body.
- pub fn mass(mut self, mass: f32, colliders_contribution_enabled: bool) -> Self {
+ pub fn mass(mut self, mass: Real, colliders_contribution_enabled: bool) -> Self {
self.mass_properties.inv_mass = utils::inv(mass);
self.flags.set(
RigidBodyFlags::IGNORE_COLLIDER_MASS,
@@ -696,7 +698,7 @@ impl RigidBodyBuilder {
#[cfg(feature = "dim2")]
pub fn principal_angular_inertia(
mut self,
- inertia: f32,
+ inertia: Real,
colliders_contribution_enabled: bool,
) -> Self {
self.mass_properties.inv_principal_inertia_sqrt = utils::inv(inertia);
@@ -712,7 +714,7 @@ impl RigidBodyBuilder {
/// Use `self.principal_angular_inertia` instead.
#[cfg(feature = "dim2")]
#[deprecated(note = "renamed to `principal_angular_inertia`.")]
- pub fn principal_inertia(self, inertia: f32, colliders_contribution_enabled: bool) -> Self {
+ pub fn principal_inertia(self, inertia: Real, colliders_contribution_enabled: bool) -> Self {
self.principal_angular_inertia(inertia, colliders_contribution_enabled)
}
@@ -731,7 +733,7 @@ impl RigidBodyBuilder {
#[cfg(feature = "dim3")]
pub fn principal_angular_inertia(
mut self,
- inertia: AngVector<f32>,
+ inertia: AngVector<Real>,
colliders_contribution_enabled: AngVector<bool>,
) -> Self {
self.mass_properties.inv_principal_inertia_sqrt = inertia.map(utils::inv);
@@ -755,7 +757,7 @@ impl RigidBodyBuilder {
#[deprecated(note = "renamed to `principal_angular_inertia`.")]
pub fn principal_inertia(
self,
- inertia: AngVector<f32>,
+ inertia: AngVector<Real>,
colliders_contribution_enabled: AngVector<bool>,
) -> Self {
self.principal_angular_inertia(inertia, colliders_contribution_enabled)
@@ -765,7 +767,7 @@ impl RigidBodyBuilder {
///
/// The higher the linear damping factor is, the more quickly the rigid-body
/// will slow-down its translational movement.
- pub fn linear_damping(mut self, factor: f32) -> Self {
+ pub fn linear_damping(mut self, factor: Real) -> Self {
self.linear_damping = factor;
self
}
@@ -774,27 +776,27 @@ impl RigidBodyBuilder {
///
/// The higher the angular damping factor is, the more quickly the rigid-body
/// will slow-down its rotational movement.
- pub fn angular_damping(mut self, factor: f32) -> Self {
+ pub fn angular_damping(mut self, factor: Real) -> Self {
self.angular_damping = factor;
self
}
/// Sets the initial linear velocity of the rigid-body to be created.
#[cfg(feature = "dim2")]
- pub fn linvel(mut self, x: f32, y: f32) -> Self {
+ pub fn linvel(mut self, x: Real, y: Real) -> Self {
self.linvel = Vector::new(x, y);
self
}
/// Sets the initial linear velocity of the rigid-body to be created.
#[cfg(feature = "dim3")]
- pub fn linvel(mut self, x: f32, y: f32, z: f32) -> Self {
+ pub fn linvel(mut self, x: Real, y: Real, z: Real) -> Self {
self.linvel = Vector::new(x, y, z);
self
}
/// Sets the initial angular velocity of the rigid-body to be created.
- pub fn angvel(mut self, angvel: AngVector<f32>) -> Self {
+ pub fn angvel(mut self, angvel: AngVector<Real>) -> Self {
self.angvel = angvel;
self
}
@@ -845,16 +847,16 @@ impl RigidBodyBuilder {
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct ActivationStatus {
/// The threshold pseudo-kinetic energy bellow which the body can fall asleep.
- pub threshold: f32,
+ pub threshold: Real,
/// The current pseudo-kinetic energy of the body.
- pub energy: f32,
+ pub energy: Real,
/// Is this body already sleeping?
pub sleeping: bool,
}
impl ActivationStatus {
/// The default amount of energy bellow which a body can be put to sleep by nphysics.
- pub fn default_threshold() -> f32 {
+ pub fn default_threshold() -> Real {
0.01
}