Scene.PhysicsWorld (or Game.PhysicsTrace for scene-level traces). Use PhysicsWorld.Trace to build and run queries against the world, then read the result from PhysicsTraceResult.
PhysicsWorld
PhysicsWorld represents the physics simulation for a scene. You rarely construct one yourself — the active scene provides one automatically.
Properties
The gravitational acceleration applied to all dynamic bodies. Default is
(0, 0, -800) (Source engine units).Air density used for drag calculations on dynamic bodies.
When
true, bodies that have been inactive for a while are put to sleep to save CPU. Default is true.Number of physics sub-steps executed per simulation tick. Increase to reduce tunnelling at the cost of performance. Default is
1.Controls collision detection mode.
Discrete is faster; Continuous prevents fast-moving bodies from tunnelling through thin geometry.Returns a new
PhysicsTraceBuilder targeting this world. Use it to construct and run trace queries.All valid physics bodies currently in this world.
Gravity example
PhysicsTraceBuilder
PhysicsTraceBuilder is a fluent builder for constructing physics queries. Obtain one from PhysicsWorld.Trace, chain shape and filter methods, then call Run() or RunAll().
Shape methods
Cast an infinitely thin ray from
from to to.Cast a ray from a
Ray struct up to distance units.Sweep a sphere of
radius from from to to.Sweep a sphere along a
Ray up to distance units.Sweep an axis-aligned box with the given
extents (half-sizes) from from to to.Sweep an axis-aligned box defined by a
BBox from from to to.Sweep a capsule from
from to to.Sweep a capsule along a
Ray up to distance units.Filter methods
Exclude static (immovable) physics objects from the query.
Exclude dynamic physics bodies from the query.
Exclude keyframed bodies (moved by code, not simulation) from the query.
Include trigger volumes in the query in addition to solid geometry.
Return only trigger volume hits, ignoring solid geometry.
Execution methods
Execute the trace and return the first (closest) hit.
Execute the trace and return all hits, sorted by distance.
Run the trace against a specific capsule at a given transform, ignoring all other physics objects.
Run the trace against a specific sphere at a given transform.
Run the trace against a specific bounding box at a given transform.
PhysicsTraceResult
PhysicsTraceResult is returned by Run() and RunAll(). Always check Hit before reading geometry data.
Fields
true if the trace hit something. Always check this before reading any hit data.true if the trace origin was already inside a solid object.World-space position where the trace first intersected geometry.
World-space origin of the trace.
World-space end position of the trace (the hit point, or the target if there was no hit).
Surface normal at the hit point, pointing away from the hit surface.
Distance between
StartPosition and EndPosition.A value in
[0..1] indicating how far along the trace the hit occurred. 1.0 means no hit.The physics body that was hit, or
null if the trace hit world geometry.The specific physics shape that was hit.
The physical material properties of the hit surface (footstep sounds, impact effects, etc.).
Tags applied to the hit shape’s physics layer.
Triangle index of the hit mesh shape.
-1 for non-mesh shapes.Convenience accessor. Returns
Body?.GameObject — the GameObject associated with the hit body.PhysicsBody
PhysicsBody represents a single rigid body in the physics world. Obtain one from PhysicsTraceResult.Body, from a Rigidbody component, or create one manually.
Motion properties
Linear velocity of the body in world space. Get or set directly.
Angular velocity of the body in world space (radians per second around each axis). Get or set directly.
Mass of the body in kilograms. Affects how forces change velocity.
Whether gravity is applied to this body. Default is
true.Multiplier applied to the world gravity for this body.
2.0 is double gravity.Coefficient of linear drag — how quickly the body decelerates on its own.
Coefficient of rotational drag.
Movement type:
Static, Keyframed, or Dynamic.Get or set the sleep state. Set to
false to wake the body from sleep.Force methods
Apply a continuous force at the center of mass. Force is scaled by the physics timestep, so this is appropriate for forces applied every tick (engines, wind, etc.).
Apply a continuous force at a world-space position. Can impart angular velocity.
Apply an instantaneous impulse at the center of mass. Appropriate for one-shot events like bullet impacts or jumps.
Apply an instantaneous impulse at a world-space position.
Apply an instantaneous angular impulse (spin kick).
Apply a continuous torque (rotational force) scaled by the physics timestep.
Utility methods
Returns the world-space velocity at a specific point on the body, accounting for both linear and angular velocity. Useful for ragdoll and contact calculations.
Move the body toward
position using velocity damping over timeToArrive seconds. Works well for grabbed objects and kinematic movement.Returns the nearest point on the surface of the body to the given world-space position.
Returns the axis-aligned world-space bounding box of the body.
Practical examples
- Raycast
- Sphere sweep
- Box sweep
- Apply forces
When checking what was hit you often only need
result.Body?.GameObject to get the GameObject. If the hit body belongs to the world (static map geometry), Body will be non-null but Body.GameObject may be null.