Class Box2DSimulation
High-level integration facade between Box2D.NET and Stride entities. Wraps a reusable PhysicsWorld2D plus an entity/body bridge and exposes a simplified stepping, query and event API, mirroring the role BepuSimulation plays for Bepu physics in Stride.
public sealed class Box2DSimulation : IDisposable
- Inheritance
-
Box2DSimulation
- Implements
Constructors
Box2DSimulation(PhysicsStepSettings?)
Creates a new simulation instance with a fresh Box2D world.
public Box2DSimulation(PhysicsStepSettings? settings = null)
Parameters
settingsPhysicsStepSettingsOptional stepping and threading configuration; defaults to PhysicsStepSettings defaults.
Properties
EnableContactEvents
Enable dispatch of begin/end contact events.
public bool EnableContactEvents { get; set; }
Property Value
EnableHitEvents
Enable dispatch of hit (post-solve / impact style) events.
public bool EnableHitEvents { get; set; }
Property Value
EnableSensorEvents
Enable dispatch of sensor overlap events.
public bool EnableSensorEvents { get; set; }
Property Value
Enabled
Whether simulation stepping and event processing should occur.
public bool Enabled { get; set; }
Property Value
Gravity
World gravity applied to dynamic bodies.
public Vector2 Gravity { get; set; }
Property Value
Joints
Joint factories with world-space anchors, for body ids or the entities this simulation created bodies for. See Joints2D for the joint types and their options.
public SimulationJoints2D Joints { get; }
Property Value
MaxStepsPerFrame
Maximum number of fixed steps processed per frame, to avoid the spiral of death.
public int MaxStepsPerFrame { get; set; }
Property Value
TargetHz
Target simulation frequency in hertz used to derive the fixed step size.
public int TargetHz { get; set; }
Property Value
TimeScale
Multiplier applied to incoming delta time before fixed-step accumulation.
public float TimeScale { get; set; }
Property Value
Methods
CastCircleClosest(Vector2, float, Vector2, B2QueryFilter)
Sweeps a circle (or a point, when radius is zero) along translation
and returns the closest thing it strikes.
public ShapeCastHit? CastCircleClosest(Vector2 center, float radius, Vector2 translation, B2QueryFilter filter)
Parameters
centerVector2World-space centre of the circle at the start of the sweep.
radiusfloatCircle radius; zero casts a point.
translationVector2The sweep, as a world-space displacement.
filterB2QueryFilterCategory and mask bits the struck shapes must satisfy.
Returns
- ShapeCastHit?
The closest hit, or null when the sweep is clear.
CastSegmentClosest(Vector2, Vector2, Vector2, B2QueryFilter)
Sweeps a line segment along translation and returns the closest thing it strikes.
public ShapeCastHit? CastSegmentClosest(Vector2 a, Vector2 b, Vector2 translation, B2QueryFilter filter)
Parameters
aVector2One end of the segment at the start of the sweep, in world space.
bVector2The other end.
translationVector2The sweep, as a world-space displacement.
filterB2QueryFilterCategory and mask bits the struck shapes must satisfy.
Returns
- ShapeCastHit?
The closest hit, or null when the sweep is clear.
CreateDynamicBody(Vector3, float)
Creates a dynamic body without an associated entity at a world position.
public B2BodyId CreateDynamicBody(Vector3 position, float rotation = 0)
Parameters
positionVector3Initial world position (Z is ignored).
rotationfloatInitial rotation in radians about the Z axis.
Returns
- B2BodyId
The id of the created body.
CreateDynamicBody(Entity, Vector3, float)
Creates a dynamic body associated with the given entity at a world position.
public B2BodyId CreateDynamicBody(Entity entity, Vector3 position, float rotation = 0)
Parameters
entityEntityThe entity driven by the body.
positionVector3Initial world position (Z is ignored).
rotationfloatInitial rotation in radians about the Z axis.
Returns
- B2BodyId
The id of the created body.
CreateKinematicBody(Vector3, float)
Creates a kinematic body without an associated entity at a world position.
public B2BodyId CreateKinematicBody(Vector3 position, float rotation = 0)
Parameters
positionVector3Initial world position (Z is ignored).
rotationfloatInitial rotation in radians about the Z axis.
Returns
- B2BodyId
The id of the created body.
CreateKinematicBody(Entity, Vector3, float)
Creates a kinematic body associated with the given entity at a world position.
public B2BodyId CreateKinematicBody(Entity entity, Vector3 position, float rotation = 0)
Parameters
entityEntityThe entity driven by the body.
positionVector3Initial world position (Z is ignored).
rotationfloatInitial rotation in radians about the Z axis.
Returns
- B2BodyId
The id of the created body.
CreateStaticBody(Vector3, float)
Creates a static body without an associated entity at a world position.
public B2BodyId CreateStaticBody(Vector3 position, float rotation = 0)
Parameters
positionVector3Initial world position (Z is ignored).
rotationfloatInitial rotation in radians about the Z axis.
Returns
- B2BodyId
The id of the created body.
CreateStaticBody(Entity, Vector3, float)
Creates a static body associated with the given entity at a world position.
public B2BodyId CreateStaticBody(Entity entity, Vector3 position, float rotation = 0)
Parameters
entityEntityThe entity driven by the body.
positionVector3Initial world position (Z is ignored).
rotationfloatInitial rotation in radians about the Z axis.
Returns
- B2BodyId
The id of the created body.
Dispose()
Disposes underlying world resources.
public void Dispose()
Explode(Vector2, float, float, float, ulong)
A radial blast: every shape within radius of position
receives an impulse away from it.
public void Explode(Vector2 position, float radius, float impulsePerLength, float falloff = 0, ulong maskBits = 18446744073709551615)
Parameters
positionVector2World-space centre of the blast.
radiusfloatFull-strength radius, in metres.
impulsePerLengthfloatImpulse per metre of shape perimeter facing the blast, in newton-seconds per metre. A broad flat body therefore takes more than a small round one of the same mass, which is what makes the effect read as a pressure wave rather than a point kick.
fallofffloatDistance beyond the radius over which the impulse fades to nothing, in metres.
maskBitsulongCollision category mask of the shapes affected; everything by default.
GetAllBodyIds()
Returns a snapshot list of all body ids tracked by the bridge.
public List<B2BodyId> GetAllBodyIds()
Returns
- List<B2BodyId>
A new list with every tracked body id.
GetEntity(B2BodyId)
Gets the entity previously registered for the given body id (or null).
public Entity? GetEntity(B2BodyId bodyId)
Parameters
bodyIdB2BodyIdThe body to look up.
Returns
- Entity
The associated entity, or null when the body has none.
GetWorldId()
Gets the underlying Box2D world id.
public B2WorldId GetWorldId()
Returns
- B2WorldId
The native world id.
OverlapAABB(Vector2, Vector2)
Overlaps an AABB and returns all bodies within it.
public List<B2BodyId> OverlapAABB(Vector2 lowerBound, Vector2 upperBound)
Parameters
Returns
- List<B2BodyId>
List of body ids (no duplicates).
OverlapCircle(Vector2, float)
Overlaps a circle and returns all bodies within it.
public List<B2BodyId> OverlapCircle(Vector2 center, float radius)
Parameters
Returns
- List<B2BodyId>
List of body ids (no duplicates).
OverlapCircle(Vector2, float, B2QueryFilter)
Overlaps a circle and returns all bodies within it whose shapes pass filter.
public List<B2BodyId> OverlapCircle(Vector2 center, float radius, B2QueryFilter filter)
Parameters
centerVector2World-space circle center.
radiusfloatCircle radius.
filterB2QueryFilterCategory and mask bits the overlapped shapes must satisfy.
Returns
- List<B2BodyId>
List of body ids (no duplicates).
OverlapPoint(Vector2, float)
Tests if a point overlaps with any physics shape in the world.
public B2BodyId? OverlapPoint(Vector2 point, float querySize = 0.1)
Parameters
Returns
- B2BodyId?
The body id that was hit, or null if nothing was hit.
Raycast(Vector2, Vector2, float)
Performs a raycast and returns the first hit.
public RaycastHit? Raycast(Vector2 origin, Vector2 direction, float maxDistance)
Parameters
originVector2Ray origin.
directionVector2Ray direction (normalized).
maxDistancefloatMaximum distance to cast.
Returns
- RaycastHit?
First hit or null if no hit.
RaycastAll(Vector2, Vector2, float)
Performs a raycast and returns all hits along the ray.
public List<RaycastHit> RaycastAll(Vector2 origin, Vector2 direction, float maxDistance)
Parameters
originVector2Ray origin.
directionVector2Ray direction (normalized).
maxDistancefloatMaximum distance to cast.
Returns
- List<RaycastHit>
List of all hits along the ray.
RegisterContactEventHandler(IContactEventHandler)
Registers a contact event handler.
public void RegisterContactEventHandler(IContactEventHandler handler)
Parameters
handlerIContactEventHandlerThe handler to receive contact events.
RegisterSensorEventHandler(ISensorEventHandler)
Registers a sensor event handler.
public void RegisterSensorEventHandler(ISensorEventHandler handler)
Parameters
handlerISensorEventHandlerThe handler to receive sensor events.
RegisterSimulationUpdate(IBox2DSimulationUpdate)
Registers a handler whose SimulationUpdate(Box2DSimulation, float) runs before and AfterSimulationUpdate(Box2DSimulation, float) after every fixed step; a handler is only registered once.
public void RegisterSimulationUpdate(IBox2DSimulationUpdate handler)
Parameters
handlerIBox2DSimulationUpdateThe handler to invoke around each fixed step.
RemoveBody(Entity)
Removes the body associated with entity from the world if present.
public void RemoveBody(Entity entity)
Parameters
entityEntityThe entity whose body should be removed.
UnregisterContactEventHandler(IContactEventHandler)
Unregisters a contact event handler.
public void UnregisterContactEventHandler(IContactEventHandler handler)
Parameters
handlerIContactEventHandlerThe handler to remove.
UnregisterSensorEventHandler(ISensorEventHandler)
Unregisters a sensor event handler.
public void UnregisterSensorEventHandler(ISensorEventHandler handler)
Parameters
handlerISensorEventHandlerThe handler to remove.
UnregisterSimulationUpdate(IBox2DSimulationUpdate)
Unregisters a simulation update handler.
public void UnregisterSimulationUpdate(IBox2DSimulationUpdate handler)
Parameters
handlerIBox2DSimulationUpdateThe handler to remove.
Update(TimeSpan)
Advances the simulation by the elapsed real time, executing zero or more fixed steps.
public void Update(TimeSpan elapsed)
Parameters
elapsedTimeSpanFrame time delta.