Table of Contents

Class PhysicsQueries2D

Namespace
Stride.CommunityToolkit.Box2D
Assembly
Stride.CommunityToolkit.Box2D.dll

Stateless helper methods for common Box2D query patterns. These wrappers avoid any Stride engine types and operate directly on world ids and primitive math structs only.

public static class PhysicsQueries2D
Inheritance
PhysicsQueries2D

Methods

CastCircleClosest(B2WorldId, Vector2, float, Vector2, B2QueryFilter)

Sweeps a circle (or a point, when radius is zero) from center along translation and returns the closest thing it strikes.

public static ShapeCastHit? CastCircleClosest(B2WorldId worldId, Vector2 center, float radius, Vector2 translation, B2QueryFilter filter)

Parameters

worldId B2WorldId

Target Box2D world.

center Vector2

World-space centre of the circle at the start of the sweep.

radius float

Circle radius; zero casts a point.

translation Vector2

The sweep, as a world-space displacement.

filter B2QueryFilter

Category and mask bits the struck shapes must satisfy.

Returns

ShapeCastHit?

The closest hit, or null when the sweep is clear.

CastSegmentClosest(B2WorldId, Vector2, Vector2, Vector2, B2QueryFilter)

Sweeps a line segment from a to b along translation and returns the closest thing it strikes.

public static ShapeCastHit? CastSegmentClosest(B2WorldId worldId, Vector2 a, Vector2 b, Vector2 translation, B2QueryFilter filter)

Parameters

worldId B2WorldId

Target Box2D world.

a Vector2

One end of the segment at the start of the sweep, in world space.

b Vector2

The other end.

translation Vector2

The sweep, as a world-space displacement.

filter B2QueryFilter

Category and mask bits the struck shapes must satisfy.

Returns

ShapeCastHit?

The closest hit, or null when the sweep is clear.

CastShapeClosest(B2WorldId, ref B2ShapeProxy, Vector2, B2QueryFilter)

Sweeps any convex proxy (see b2MakeProxy) along translation and returns the closest thing it strikes. The circle and segment overloads build the proxy for you.

public static ShapeCastHit? CastShapeClosest(B2WorldId worldId, ref B2ShapeProxy proxy, Vector2 translation, B2QueryFilter filter)

Parameters

worldId B2WorldId

Target Box2D world.

proxy B2ShapeProxy

The shape to sweep, already in world space.

translation Vector2

The sweep, as a world-space displacement.

filter B2QueryFilter

Category and mask bits the struck shapes must satisfy.

Returns

ShapeCastHit?

The closest hit, or null when the sweep is clear.

OverlapAABB(B2WorldId, Vector2, Vector2)

Collects all unique bodies whose shapes overlap the axis-aligned box defined by lowerBound and upperBound.

public static List<B2BodyId> OverlapAABB(B2WorldId worldId, Vector2 lowerBound, Vector2 upperBound)

Parameters

worldId B2WorldId

Target Box2D world.

lowerBound Vector2

Lower corner of the AABB.

upperBound Vector2

Upper corner of the AABB.

Returns

List<B2BodyId>

List of body ids (no duplicates).

OverlapCircle(B2WorldId, Vector2, float)

Collects all unique bodies whose shapes overlap a circle centered at center with radius.

public static List<B2BodyId> OverlapCircle(B2WorldId worldId, Vector2 center, float radius)

Parameters

worldId B2WorldId

Target Box2D world.

center Vector2

World-space circle center.

radius float

Circle radius.

Returns

List<B2BodyId>

List of body ids (no duplicates).

OverlapCircle(B2WorldId, Vector2, float, B2QueryFilter)

Collects all unique bodies whose shapes overlap a circle centered at center with radius, restricted to shapes whose category bits pass filter.

public static List<B2BodyId> OverlapCircle(B2WorldId worldId, Vector2 center, float radius, B2QueryFilter filter)

Parameters

worldId B2WorldId

Target Box2D world.

center Vector2

World-space circle center.

radius float

Circle radius.

filter B2QueryFilter

Category and mask bits the overlapped shapes must satisfy.

Returns

List<B2BodyId>

List of body ids (no duplicates).

OverlapPoint(B2WorldId, Vector2, float)

Tests whether any shape overlaps the given point by constructing a tiny AABB around it.

public static B2BodyId? OverlapPoint(B2WorldId worldId, Vector2 point, float querySize = 0.1)

Parameters

worldId B2WorldId

Target Box2D world.

point Vector2

The point to test.

querySize float

Half-extent of the temporary AABB used for the broad-phase query.

Returns

B2BodyId?

The first body id whose shape actually contains the point; null if none.

RaycastAll(B2WorldId, Vector2, Vector2, float)

Performs a raycast returning every hit encountered along the segment starting at origin in direction up to maxDistance.

public static List<QueryRaycastHit> RaycastAll(B2WorldId worldId, Vector2 origin, Vector2 direction, float maxDistance)

Parameters

worldId B2WorldId

Target Box2D world.

origin Vector2

World-space ray origin.

direction Vector2

Normalized direction vector.

maxDistance float

Maximum ray length.

Returns

List<QueryRaycastHit>

List of raw hit data (unsorted). Empty when nothing is hit.

RaycastClosest(B2WorldId, Vector2, Vector2, float)

Performs a closest-hit raycast against every shape in worldId along a segment starting at origin in direction up to maxDistance.

public static (bool hit, B2BodyId bodyId, B2ShapeId shapeId, Vector2 point, Vector2 normal, float fraction) RaycastClosest(B2WorldId worldId, Vector2 origin, Vector2 direction, float maxDistance)

Parameters

worldId B2WorldId

Target Box2D world.

origin Vector2

World-space ray origin.

direction Vector2

Normalized direction vector.

maxDistance float

Maximum ray length.

Returns

(bool hit, B2BodyId bodyId, B2ShapeId shapeId, Vector2 point, Vector2 normal, float fraction)

Tuple: (hit flag, body id, shape id, point, normal, fraction). When hit is false, the remaining values are unspecified.