Skip to main content

PhysicsWorld

Struct PhysicsWorld 

Source
pub struct PhysicsWorld {
    pub time: Real,
    pub config: PhysicsConfig,
    pub fixed_dt: Real,
    pub accumulator: Real,
    pub bodies: BodyArena,
    pub constraints: Vec<Constraint>,
    pub events: VecDeque<PhysicsEvent>,
    pub islands: Vec<Island>,
    pub max_events: usize,
}
Expand description

Top-level physics world that owns all simulation state.

Fields§

§time: Real

Simulation time accumulated so far.

§config: PhysicsConfig

Physics configuration.

§fixed_dt: Real

Fixed time step used for stepping.

§accumulator: Real

Accumulated time not yet simulated.

§bodies: BodyArena

All registered bodies.

§constraints: Vec<Constraint>

All constraints.

§events: VecDeque<PhysicsEvent>

Event queue (pending events from last step).

§islands: Vec<Island>

Islands computed at last step.

§max_events: usize

Maximum events held in the queue before oldest are dropped.

Implementations§

Source§

impl PhysicsWorld

Source

pub fn new() -> Self

Create a new empty physics world with default config.

Source

pub fn with_config(config: PhysicsConfig) -> Self

Create a new physics world with the given configuration.

Source

pub fn add_body(&mut self, body: Body) -> BodyHandle

Add a body to the world and return its handle.

Source

pub fn remove_body(&mut self, handle: BodyHandle) -> Option<Body>

Remove a body from the world. Returns the body if the handle was valid.

Source

pub fn body(&self, handle: BodyHandle) -> Option<&Body>

Get an immutable reference to a body.

Source

pub fn body_mut(&mut self, handle: BodyHandle) -> Option<&mut Body>

Get a mutable reference to a body.

Source

pub fn body_count(&self) -> usize

Return the number of bodies currently in the world.

Source

pub fn add_constraint(&mut self, constraint: Constraint) -> usize

Add a constraint between bodies and return its index.

Source

pub fn remove_constraint(&mut self, index: usize)

Remove a constraint by index. Swaps with the last to avoid shifting.

Source

pub fn push_event(&mut self, event: PhysicsEvent)

Push an event onto the queue, dropping the oldest if over capacity.

Source

pub fn drain_events(&mut self) -> Vec<PhysicsEvent>

Drain all pending events from the queue.

Source

pub fn set_gravity(&mut self, gx: Real, gy: Real, gz: Real)

Set the world gravity vector.

Source

pub fn step(&mut self, time_step: &TimeStep)

Step the world forward by a fixed TimeStep.

Source

pub fn step_with_accumulator(&mut self, dt: Real) -> u32

Step the world forward by the given delta time, using fixed substeps. Returns the number of substeps taken.

Source

pub fn compute_islands(&mut self)

Recompute simulation islands using union-find on constraint graph. Islands group bodies that interact so they can be solved independently.

Source

pub fn bodies_in_aabb(&self, query: &Aabb) -> Vec<BodyHandle>

Return handles of all dynamic bodies whose axis-aligned bounding box (approximated as a sphere of radius query_radius around center) overlaps the given AABB.

Source

pub fn bodies_in_radius( &self, center: [Real; 3], radius: Real, ) -> Vec<BodyHandle>

Return handles of all bodies within radius of center.

Source

pub fn total_kinetic_energy(&self) -> Real

Total kinetic energy (translational + rotational) in the world.

Source

pub fn total_momentum(&self) -> [Real; 3]

Total linear momentum of the world.

Source§

impl PhysicsWorld

Source

pub fn snapshot(&self) -> WorldSnapshot

Take a complete snapshot of the world state.

Source

pub fn restore_snapshot(&mut self, snap: &WorldSnapshot)

Restore the world to a previously taken snapshot.

Source

pub fn apply_force_fields(&mut self, fields: &[ForceField])

Apply all force fields to all dynamic bodies.

Source

pub fn resolve_contacts(&mut self, contacts: &[Contact])

Resolve a list of contacts (sequential impulse, one pass).

Source

pub fn total_angular_momentum(&self) -> [Real; 3]

Compute total angular momentum of the world.

Source

pub fn damp_region(&mut self, center: [Real; 3], radius: Real, factor: Real)

Apply a damping “blast” to all bodies within radius of center, scaling their velocities by factor.

Source

pub fn most_energetic_body(&self) -> Option<BodyHandle>

Return the body with the highest kinetic energy, or None if empty.

Source

pub fn apply_position_correction( &mut self, handle: BodyHandle, delta: [Real; 3], )

Apply a position correction to a body (used after PBD/XPBD solve).

Source

pub fn set_velocity( &mut self, handle: BodyHandle, linear: [Real; 3], angular: [Real; 3], )

Set the velocity of a body by handle.

Source

pub fn set_position(&mut self, handle: BodyHandle, position: [Real; 3])

Teleport a body to a new position without affecting velocity.

Source

pub fn wake_all(&mut self)

Wake all sleeping bodies in the world.

Source

pub fn diagnostics(&self) -> WorldDiagnostics

Return a summary of the world state for diagnostics.

Source§

impl PhysicsWorld

Source

pub fn query_sphere(&self, center: [Real; 3], radius: Real) -> Vec<BodyHandle>

Return all body handles whose position is within radius of center.

Source

pub fn query_frustum(&self, frustum: &Frustum) -> Vec<BodyHandle>

Return all body handles whose AABB is at least partially inside frustum.

Bodies without a stored aabb use a small unit-box centred at their position.

Source

pub fn compute_stats(&self) -> WorldStats

Compute a compact statistics snapshot of the current world state.

Source

pub fn serialize_snapshot(&self) -> WorldSerialSnapshot

Serialise the world state to a list of human-readable text lines.

Useful for debugging, logging, and lightweight persistence.

Trait Implementations§

Source§

impl Debug for PhysicsWorld

Source§

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

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

impl Default for PhysicsWorld

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.