pub struct RigidBody {
pub body_type: RigidBodyType,
pub linear_velocity: Vec2,
pub angular_velocity: f32,
pub linear_damping: f32,
pub angular_damping: f32,
pub mass: f32,
pub inverse_mass: f32,
pub inertia: f32,
pub inverse_inertia: f32,
pub gravity_scale: f32,
/* private fields */
}Expand description
A physics body component for 2D physics simulation.
RigidBody stores all the physics state for an entity, including velocity,
forces, mass, and behavior type. It works together with the Transform2D
component (for position/rotation) and optionally with Collider components
for collision detection.
§Memory Layout
The component is laid out as:
body_type: u8 (1 byte) + 3 bytes paddinglinear_velocity: Vec2 (8 bytes)angular_velocity: f32 (4 bytes)linear_damping: f32 (4 bytes)angular_damping: f32 (4 bytes)mass: f32 (4 bytes)inverse_mass: f32 (4 bytes)inertia: f32 (4 bytes)inverse_inertia: f32 (4 bytes)gravity_scale: f32 (4 bytes)flags: u8 (1 byte) + 3 bytes paddingsleep_time: f32 (4 bytes)- Total: 56 bytes (may vary with padding)
§Examples
use goud_engine::ecs::components::{RigidBody, RigidBodyType};
use goud_engine::core::math::Vec2;
// Dynamic body with custom properties
let body = RigidBody::dynamic()
.with_mass(2.0)
.with_velocity(Vec2::new(100.0, 0.0))
.with_gravity_scale(1.5);
// Kinematic platform
let platform = RigidBody::kinematic()
.with_velocity(Vec2::new(50.0, 0.0));
// Static wall
let wall = RigidBody::static_body();§Material Properties
Material properties (restitution, friction) live on Collider, not RigidBody.
Set them via Collider::with_restitution and Collider::with_friction.
Fields§
§body_type: RigidBodyTypePhysics behavior type (Dynamic, Kinematic, or Static).
linear_velocity: Vec2Linear velocity in pixels/second.
angular_velocity: f32Angular velocity in radians/second.
linear_damping: f32Linear damping (air resistance) - 0.0 = no damping, 1.0 = full damping. Applied each frame as: velocity *= (1.0 - damping * dt)
angular_damping: f32Angular damping (rotational resistance) - 0.0 = no damping, 1.0 = full damping.
mass: f32Mass in arbitrary units (kg assumed for consistency). Must be positive for dynamic bodies.
inverse_mass: f32Inverse mass (1.0 / mass). Pre-calculated for performance. Zero for static/kinematic bodies (infinite mass).
inertia: f32Rotational inertia (moment of inertia) in kg·m². Calculated from mass and collider shape.
inverse_inertia: f32Inverse inertia (1.0 / inertia). Pre-calculated for performance. Zero for static/kinematic bodies.
gravity_scale: f32Gravity scale multiplier.
- 0.0 = no gravity
- 1.0 = normal gravity
- 2.0 = double gravity
Implementations§
Source§impl RigidBody
impl RigidBody
Sourcepub fn new(body_type: RigidBodyType) -> RigidBody
pub fn new(body_type: RigidBodyType) -> RigidBody
Sourcepub fn dynamic() -> RigidBody
pub fn dynamic() -> RigidBody
Creates a dynamic rigid body (fully simulated).
§Examples
use goud_engine::ecs::components::RigidBody;
let player = RigidBody::dynamic()
.with_mass(2.0);Sourcepub fn kinematic() -> RigidBody
pub fn kinematic() -> RigidBody
Creates a kinematic rigid body (controlled velocity).
§Examples
use goud_engine::ecs::components::RigidBody;
use goud_engine::core::math::Vec2;
let platform = RigidBody::kinematic()
.with_velocity(Vec2::new(50.0, 0.0));Sourcepub fn static_body() -> RigidBody
pub fn static_body() -> RigidBody
Creates a static rigid body (immovable).
§Examples
use goud_engine::ecs::components::RigidBody;
let wall = RigidBody::static_body();Sourcepub fn with_velocity(self, velocity: Vec2) -> RigidBody
pub fn with_velocity(self, velocity: Vec2) -> RigidBody
Sets the linear velocity.
Sourcepub fn with_angular_velocity(self, angular_velocity: f32) -> RigidBody
pub fn with_angular_velocity(self, angular_velocity: f32) -> RigidBody
Sets the angular velocity.
Sourcepub fn with_linear_damping(self, damping: f32) -> RigidBody
pub fn with_linear_damping(self, damping: f32) -> RigidBody
Sets the linear damping.
Sourcepub fn with_angular_damping(self, damping: f32) -> RigidBody
pub fn with_angular_damping(self, damping: f32) -> RigidBody
Sets the angular damping.
Sourcepub fn with_gravity_scale(self, scale: f32) -> RigidBody
pub fn with_gravity_scale(self, scale: f32) -> RigidBody
Sets the gravity scale.
Sourcepub fn with_can_sleep(self, can_sleep: bool) -> RigidBody
pub fn with_can_sleep(self, can_sleep: bool) -> RigidBody
Sets whether the body can sleep.
Sourcepub fn with_continuous_cd(self, enabled: bool) -> RigidBody
pub fn with_continuous_cd(self, enabled: bool) -> RigidBody
Enables continuous collision detection (for fast-moving objects).
Sourcepub fn with_fixed_rotation(self, fixed: bool) -> RigidBody
pub fn with_fixed_rotation(self, fixed: bool) -> RigidBody
Fixes the rotation (prevents rotation from collisions).
Sourcepub fn body_type(&self) -> RigidBodyType
pub fn body_type(&self) -> RigidBodyType
Returns the body type.
Sourcepub fn is_dynamic(&self) -> bool
pub fn is_dynamic(&self) -> bool
Returns true if this is a dynamic body.
Sourcepub fn is_kinematic(&self) -> bool
pub fn is_kinematic(&self) -> bool
Returns true if this is a kinematic body.
Sourcepub fn is_sleeping(&self) -> bool
pub fn is_sleeping(&self) -> bool
Returns true if the body is currently sleeping.
Sourcepub fn has_continuous_cd(&self) -> bool
pub fn has_continuous_cd(&self) -> bool
Returns true if continuous collision detection is enabled.
Sourcepub fn has_fixed_rotation(&self) -> bool
pub fn has_fixed_rotation(&self) -> bool
Returns true if rotation is fixed.
Sourcepub fn sleep_time(&self) -> f32
pub fn sleep_time(&self) -> f32
Returns the current sleep time.
Sourcepub fn linear_speed(&self) -> f32
pub fn linear_speed(&self) -> f32
Returns the linear speed (magnitude of velocity).
Sourcepub fn linear_speed_squared(&self) -> f32
pub fn linear_speed_squared(&self) -> f32
Returns the linear speed squared (avoids sqrt).
Sourcepub fn kinetic_energy(&self) -> f32
pub fn kinetic_energy(&self) -> f32
Returns the kinetic energy of the body (0.5 * m * v²).
Sourcepub fn set_velocity(&mut self, velocity: Vec2)
pub fn set_velocity(&mut self, velocity: Vec2)
Sets the linear velocity.
Sourcepub fn set_angular_velocity(&mut self, angular_velocity: f32)
pub fn set_angular_velocity(&mut self, angular_velocity: f32)
Sets the angular velocity.
Sourcepub fn set_mass(&mut self, mass: f32)
pub fn set_mass(&mut self, mass: f32)
Sets the mass (dynamic bodies only).
§Panics
Panics if mass is not positive and finite, or if called on non-dynamic body.
Sourcepub fn set_body_type(&mut self, body_type: RigidBodyType)
pub fn set_body_type(&mut self, body_type: RigidBodyType)
Sets the body type, updating mass accordingly.
Source§impl RigidBody
impl RigidBody
Sourcepub fn apply_force(&mut self, _force: Vec2)
pub fn apply_force(&mut self, _force: Vec2)
Applies a force to the body (affects acceleration).
Force is accumulated and applied during physics integration. Only affects dynamic bodies.
§Arguments
_force- Force vector in Newtons (kg·m/s²)
§Note
This is a placeholder for the public API. Forces will be accumulated
in a separate Forces component or applied directly during physics
integration in future implementation.
Sourcepub fn apply_impulse(&mut self, impulse: Vec2)
pub fn apply_impulse(&mut self, impulse: Vec2)
Applies an impulse to the body (instant velocity change).
Impulse directly modifies velocity: Δv = impulse / mass Only affects dynamic bodies.
§Arguments
impulse- Impulse vector in kg·m/s
Sourcepub fn apply_angular_impulse(&mut self, impulse: f32)
pub fn apply_angular_impulse(&mut self, impulse: f32)
Sourcepub fn apply_damping(&mut self, dt: f32)
pub fn apply_damping(&mut self, dt: f32)
Sourcepub fn sleep(&mut self)
pub fn sleep(&mut self)
Puts the body to sleep (optimization).
Sleeping bodies are excluded from physics updates until woken.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for RigidBody
impl<'de> Deserialize<'de> for RigidBody
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<RigidBody, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<RigidBody, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for RigidBody
impl Serialize for RigidBody
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Component for RigidBody
ECS component trait implementation.
impl Copy 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<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().