Skip to main content

RigidBody

Struct RigidBody 

Source
#[repr(C, align(64))]
pub struct RigidBody {
Show 16 fields pub position: Vec3Fix, pub velocity: Vec3Fix, pub inv_mass: Fix128, pub inv_inertia: Vec3Fix, pub prev_position: Vec3Fix, pub rotation: QuatFix, pub angular_velocity: Vec3Fix, pub prev_rotation: QuatFix, pub restitution: Fix128, pub friction: Fix128, pub gravity_scale: Fix128, pub linear_damping: Fix128, pub angular_damping: Fix128, pub is_sensor: bool, pub body_type: BodyType, pub kinematic_target: Option<(Vec3Fix, QuatFix)>,
}
Expand description

Rigid body state

Field layout is optimized for cache performance (Gap 1.1):

  • HOT fields (accessed every solver iteration) are placed first.
  • COLD fields (accessed occasionally) follow after.

#[repr(C, align(64))] ensures the struct starts on a cache-line boundary so the hot fields are always in the first 64-byte cache line.

Fields§

§position: Vec3Fix

Position (center of mass)

§velocity: Vec3Fix

Linear velocity

§inv_mass: Fix128

Inverse mass (0 = static/infinite mass)

§inv_inertia: Vec3Fix

Inverse inertia tensor (diagonal, in local space)

§prev_position: Vec3Fix

Previous position (for XPBD)

§rotation: QuatFix

Orientation, a unit quaternion

Self::set_rotation, PhysicsWorld::add_body and the start of every PhysicsWorld::step store it at unit length. A non-unit value written to this field directly (or through PhysicsWorld::get_body_mut, or restored from a snapshot) is used as written until the next step, so what it rotates is scaled by |q|^2 in the queries made before that step.

§angular_velocity: Vec3Fix

Angular velocity

§prev_rotation: QuatFix

Previous rotation (for XPBD)

§restitution: Fix128

Coefficient of restitution (bounciness), default 0.3

Read by contacts only while the body has the default material (see PhysicsWorld::combined_material).

§friction: Fix128

Friction coefficient, default 0.5

Read by contacts only while the body has the default material (see PhysicsWorld::combined_material).

§gravity_scale: Fix128

Gravity scale multiplier (1.0 = normal, 0.0 = no gravity, 2.0 = double)

§linear_damping: Fix128

Per-body linear damping factor (1.0 = no damping, 0.0 = full damping) Multiplied with the global damping, applied once per frame (step()).

§angular_damping: Fix128

Per-body angular damping factor (1.0 = no damping, 0.0 = full damping) Multiplied with the global damping, applied once per frame (step()).

§is_sensor: bool

Whether this body is a sensor/trigger (detects overlap but no physics response)

§body_type: BodyType

Body type (Dynamic, Static, Kinematic)

§kinematic_target: Option<(Vec3Fix, QuatFix)>

Kinematic target position and rotation

Implementations§

Source§

impl RigidBody

Source

pub fn new(position: Vec3Fix, mass: Fix128) -> Self

Create a new dynamic rigid body at the given position with the given mass.

The inverse mass is computed automatically. A unit-sphere inertia tensor is used as the default. Use new_dynamic as a more descriptive alias.

§Examples
use alice_physics::{Fix128, RigidBody, Vec3Fix};

let body = RigidBody::new(Vec3Fix::from_int(0, 10, 0), Fix128::ONE);
// inv_mass should be 1 / 1 = 1
assert_eq!(body.inv_mass.hi, 1);
// Position should match
assert_eq!(body.position.y.hi, 10);
// Dynamic body is not static
assert!(!body.is_static());
Source

pub fn new_dynamic(position: Vec3Fix, mass: Fix128) -> Self

Create new dynamic rigid body (alias for new)

Source

pub const fn new_static(position: Vec3Fix) -> Self

Create static (immovable) rigid body

Source

pub const fn new_sensor(position: Vec3Fix) -> Self

