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: RealSimulation time accumulated so far.
config: PhysicsConfigPhysics configuration.
fixed_dt: RealFixed time step used for stepping.
accumulator: RealAccumulated time not yet simulated.
bodies: BodyArenaAll 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: usizeMaximum events held in the queue before oldest are dropped.
Implementations§
Source§impl PhysicsWorld
impl PhysicsWorld
Sourcepub fn with_config(config: PhysicsConfig) -> Self
pub fn with_config(config: PhysicsConfig) -> Self
Create a new physics world with the given configuration.
Sourcepub fn add_body(&mut self, body: Body) -> BodyHandle
pub fn add_body(&mut self, body: Body) -> BodyHandle
Add a body to the world and return its handle.
Sourcepub fn remove_body(&mut self, handle: BodyHandle) -> Option<Body>
pub fn remove_body(&mut self, handle: BodyHandle) -> Option<Body>
Remove a body from the world. Returns the body if the handle was valid.
Sourcepub fn body(&self, handle: BodyHandle) -> Option<&Body>
pub fn body(&self, handle: BodyHandle) -> Option<&Body>
Get an immutable reference to a body.
Sourcepub fn body_mut(&mut self, handle: BodyHandle) -> Option<&mut Body>
pub fn body_mut(&mut self, handle: BodyHandle) -> Option<&mut Body>
Get a mutable reference to a body.
Sourcepub fn body_count(&self) -> usize
pub fn body_count(&self) -> usize
Return the number of bodies currently in the world.
Sourcepub fn add_constraint(&mut self, constraint: Constraint) -> usize
pub fn add_constraint(&mut self, constraint: Constraint) -> usize
Add a constraint between bodies and return its index.
Sourcepub fn remove_constraint(&mut self, index: usize)
pub fn remove_constraint(&mut self, index: usize)
Remove a constraint by index. Swaps with the last to avoid shifting.
Sourcepub fn push_event(&mut self, event: PhysicsEvent)
pub fn push_event(&mut self, event: PhysicsEvent)
Push an event onto the queue, dropping the oldest if over capacity.
Sourcepub fn drain_events(&mut self) -> Vec<PhysicsEvent>
pub fn drain_events(&mut self) -> Vec<PhysicsEvent>
Drain all pending events from the queue.
Sourcepub fn set_gravity(&mut self, gx: Real, gy: Real, gz: Real)
pub fn set_gravity(&mut self, gx: Real, gy: Real, gz: Real)
Set the world gravity vector.
Sourcepub fn step_with_accumulator(&mut self, dt: Real) -> u32
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.
Sourcepub fn compute_islands(&mut self)
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.
Sourcepub fn bodies_in_aabb(&self, query: &Aabb) -> Vec<BodyHandle>
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.
Sourcepub fn bodies_in_radius(
&self,
center: [Real; 3],
radius: Real,
) -> Vec<BodyHandle>
pub fn bodies_in_radius( &self, center: [Real; 3], radius: Real, ) -> Vec<BodyHandle>
Return handles of all bodies within radius of center.
Sourcepub fn total_kinetic_energy(&self) -> Real
pub fn total_kinetic_energy(&self) -> Real
Total kinetic energy (translational + rotational) in the world.
Sourcepub fn total_momentum(&self) -> [Real; 3]
pub fn total_momentum(&self) -> [Real; 3]
Total linear momentum of the world.
Source§impl PhysicsWorld
impl PhysicsWorld
Sourcepub fn snapshot(&self) -> WorldSnapshot
pub fn snapshot(&self) -> WorldSnapshot
Take a complete snapshot of the world state.
Sourcepub fn restore_snapshot(&mut self, snap: &WorldSnapshot)
pub fn restore_snapshot(&mut self, snap: &WorldSnapshot)
Restore the world to a previously taken snapshot.
Sourcepub fn apply_force_fields(&mut self, fields: &[ForceField])
pub fn apply_force_fields(&mut self, fields: &[ForceField])
Apply all force fields to all dynamic bodies.
Sourcepub fn resolve_contacts(&mut self, contacts: &[Contact])
pub fn resolve_contacts(&mut self, contacts: &[Contact])
Resolve a list of contacts (sequential impulse, one pass).
Sourcepub fn total_angular_momentum(&self) -> [Real; 3]
pub fn total_angular_momentum(&self) -> [Real; 3]
Compute total angular momentum of the world.
Sourcepub fn damp_region(&mut self, center: [Real; 3], radius: Real, factor: Real)
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.
Sourcepub fn most_energetic_body(&self) -> Option<BodyHandle>
pub fn most_energetic_body(&self) -> Option<BodyHandle>
Return the body with the highest kinetic energy, or None if empty.
Sourcepub fn apply_position_correction(
&mut self,
handle: BodyHandle,
delta: [Real; 3],
)
pub fn apply_position_correction( &mut self, handle: BodyHandle, delta: [Real; 3], )
Apply a position correction to a body (used after PBD/XPBD solve).
Sourcepub fn set_velocity(
&mut self,
handle: BodyHandle,
linear: [Real; 3],
angular: [Real; 3],
)
pub fn set_velocity( &mut self, handle: BodyHandle, linear: [Real; 3], angular: [Real; 3], )
Set the velocity of a body by handle.
Sourcepub fn set_position(&mut self, handle: BodyHandle, position: [Real; 3])
pub fn set_position(&mut self, handle: BodyHandle, position: [Real; 3])
Teleport a body to a new position without affecting velocity.
Sourcepub fn diagnostics(&self) -> WorldDiagnostics
pub fn diagnostics(&self) -> WorldDiagnostics
Return a summary of the world state for diagnostics.
Source§impl PhysicsWorld
impl PhysicsWorld
Sourcepub fn query_sphere(&self, center: [Real; 3], radius: Real) -> Vec<BodyHandle>
pub fn query_sphere(&self, center: [Real; 3], radius: Real) -> Vec<BodyHandle>
Return all body handles whose position is within radius of center.
Sourcepub fn query_frustum(&self, frustum: &Frustum) -> Vec<BodyHandle>
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.
Sourcepub fn compute_stats(&self) -> WorldStats
pub fn compute_stats(&self) -> WorldStats
Compute a compact statistics snapshot of the current world state.
Sourcepub fn serialize_snapshot(&self) -> WorldSerialSnapshot
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
impl Debug for PhysicsWorld
Auto Trait Implementations§
impl Freeze for PhysicsWorld
impl RefUnwindSafe for PhysicsWorld
impl Send for PhysicsWorld
impl Sync for PhysicsWorld
impl Unpin for PhysicsWorld
impl UnsafeUnpin for PhysicsWorld
impl UnwindSafe for PhysicsWorld
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<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.