Shape Module

This module defines high-level shape objects for Box2D. Each shape’s constructor now accepts a body and a shape definition. Each shape (except Chain) subclasses a common base that implements common properties. Each shape also has a classmethod “create” that matches the signature from before. Chain is now implemented as a separate class.

class box2d.shape.Box(body, shapedef)[source]

A box (rectangle) shape that can be attached to a body. Inherits from Polygon since a box is a special case of a convex polygon.

classmethod create(body, width, height, radius=0.0, offset=(0, 0), angle=0.0, density=None, friction=None, restitution=None, is_sensor=None, collision_filter=None, custom_color=None)[source]

Create and attach a box shape to a body. The parameters are the same as the previous initializer.

class box2d.shape.Capsule(body, shapedef)[source]

A capsule shape that can be attached to a body.

__init__(body, shapedef)[source]

Initialize a Capsule shape from a body and a CapsuleDef instance.

classmethod create(body, point1, point2, radius, density=None, friction=None, restitution=None, is_sensor=None, collision_filter=None, custom_color=None)[source]

Create and attach a capsule shape to a body. The parameters are the same as the previous initializer.

class box2d.shape.Chain(body, shapedef)[source]

A chain shape that can be attached to a body. Chain shapes are not a subclass of Shape and do not have the common shape methods because they are typically used for static boundaries and have no density/sensor properties.

property body

Return the body this chain is attached to.

classmethod create(body, vertices, loop=False, friction=None, restitution=None, collision_filter=None, custom_color=None)[source]

Create and attach a chain shape to a body.

class box2d.shape.Circle(body, shapedef)[source]

A circle shape that can be attached to a body.

__init__(body, shapedef)[source]

Initialize a Circle shape from a body and a CircleDef instance.

classmethod create(body, radius, center=(0, 0), density=None, friction=None, restitution=None, is_sensor=None, collision_filter=None, custom_color=None)[source]

Create and attach a circle shape to a body. The parameters are the same as the previous initializer.

class box2d.shape.Polygon(body, shapedef)[source]

A convex polygon shape that can be attached to a body.

__init__(body, shapedef)[source]

Initialize a Polygon shape from a body and a PolygonDef instance.

classmethod create(body, vertices, radius=0.0, density=None, friction=None, restitution=None, is_sensor=None, collision_filter=None, custom_color=None)[source]

Create and attach a polygon shape to a body. The parameters are the same as the previous initializer.

class box2d.shape.Segment(body, shapedef)[source]

A line segment shape that can be attached to a body.

__init__(body, shapedef)[source]

Initialize a Segment shape from a body and a SegmentDef instance.

classmethod create(body, point1, point2, density=None, friction=None, restitution=None, is_sensor=None, collision_filter=None, custom_color=None)[source]

Create and attach a segment shape to a body. The parameters are the same as the previous initializer.

class box2d.shape.Shape(body, shapedef)[source]

Base class for all non-chain shapes. Its constructor now accepts only a body and a shape definition. It provides common properties like density, friction, restitution, and a helper _finalize.

property body

Return the body to which this shape is attached.

property density

Get the mass density of the shape.

property friction

Get the friction coefficient of the shape.

property is_sensor

Check if this shape is a sensor.

property restitution

Get the restitution (bounciness) of the shape.