Create a sensor (trigger) body - detects overlap but no physics response

Source

pub const fn new_kinematic(position: Vec3Fix) -> Self

Create a kinematic body (moved by user code, not physics)

Kinematic bodies have infinite mass and are unaffected by forces, but can push dynamic bodies. Set target via set_kinematic_target.

Source

pub const fn is_static(&self) -> bool

Check if body is static

Source

pub fn is_kinematic(&self) -> bool

Check if body is kinematic

Source

pub fn is_dynamic(&self) -> bool

Check if body is dynamic

Source

pub fn set_kinematic_target(&mut self, position: Vec3Fix, rotation: QuatFix)

Set kinematic target position and rotation

The body will be moved to this target during the next simulation step. Velocity is automatically computed from the position change.

rotation is stored as a unit quaternion (see Self::set_rotation).

Source

pub fn apply_impulse(&mut self, impulse: Vec3Fix)

Apply impulse at center of mass

Source

pub fn apply_impulse_at(&mut self, impulse: Vec3Fix, point: Vec3Fix)

Apply impulse at world-space point

§Claims
  • the linear part is impulse * inv_mass
  • the angular part is I_world⁻¹ ((point - position) × impulse), with the body-frame diagonal inv_inertia rotated by rotation into the world frame
  • a static body is unchanged
Source

pub fn add_force(&mut self, force: Vec3Fix, dt: Fix128)

Apply a continuous force (accumulated, applied during next step)

Force is converted to velocity change: v += (F * inv_mass) * dt For one-shot velocity changes, use apply_impulse instead.

Source

pub fn add_torque(&mut self, torque: Vec3Fix, dt: Fix128)

Apply a continuous torque

§Claims
  • torque is a world-frame vector; angular_velocity += I_world⁻¹ torque * dt
  • I_world⁻¹ = R diag(inv_inertia) R⁻¹ with R = rotation
  • a static body is unchanged
Source

pub fn set_velocity(&mut self, velocity: Vec3Fix)

Set the linear velocity directly

Source

pub fn set_angular_velocity(&mut self, angular_velocity: Vec3Fix)

Set the angular velocity directly

Source

pub fn set_position(&mut self, position: Vec3Fix)

Set position directly (teleport)

Source

pub fn set_rotation(&mut self, rotation: QuatFix)

Set rotation directly

The stored rotation is rotation as a unit quaternion: one whose squared length is within 2^-32 of one is kept bit for bit, any other is normalized (the zero quaternion becomes the identity). Applying a quaternion q scales what it rotates by |q|^2.

Source

pub fn mass(&self) -> Fix128

Get mass (inverse of inv_mass, returns infinity for static bodies)

Source

pub fn speed(&self) -> Fix128

Get linear speed (magnitude of velocity)

Source

pub const fn with_linear_damping(self, damping: Fix128) -> Self

Set per-body linear damping (1.0 = no extra damping)

Source

pub const fn with_angular_damping(self, damping: Fix128) -> Self

Set per-body angular damping (1.0 = no extra damping)

Source

pub const fn with_restitution(self, restitution: Fix128) -> Self

Builder: set restitution (bounciness)

Source

pub const fn with_friction(self, friction: Fix128) -> Self

Builder: set friction coefficient

Source

pub const fn with_gravity_scale(self, scale: Fix128) -> Self

Builder: set gravity scale

Source

pub const fn with_sensor(self, is_sensor: bool) -> Self

Builder: set as sensor (trigger)

Source

pub const fn with_velocity(self, velocity: Vec3Fix) -> Self

Builder: set initial velocity

Source

pub const fn with_rotation(self, rotation: QuatFix) -> Self

Builder: set initial rotation

Trait Implementations§

Source§

impl Clone for RigidBody

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for RigidBody

Source§

impl Debug for RigidBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RigidBody

Source§

fn default() -> Self

Default creates a 1kg dynamic body at the origin.

Source§

impl Eq for RigidBody

Source§

impl PartialEq for RigidBody

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RigidBody

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.