pub struct PyPhysicsWorld { /* private fields */ }Expand description
A self-contained physics simulation world.
Provides Python-friendly methods using integer handles (u32) and plain
array types. Internally uses a slot-based arena with generation counters
so that stale handles are detected as absent bodies.
Implementations§
Source§impl PyPhysicsWorld
Extension: constraint storage and resolution on PyPhysicsWorld.
impl PyPhysicsWorld
Extension: constraint storage and resolution on PyPhysicsWorld.
Sourcepub fn add_constraint(&mut self, c: PyConstraint) -> u32
pub fn add_constraint(&mut self, c: PyConstraint) -> u32
Add a constraint to the world. Returns the constraint handle.
Note: constraint handles are auto-incremented independently of body handles.
Sourcepub fn remove_constraint(&mut self, handle: u32) -> bool
pub fn remove_constraint(&mut self, handle: u32) -> bool
Remove a constraint by handle. Returns true if found.
Sourcepub fn constraint_count(&self) -> usize
pub fn constraint_count(&self) -> usize
Return the number of active constraints.
Source§impl PyPhysicsWorld
impl PyPhysicsWorld
Sourcepub fn compute_total_energy(&self) -> (f64, f64, f64)
pub fn compute_total_energy(&self) -> (f64, f64, f64)
Compute total mechanical energy: kinetic energy + gravitational potential energy.
KE = Σ ½ m v² (summed over dynamic bodies) PE = Σ m * |g| * h (height measured along gravity direction; uses Y component of gravity)
Returns (kinetic, potential, total).
Sourcepub fn apply_gravity_field<F>(&mut self, field: F)
pub fn apply_gravity_field<F>(&mut self, field: F)
Apply a spatially varying gravity field to all dynamic bodies for one step.
The field is specified as a closure f(position) -> [f64; 3] mapping
a body’s world-space position to a gravitational acceleration vector.
This method accumulates forces based on the field and does not
step the simulation; call step() afterwards.
§Example
Radial gravity toward the origin:
world.apply_gravity_field(|p| {
let r2 = p[0]*p[0] + p[1]*p[1] + p[2]*p[2] + 1e-6;
let r = r2.sqrt();
[-9.81 * p[0] / r, -9.81 * p[1] / r, -9.81 * p[2] / r]
});Sourcepub fn get_contact_list(&self) -> Vec<ContactPair>
pub fn get_contact_list(&self) -> Vec<ContactPair>
Return the list of contact pairs from the most recent simulation step.
Each entry contains the two body handles and contact geometry.
Source§impl PyPhysicsWorld
impl PyPhysicsWorld
Sourcepub fn body_kinetic_energy(&self, handle: u32) -> Option<f64>
pub fn body_kinetic_energy(&self, handle: u32) -> Option<f64>
Return the kinetic energy of a single body, or None if not found.
Sourcepub fn closest_body(&self, point: [f64; 3]) -> Option<(u32, f64)>
pub fn closest_body(&self, point: [f64; 3]) -> Option<(u32, f64)>
Return the closest active body handle to point, or None if empty.
Source§impl PyPhysicsWorld
impl PyPhysicsWorld
Sourcepub fn new(config: PySimConfig) -> Self
pub fn new(config: PySimConfig) -> Self
Create a new physics world with the given simulation config.
Sourcepub fn add_rigid_body(&mut self, config: PyRigidBodyConfig) -> u32
pub fn add_rigid_body(&mut self, config: PyRigidBodyConfig) -> u32
Add a new rigid body described by config. Returns a u32 handle.
Sourcepub fn add_body_legacy(&mut self, desc: &PyRigidBodyDesc) -> u32
pub fn add_body_legacy(&mut self, desc: &PyRigidBodyDesc) -> u32
Add a rigid body using the legacy PyRigidBodyDesc interface.
Sourcepub fn add_collider(&mut self, handle: u32, desc: &PyColliderDesc)
pub fn add_collider(&mut self, handle: u32, desc: &PyColliderDesc)
Add a collider shape to the body with the given handle.
Does nothing if the handle is invalid.
Sourcepub fn remove_body(&mut self, handle: u32) -> bool
pub fn remove_body(&mut self, handle: u32) -> bool
Remove a body by handle. Returns true if the body existed.
Sourcepub fn get_position(&self, handle: u32) -> Option<[f64; 3]>
pub fn get_position(&self, handle: u32) -> Option<[f64; 3]>
Get the position of a body, or None if the handle is invalid.
Sourcepub fn get_velocity(&self, handle: u32) -> Option<[f64; 3]>
pub fn get_velocity(&self, handle: u32) -> Option<[f64; 3]>
Get the linear velocity of a body, or None if the handle is invalid.
Sourcepub fn get_orientation(&self, handle: u32) -> Option<[f64; 4]>
pub fn get_orientation(&self, handle: u32) -> Option<[f64; 4]>
Get the orientation quaternion [x, y, z, w], or None.
Sourcepub fn get_angular_velocity(&self, handle: u32) -> Option<[f64; 3]>
pub fn get_angular_velocity(&self, handle: u32) -> Option<[f64; 3]>
Get the angular velocity, or None.
Sourcepub fn is_sleeping(&self, handle: u32) -> bool
pub fn is_sleeping(&self, handle: u32) -> bool
Whether the body with handle is currently sleeping.
Sourcepub fn set_position(&mut self, handle: u32, pos: [f64; 3])
pub fn set_position(&mut self, handle: u32, pos: [f64; 3])
Set the position of a body.
Sourcepub fn set_velocity(&mut self, handle: u32, vel: [f64; 3])
pub fn set_velocity(&mut self, handle: u32, vel: [f64; 3])
Set the linear velocity of a body.
Sourcepub fn set_orientation(&mut self, handle: u32, orientation: [f64; 4])
pub fn set_orientation(&mut self, handle: u32, orientation: [f64; 4])
Set the orientation (quaternion [x, y, z, w]) of a body.
Sourcepub fn set_angular_velocity(&mut self, handle: u32, omega: [f64; 3])
pub fn set_angular_velocity(&mut self, handle: u32, omega: [f64; 3])
Set the angular velocity of a body.
Sourcepub fn apply_force(
&mut self,
handle: u32,
force: [f64; 3],
point: Option<[f64; 3]>,
)
pub fn apply_force( &mut self, handle: u32, force: [f64; 3], point: Option<[f64; 3]>, )
Apply a force [fx, fy, fz] to a body, optionally at a world-space point.
If point is None, the force is applied at the center of mass.
Force accumulates until the next step().
Sourcepub fn apply_impulse(
&mut self,
handle: u32,
impulse: [f64; 3],
point: Option<[f64; 3]>,
)
pub fn apply_impulse( &mut self, handle: u32, impulse: [f64; 3], point: Option<[f64; 3]>, )
Apply an impulse [ix, iy, iz] directly to a body’s velocity.
An impulse is a instantaneous change in momentum: Δv = impulse / mass.
If point is provided, an angular impulse is also applied.
Sourcepub fn apply_torque(&mut self, handle: u32, torque: [f64; 3])
pub fn apply_torque(&mut self, handle: u32, torque: [f64; 3])
Apply a torque [tx, ty, tz] to a body (accumulates until next step).
Sourcepub fn sleep_body(&mut self, handle: u32)
pub fn sleep_body(&mut self, handle: u32)
Put a body to sleep.
Sourcepub fn set_gravity(&mut self, g: [f64; 3])
pub fn set_gravity(&mut self, g: [f64; 3])
Set the gravity vector.
Sourcepub fn config(&self) -> &PySimConfig
pub fn config(&self) -> &PySimConfig
Get the current simulation configuration.
Sourcepub fn body_count(&self) -> usize
pub fn body_count(&self) -> usize
Total number of allocated body slots (includes removed ones until reuse).
Sourcepub fn sleeping_count(&self) -> usize
pub fn sleeping_count(&self) -> usize
Number of bodies currently sleeping.
Sourcepub fn get_contacts(&self) -> Vec<PyContactResult>
pub fn get_contacts(&self) -> Vec<PyContactResult>
Contacts from the most recent simulation step.
Sourcepub fn all_positions(&self) -> Vec<[f64; 3]>
pub fn all_positions(&self) -> Vec<[f64; 3]>
Return all body positions as a flat Vec<[f64;3]> in handle order (skipping removed).
Sourcepub fn all_velocities(&self) -> Vec<[f64; 3]>
pub fn all_velocities(&self) -> Vec<[f64; 3]>
Return all body velocities in handle order (skipping removed).
Sourcepub fn active_handles(&self) -> Vec<u32>
pub fn active_handles(&self) -> Vec<u32>
Return all active (non-removed) handles.
Sourcepub fn step(&mut self, dt: f64)
pub fn step(&mut self, dt: f64)
Advance the simulation by dt seconds.
Uses symplectic Euler integration:
- Accumulate gravity + applied forces.
- Update velocity from forces.
- Apply damping.
- Update position from velocity.
- Integrate orientation from angular velocity.
- Run a simple sphere-sphere narrow-phase to detect contacts.
- Resolve contacts with impulse-based collision response.
- Update sleep state.
- Clear accumulated forces.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the world: remove all bodies, clear contacts, constraints, reset time.
Sourcepub fn step_substeps(&mut self, dt: f64, substeps: u32)
pub fn step_substeps(&mut self, dt: f64, substeps: u32)
Sourcepub fn bodies_in_aabb(&self, aabb: [f64; 6]) -> Vec<u32>
pub fn bodies_in_aabb(&self, aabb: [f64; 6]) -> Vec<u32>
Return the handles of all bodies whose centre-of-mass lies within aabb.
aabb is given as [xmin, ymin, zmin, xmax, ymax, zmax].
Sourcepub fn raycast(
&self,
origin: [f64; 3],
dir: [f64; 3],
max_dist: f64,
) -> Option<(u32, f64)>
pub fn raycast( &self, origin: [f64; 3], dir: [f64; 3], max_dist: f64, ) -> Option<(u32, f64)>
Cast a ray from origin in direction dir and return the handle and
hit distance of the nearest body whose sphere collider is struck.
Returns None if no body is hit within max_dist.
Direction dir need not be normalized; it is normalised internally.
Sourcepub fn contact_count(&self) -> usize
pub fn contact_count(&self) -> usize
Return the total number of contacts from the most recent step.
Sourcepub fn get_body_position(&self, handle: usize) -> Option<PyVec3>
pub fn get_body_position(&self, handle: usize) -> Option<PyVec3>
Legacy: get body position as PyVec3.
Sourcepub fn get_body_velocity(&self, handle: usize) -> Option<PyVec3>
pub fn get_body_velocity(&self, handle: usize) -> Option<PyVec3>
Legacy: get body velocity as PyVec3.
Sourcepub fn set_body_velocity(&mut self, handle: usize, vel: PyVec3)
pub fn set_body_velocity(&mut self, handle: usize, vel: PyVec3)
Legacy: set body velocity from PyVec3.
Sourcepub fn num_bodies(&self) -> usize
pub fn num_bodies(&self) -> usize
Legacy: return body count (same as body_count).
Sourcepub fn gravity_vec3(&self) -> PyVec3
pub fn gravity_vec3(&self) -> PyVec3
Legacy: return gravity as PyVec3.
Sourcepub fn all_positions_vec3(&self) -> Vec<PyVec3>
pub fn all_positions_vec3(&self) -> Vec<PyVec3>
Legacy: return all positions as VecPyVec3`.
Trait Implementations§
Source§impl Clone for PyPhysicsWorld
impl Clone for PyPhysicsWorld
Source§fn clone(&self) -> PyPhysicsWorld
fn clone(&self) -> PyPhysicsWorld
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PyPhysicsWorld
impl RefUnwindSafe for PyPhysicsWorld
impl Send for PyPhysicsWorld
impl Sync for PyPhysicsWorld
impl Unpin for PyPhysicsWorld
impl UnsafeUnpin for PyPhysicsWorld
impl UnwindSafe for PyPhysicsWorld
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<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.