#[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: Vec3FixPosition (center of mass)
velocity: Vec3FixLinear velocity
inv_mass: Fix128Inverse mass (0 = static/infinite mass)
inv_inertia: Vec3FixInverse inertia tensor (diagonal, in local space)
prev_position: Vec3FixPrevious position (for XPBD)
rotation: QuatFixOrientation, 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: Vec3FixAngular velocity
prev_rotation: QuatFixPrevious rotation (for XPBD)
restitution: Fix128Coefficient of restitution (bounciness), default 0.3
Read by contacts only while the body has the default material
(see PhysicsWorld::combined_material).
friction: Fix128Friction coefficient, default 0.5
Read by contacts only while the body has the default material
(see PhysicsWorld::combined_material).
gravity_scale: Fix128Gravity scale multiplier (1.0 = normal, 0.0 = no gravity, 2.0 = double)
linear_damping: Fix128Per-body linear damping factor (1.0 = no damping, 0.0 = full damping)
Multiplied with the global damping, applied once per frame (step()).
angular_damping: Fix128Per-body angular damping factor (1.0 = no damping, 0.0 = full damping)
Multiplied with the global damping, applied once per frame (step()).
is_sensor: boolWhether this body is a sensor/trigger (detects overlap but no physics response)
body_type: BodyTypeBody type (Dynamic, Static, Kinematic)
kinematic_target: Option<(Vec3Fix, QuatFix)>Kinematic target position and rotation
Implementations§
Source§impl RigidBody
impl RigidBody
Sourcepub fn new(position: Vec3Fix, mass: Fix128) -> Self
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());Sourcepub fn new_dynamic(position: Vec3Fix, mass: Fix128) -> Self
pub fn new_dynamic(position: Vec3Fix, mass: Fix128) -> Self
Create new dynamic rigid body (alias for new)
Sourcepub const fn new_static(position: Vec3Fix) -> Self
pub const fn new_static(position: Vec3Fix) -> Self
Create static (immovable) rigid body
Sourcepub const fn new_sensor(position: Vec3Fix) -> Self
pub const fn new_sensor(position: Vec3Fix) -> Self
Create a sensor (trigger) body - detects overlap but no physics response
Sourcepub const fn new_kinematic(position: Vec3Fix) -> Self
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.
Sourcepub fn is_kinematic(&self) -> bool
pub fn is_kinematic(&self) -> bool
Check if body is kinematic
Sourcepub fn is_dynamic(&self) -> bool
pub fn is_dynamic(&self) -> bool
Check if body is dynamic
Sourcepub fn set_kinematic_target(&mut self, position: Vec3Fix, rotation: QuatFix)
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).
Sourcepub fn apply_impulse(&mut self, impulse: Vec3Fix)
pub fn apply_impulse(&mut self, impulse: Vec3Fix)
Apply impulse at center of mass
Sourcepub fn apply_impulse_at(&mut self, impulse: Vec3Fix, point: Vec3Fix)
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 diagonalinv_inertiarotated byrotationinto the world frame - a static body is unchanged
Sourcepub fn add_force(&mut self, force: Vec3Fix, dt: Fix128)
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.
Sourcepub fn add_torque(&mut self, torque: Vec3Fix, dt: Fix128)
pub fn add_torque(&mut self, torque: Vec3Fix, dt: Fix128)
Apply a continuous torque
§Claims
torqueis a world-frame vector;angular_velocity += I_world⁻¹ torque * dtI_world⁻¹ = R diag(inv_inertia) R⁻¹withR = rotation- a static body is unchanged
Sourcepub fn set_velocity(&mut self, velocity: Vec3Fix)
pub fn set_velocity(&mut self, velocity: Vec3Fix)
Set the linear velocity directly
Sourcepub fn set_angular_velocity(&mut self, angular_velocity: Vec3Fix)
pub fn set_angular_velocity(&mut self, angular_velocity: Vec3Fix)
Set the angular velocity directly
Sourcepub fn set_position(&mut self, position: Vec3Fix)
pub fn set_position(&mut self, position: Vec3Fix)
Set position directly (teleport)
Sourcepub fn set_rotation(&mut self, rotation: QuatFix)
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.
Sourcepub fn mass(&self) -> Fix128
pub fn mass(&self) -> Fix128
Get mass (inverse of inv_mass, returns infinity for static bodies)
Sourcepub const fn with_linear_damping(self, damping: Fix128) -> Self
pub const fn with_linear_damping(self, damping: Fix128) -> Self
Set per-body linear damping (1.0 = no extra damping)
Sourcepub const fn with_angular_damping(self, damping: Fix128) -> Self
pub const fn with_angular_damping(self, damping: Fix128) -> Self
Set per-body angular damping (1.0 = no extra damping)
Sourcepub const fn with_restitution(self, restitution: Fix128) -> Self
pub const fn with_restitution(self, restitution: Fix128) -> Self
Builder: set restitution (bounciness)
Sourcepub const fn with_friction(self, friction: Fix128) -> Self
pub const fn with_friction(self, friction: Fix128) -> Self
Builder: set friction coefficient
Sourcepub const fn with_gravity_scale(self, scale: Fix128) -> Self
pub const fn with_gravity_scale(self, scale: Fix128) -> Self
Builder: set gravity scale
Sourcepub const fn with_sensor(self, is_sensor: bool) -> Self
pub const fn with_sensor(self, is_sensor: bool) -> Self
Builder: set as sensor (trigger)
Sourcepub const fn with_velocity(self, velocity: Vec3Fix) -> Self
pub const fn with_velocity(self, velocity: Vec3Fix) -> Self
Builder: set initial velocity
Sourcepub const fn with_rotation(self, rotation: QuatFix) -> Self
pub const fn with_rotation(self, rotation: QuatFix) -> Self
Builder: set initial rotation
Trait Implementations§
impl Copy for RigidBody
impl Eq for RigidBody
impl StructuralPartialEq for RigidBody
Auto Trait Implementations§
impl Freeze for RigidBody
impl RefUnwindSafe for RigidBody
impl Send for RigidBody
impl Sync for RigidBody
impl Unpin for RigidBody
impl UnsafeUnpin for RigidBody
impl UnwindSafe for RigidBody
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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