Skip to main content

RigidBody

Struct RigidBody 

Source
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 padding
  • linear_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 padding
  • sleep_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: RigidBodyType

Physics behavior type (Dynamic, Kinematic, or Static).

§linear_velocity: Vec2

Linear velocity in pixels/second.

§angular_velocity: f32

Angular velocity in radians/second.

§linear_damping: f32

Linear damping (air resistance) - 0.0 = no damping, 1.0 = full damping. Applied each frame as: velocity *= (1.0 - damping * dt)

§angular_damping: f32

Angular damping (rotational resistance) - 0.0 = no damping, 1.0 = full damping.

§mass: f32

Mass in arbitrary units (kg assumed for consistency). Must be positive for dynamic bodies.

§inverse_mass: f32

Inverse mass (1.0 / mass). Pre-calculated for performance. Zero for static/kinematic bodies (infinite mass).

§inertia: f32

Rotational inertia (moment of inertia) in kg·m². Calculated from mass and collider shape.

§inverse_inertia: f32

Inverse inertia (1.0 / inertia). Pre-calculated for performance. Zero for static/kinematic bodies.

§gravity_scale: f32

Gravity scale multiplier.

  • 0.0 = no gravity
  • 1.0 = normal gravity
  • 2.0 = double gravity

Implementations§

Source§

impl RigidBody

Source

pub fn new(body_type: RigidBodyType) -> RigidBody

Creates a new rigid body with custom parameters.

§Arguments
  • body_type - Physics behavior type
§Examples
use goud_engine::ecs::components::{RigidBody, RigidBodyType};

let body = RigidBody::new(RigidBodyType::Dynamic);
Source

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);
Source

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));
Source

pub fn static_body() -> RigidBody

Creates a static rigid body (immovable).

§Examples
use goud_engine::ecs::components::RigidBody;

let wall = RigidBody::static_body();
Source

pub fn with_velocity(self, velocity: Vec2) -> RigidBody

Sets the linear velocity.

Source

pub fn with_angular_velocity(self, angular_velocity: f32) -> RigidBody

Sets the angular velocity.

Source

pub fn with_mass(self, mass: f32) -> RigidBody

Sets the mass (dynamic bodies only).

§Panics

Panics if mass is not positive and finite.

Source

pub fn with_linear_damping(self, damping: f32) -> RigidBody

Sets the linear damping.

Source

pub fn with_angular_damping(self, damping: f32) -> RigidBody

Sets the angular damping.

Source

pub fn with_gravity_scale(self, scale: f32) -> RigidBody

Sets the gravity scale.

Source

pub fn with_can_sleep(self, can_sleep: bool) -> RigidBody

Sets whether the body can sleep.

Source

pub fn with_continuous_cd(self, enabled: bool) -> RigidBody

Enables continuous collision detection (for fast-moving objects).

Source

pub fn with_fixed_rotation(self, fixed: bool) -> RigidBody

Fixes the rotation (prevents rotation from collisions).

Source

pub fn body_type(&self) -> RigidBodyType

Returns the body type.

Source

pub fn is_dynamic(&self) -> bool

Returns true if this is a dynamic body.

Source

pub fn is_kinematic(&self) -> bool

Returns true if this is a kinematic body.

Source

pub fn is_static(&self) -> bool

Returns true if this is a static body.

Source

pub fn is_sleeping(&self) -> bool

Returns true if the body is currently sleeping.

Source

pub fn can_sleep(&self) -> bool

Returns true if the body can sleep.

Source

pub fn has_continuous_cd(&self) -> bool

Returns true if continuous collision detection is enabled.

Source

pub fn has_fixed_rotation(&self) -> bool

Returns true if rotation is fixed.

Source

pub fn sleep_time(&self) -> f32

Returns the current sleep time.

Source

pub fn linear_speed(&self) -> f32

Returns the linear speed (magnitude of velocity).

Source

pub fn linear_speed_squared(&self) -> f32

Returns the linear speed squared (avoids sqrt).

Source

pub fn kinetic_energy(&self) -> f32

Returns the kinetic energy of the body (0.5 * m * v²).

Source

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

Sets the linear velocity.

Source

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

Sets the angular velocity.

Source

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.

Source

pub fn set_body_type(&mut self, body_type: RigidBodyType)

Sets the body type, updating mass accordingly.

Source§

impl RigidBody

Source

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.

Source

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
Source

pub fn apply_angular_impulse(&mut self, impulse: f32)

Applies an angular impulse to the body.

§Arguments
  • impulse - Angular impulse in kg·m²/s
Source

pub fn apply_damping(&mut self, dt: f32)

Applies damping to velocity (called by physics system).

§Arguments
  • dt - Delta time in seconds
Source

pub fn sleep(&mut self)

Puts the body to sleep (optimization).

Sleeping bodies are excluded from physics updates until woken.

Source

pub fn wake(&mut self)

Wakes the body from sleep.

Source

pub fn update_sleep_time( &mut self, dt: f32, linear_threshold: f32, angular_threshold: f32, ) -> bool

Updates sleep time based on current motion (called by physics system).

§Arguments
  • dt - Delta time in seconds
  • linear_threshold - Linear velocity threshold for sleep (pixels/s)
  • angular_threshold - Angular velocity threshold for sleep (radians/s)
§Returns

True if the body should sleep.

Trait Implementations§

Source§

impl Clone for RigidBody

Source§

fn clone(&self) -> RigidBody

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RigidBody

Source§

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

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

impl Default for RigidBody

Source§

fn default() -> RigidBody

Creates a default dynamic rigid body.

Source§

impl<'de> Deserialize<'de> for RigidBody

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<RigidBody, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for RigidBody

Source§

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

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

impl PartialEq for RigidBody

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for RigidBody

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Component for RigidBody

ECS component trait implementation.

Source§

impl Copy for RigidBody

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<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

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<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,