Table of Contents

Class ShapeFixtureBuilder

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

Builds Box2D shapes (fixtures) matching the toolkit's 2D primitive shapes, so a body's collision shape lines up with the rendered Primitive2D mesh of the same size.

public static class ShapeFixtureBuilder
Inheritance
ShapeFixtureBuilder

Methods

AttachChain(Vector2[], B2BodyId, bool, float?)

Attaches a chain of segments to a body - terrain, a track, the walls of a room. Unlike a row of separate boxes or segments, a chain has no internal corners for a rolling or sliding body to catch on: Box2D smooths the joins between consecutive segments.

public static B2ChainId AttachChain(Vector2[] points, B2BodyId bodyId, bool isLoop = false, float? friction = null)

Parameters

points Vector2[]

The chain vertices in the body's local space, in order; every point is part of the chain. A chain is one-sided: it collides on the right of its direction of travel, so a floor to be stood on from above must be listed right to left, and a closed room must wind counter-clockwise.

bodyId B2BodyId

The body to attach to, usually static.

isLoop bool

Close the chain from the last point back to the first.

friction float?

Surface friction of every segment. Box2D's default when omitted.

Returns

B2ChainId

Remarks

Box2D wants an open chain to carry one extra point at each end - ghost vertices that shape the smoothing but are not collided. They are added here by extending the first and last segments, so the points given are exactly the chain that exists.

Exceptions

ArgumentException

Fewer than two points, or fewer than four for a loop.

AttachPolygon(Vector2[], B2BodyId, B2ShapeDef?)

Creates and attaches a convex polygon fixture from custom vertices.

public static void AttachPolygon(Vector2[] vertices, B2BodyId bodyId, B2ShapeDef? shapeDef = null)

Parameters

vertices Vector2[]

The polygon corners in local space; their convex hull is used. Box2D supports at most 8 hull vertices.

bodyId B2BodyId

The Box2D body to which the polygon will be attached.

shapeDef B2ShapeDef?

Optional shape definition (density, friction, restitution, sensor flag). If null, CreateDefaultShapeDef() is used.

Exceptions

ArgumentException

Thrown when fewer than 3 vertices are provided.

AttachShape(Primitive2D, B2BodyId, B2ShapeDef?)

Creates and attaches a Box2D fixture matching the given primitive shape and its dimensions.

public static void AttachShape(Primitive2D shape, B2BodyId bodyId, B2ShapeDef? shapeDef = null)

Parameters

shape Primitive2D

The 2D shape, from the Primitive2D factories.

bodyId B2BodyId

The Box2D body to which the generated shape will be attached.

shapeDef B2ShapeDef?

Optional shape definition (density, friction, restitution, sensor flag). If null, CreateDefaultShapeDef() is used.

Examples

var bodyId = simulation.CreateStaticBody(entity, position);
ShapeFixtureBuilder.AttachShape(Primitive2D.Circle(0.5f), bodyId);

Exceptions

ArgumentException

Thrown when shape is not one of the known shapes.

CreateCustomShapeDef(float, float, float, bool)

Creates a custom Box2D.NET.B2ShapeDef with explicitly specified physics material properties.

public static B2ShapeDef CreateCustomShapeDef(float density, float friction, float restitution, bool isSensor = false)

Parameters

density float

Mass density (kg/m^2). Higher values increase mass.

friction float

Coefficient of friction (typical range 0-1).

restitution float

Bounciness (0 = inelastic, 1 = perfectly elastic).

isSensor bool

If true, the shape detects contacts but produces no collision response.

Returns

B2ShapeDef

A shape definition initialized with the provided parameters.

Examples

var customDef = ShapeFixtureBuilder.CreateCustomShapeDef(2.0f, 0.6f, 0.1f, isSensor: true);
ShapeFixtureBuilder.AttachShape(Primitive2D.Square(size), bodyId, customDef);

CreateDefaultShapeDef()

Creates a Box2D.NET.B2ShapeDef with Box2D's own defaults (density 1, friction 0.6, restitution 0). Use CreateCustomShapeDef(float, float, float, bool) to specify material properties.

public static B2ShapeDef CreateDefaultShapeDef()

Returns

B2ShapeDef

A shape definition with Box2D default material properties.

InCollisionGroup(B2ShapeDef, int)

The shape definition with its shape in a collision group, so CollisionMatrix decides what the shape collides with. The group is Box2D's groupIndex and must be positive: zero is no group, and a negative group is Box2D's own "never collide with each other".

public static B2ShapeDef InCollisionGroup(this B2ShapeDef shapeDef, int group)

Parameters

shapeDef B2ShapeDef

The definition to put in the group; the copy returned is the one to attach shapes with.

group int

The group number, 1 or more.

Returns

B2ShapeDef

The definition in the group, with Box2D's custom filtering enabled so the matrix is consulted.

Exceptions

ArgumentOutOfRangeException

If group is not positive.