Class ShapeFixtureBuilder
Builds Box2D shapes (fixtures) matching the toolkit's 2D primitive types, so a body's collision shape lines up with the rendered Primitive2DModelType 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
pointsVector2[]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.
bodyIdB2BodyIdThe body to attach to, usually static.
isLoopboolClose the chain from the last point back to the first.
frictionfloat?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
verticesVector2[]The polygon corners in local space; their convex hull is used. Box2D supports at most 8 hull vertices.
bodyIdB2BodyIdThe Box2D body to which the polygon will be attached.
shapeDefB2ShapeDef?Optional shape definition (density, friction, restitution, sensor flag). If
null, CreateDefaultShapeDef() is used.
Exceptions
- ArgumentException
Thrown when fewer than 3 vertices are provided.
AttachShape(Primitive2DModelType, Vector2, B2BodyId, B2ShapeDef?)
Creates and attaches a Box2D fixture matching the given primitive type and dimensions.
public static void AttachShape(Primitive2DModelType type, Vector2 size, B2BodyId bodyId, B2ShapeDef? shapeDef = null)
Parameters
typePrimitive2DModelTypeThe 2D primitive type describing the shape's geometry.
sizeVector2The shape dimensions: width/height for boxes and triangles, X as radius for circles, X as radius and Y as total height for capsules.
bodyIdB2BodyIdThe Box2D body to which the generated shape will be attached.
shapeDefB2ShapeDef?Optional shape definition (density, friction, restitution, sensor flag). If
null, CreateDefaultShapeDef() is used.
Examples
var bodyId = simulation.CreateStaticBody(entity, position);
ShapeFixtureBuilder.AttachShape(Primitive2DModelType.Circle, new Vector2(0.5f), bodyId);
Exceptions
- ArgumentException
Thrown if the shape type is not supported.
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
densityfloatMass density (kg/m^2). Higher values increase mass.
frictionfloatCoefficient of friction (typical range 0-1).
restitutionfloatBounciness (0 = inelastic, 1 = perfectly elastic).
isSensorboolIf 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(Primitive2DModelType.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.