Table of Contents

Class b2Body

Namespace
Gym.Box2D
Assembly
Gym.Environments.dll

A rigid body. Created by b2World, never directly; its fixtures, contacts and joints die with it.

public class b2Body
Inheritance
b2Body
Inherited Members
Extension Methods

Remarks

Properties

Active

Whether the body is active (pybox2d's active).

public bool Active { get; set; }

Property Value

bool

Exceptions

AssertionError

Thrown by the setter while the world is stepping.

Angle

The angle in radians, not normalized (pybox2d's angle); setting it calls SetTransform(position, angle).

public float Angle { get; set; }

Property Value

float

Exceptions

AssertionError

Thrown by the setter while the world is stepping.

AngularDamping

The angular damping (pybox2d's angularDamping).

public float AngularDamping { get; set; }

Property Value

float

AngularVelocity

The angular velocity (pybox2d's angularVelocity); setting a non-zero one wakes the body.

public float AngularVelocity { get; set; }

Property Value

float

Awake

Whether the body is awake (pybox2d's awake); putting it to sleep zeroes its velocities and forces.

public bool Awake { get; set; }

Property Value

bool

Bullet

Whether the body is a bullet (pybox2d's bullet).

public bool Bullet { get; set; }

Property Value

bool

Contacts

The contact edges, newest first (pybox2d's contacts); a new list on each read.

public IReadOnlyList<b2ContactEdge> Contacts { get; }

Property Value

IReadOnlyList<b2ContactEdge>

FixedRotation

Whether the body may not rotate (pybox2d's fixedRotation); changing it recomputes the mass.

public bool FixedRotation { get; set; }

Property Value

bool

Fixtures

The fixtures, newest first (pybox2d's fixtures); a new list on each read.

public IReadOnlyList<b2Fixture> Fixtures { get; }

Property Value

IReadOnlyList<b2Fixture>

GravityScale

The gravity scale (pybox2d's gravityScale).

public float GravityScale { get; set; }

Property Value

float

Inertia

The rotational inertia about the body origin (pybox2d's inertia).

public float Inertia { get; }

Property Value

float

Joints

The joint edges, newest first (pybox2d's joints); a new list on each read.

public IReadOnlyList<b2JointEdge> Joints { get; }

Property Value

IReadOnlyList<b2JointEdge>

LinearDamping

The linear damping (pybox2d's linearDamping).

public float LinearDamping { get; set; }

Property Value

float

LinearVelocity

The center of mass's linear velocity (pybox2d's linearVelocity); setting a non-zero one wakes the body.

public b2Vec2 LinearVelocity { get; set; }

Property Value

b2Vec2

LocalCenter

The center of mass in local coordinates (pybox2d's localCenter).

public b2Vec2 LocalCenter { get; }

Property Value

b2Vec2

Mass

The mass (pybox2d's mass).

public float Mass { get; }

Property Value

float

Position

The body origin's world position (pybox2d's position). Setting it calls SetTransform(pos, angle), which re-synchronizes the proxies and finds new contacts at once.

public b2Vec2 Position { get; set; }

Property Value

b2Vec2

Exceptions

AssertionError

Thrown by the setter while the world is stepping.

SleepingAllowed

Whether the body may sleep (pybox2d's sleepingAllowed).

public bool SleepingAllowed { get; set; }

Property Value

bool

Transform

The body origin transform (pybox2d's transform); trans * vertex places a fixture vertex in the world.

public b2Transform Transform { get; }

Property Value

b2Transform

Type

The body type (pybox2d's type); changing it destroys the body's contacts.

public b2BodyType Type { get; set; }

Property Value

b2BodyType

Exceptions

AssertionError

Thrown by the setter while the world is stepping.

UserData

The user data (pybox2d's userData): any object, such as the env's own record of the body.

public object? UserData { get; set; }

Property Value

object

World

The world (pybox2d's world).

public b2World World { get; }

Property Value

b2World

WorldCenter

The center of mass in world coordinates (pybox2d's worldCenter).

public b2Vec2 WorldCenter { get; }

Property Value

b2Vec2

Methods

ApplyAngularImpulse(double, bool)

Applies an angular impulse given as a Python float, rounded to float32 as SWIG converts it.

public void ApplyAngularImpulse(double impulse, bool wake)

Parameters

impulse double

The angular impulse.

wake bool

Whether to wake the body.

Exceptions

OverflowError

Thrown for an impulse beyond the float32 range.

ApplyAngularImpulse(float, bool)

Applies an angular impulse.

public void ApplyAngularImpulse(float impulse, bool wake)

Parameters

impulse float

The angular impulse in kg·m²/s.

wake bool

Whether to wake the body.

ApplyForce(in b2Vec2, in b2Vec2, bool)

Adds a force at a world point (wakes the body when wake); a sleeping body that is not woken ignores it, and only dynamic bodies take forces.

public void ApplyForce(in b2Vec2 force, in b2Vec2 point, bool wake)

Parameters

force b2Vec2

The force in newtons.

point b2Vec2

The world point of application.

wake bool

Whether to wake the body.

ApplyForceToCenter(in b2Vec2, bool)

Adds a force at the center of mass.

public void ApplyForceToCenter(in b2Vec2 force, bool wake)

Parameters

force b2Vec2

The force in newtons.

wake bool

Whether to wake the body.

ApplyLinearImpulse(in b2Vec2, in b2Vec2, bool)

Applies an impulse at a world point: the velocities change at once.

public void ApplyLinearImpulse(in b2Vec2 impulse, in b2Vec2 point, bool wake)

Parameters

impulse b2Vec2

The impulse in N·s.

point b2Vec2

The world point of application.

wake bool

Whether to wake the body.

ApplyTorque(double, bool)

Adds a torque given as a Python float, rounded to float32 as SWIG converts it.

public void ApplyTorque(double torque, bool wake)

Parameters

torque double

The torque in N·m.

wake bool

Whether to wake the body.

Exceptions

OverflowError

Thrown for a torque beyond the float32 range.

ApplyTorque(float, bool)

Adds a torque about the z-axis.

public void ApplyTorque(float torque, bool wake)

Parameters

torque float

The torque in N·m.

wake bool

Whether to wake the body.

CreateCircleFixture(double, b2Vec2?, double, double, double, bool, ushort, ushort, short)

Creates a circle fixture without a definition (pybox2d's CreateCircleFixture(radius=..., pos=..., ...)); a refused argument raises pybox2d's Failed on kwargs, class="b2CircleShape" key="radius": ….

public b2Fixture CreateCircleFixture(double radius, b2Vec2? pos = null, double density = 0, double friction = 0.2, double restitution = 0, bool isSensor = false, ushort categoryBits = 1, ushort maskBits = 65535, short groupIndex = 0)

Parameters

radius double

The radius.

pos b2Vec2?

The center in the body's frame (origin when null).

density double

The density.

friction double

The friction.

restitution double

The restitution.

isSensor bool

Whether it is a sensor.

categoryBits ushort

The collision category bits.

maskBits ushort

The collision mask bits.

groupIndex short

The collision group.

Returns

b2Fixture

The fixture.

Exceptions

AssertionError

Thrown while the world is stepping.

OverflowError

Thrown for the radius or a material scalar beyond the float32 range.

CreateEdgeFixture(IReadOnlyList<b2Vec2?>, double, double, double, bool, ushort, ushort, short)

Creates an edge fixture without a definition (pybox2d's CreateEdgeFixture(vertices=..., ...)); a refused argument raises pybox2d's Failed on kwargs, class="b2EdgeShape" key="vertices": ….

public b2Fixture CreateEdgeFixture(IReadOnlyList<b2Vec2?> vertices, double density = 0, double friction = 0.2, double restitution = 0, bool isSensor = false, ushort categoryBits = 1, ushort maskBits = 65535, short groupIndex = 0)

Parameters

vertices IReadOnlyList<b2Vec2?>

Two to four vertices, as Vertices takes them.

density double

The density.

friction double

The friction.

restitution double

The restitution.

isSensor bool

Whether it is a sensor.

categoryBits ushort

The collision category bits.

maskBits ushort

The collision mask bits.

groupIndex short

The collision group.

Returns

b2Fixture

The fixture.

Exceptions

ValueError

Thrown for other than 2 to 4 vertices.

AssertionError

Thrown while the world is stepping.

OverflowError

Thrown for a material scalar beyond the float32 range.

CreateFixture(b2FixtureDef)

Creates a fixture from a definition and attaches it (newest first); a dense fixture updates the mass, and the world finds its contacts at the next step.

public b2Fixture CreateFixture(b2FixtureDef def)

Parameters

def b2FixtureDef

The definition; its shape is cloned.

Returns

b2Fixture

The fixture.

Exceptions

AssertionError

Thrown while the world is stepping.

CreateFixture(b2Shape, double, double, double, bool, ushort, ushort, short, object?)

Creates a fixture from a shape and material (pybox2d's CreateFixture(shape=..., ...)): a b2FixtureDef of these arguments.

public b2Fixture CreateFixture(b2Shape shape, double density = 0, double friction = 0.2, double restitution = 0, bool isSensor = false, ushort categoryBits = 1, ushort maskBits = 65535, short groupIndex = 0, object? userData = null)

Parameters

shape b2Shape

The shape; it is cloned.

density double

The density.

friction double

The friction.

restitution double

The restitution.

isSensor bool

Whether it is a sensor.

categoryBits ushort

The collision category bits.

maskBits ushort

The collision mask bits.

groupIndex short

The collision group.

userData object

The user data.

Returns

b2Fixture

The fixture.

Exceptions

AssertionError

Thrown while the world is stepping.

CreateFixturesFromShapes(IEnumerable<b2Shape>, b2FixtureDef?)

Creates one fixture per shape with a shared material (pybox2d's CreateFixturesFromShapes): the material's shape is replaced for each and restored afterwards, even on failure.

public IReadOnlyList<b2Fixture> CreateFixturesFromShapes(IEnumerable<b2Shape> shapes, b2FixtureDef? shapeFixture = null)

Parameters

shapes IEnumerable<b2Shape>

The shapes.

shapeFixture b2FixtureDef

The material (Box2D's defaults when null).

Returns

IReadOnlyList<b2Fixture>

The fixtures, in order.

Exceptions

AssertionError

Thrown while the world is stepping.

CreatePolygonFixture(IReadOnlyList<b2Vec2>?, (double hx, double hy)?, double, double, double, bool, ushort, ushort, short)

Creates a polygon fixture without a definition (pybox2d's CreatePolygonFixture(vertices=... or box=..., ...)); a refused argument raises pybox2d's Failed on kwargs, class="b2PolygonShape" key="box": ….

public b2Fixture CreatePolygonFixture(IReadOnlyList<b2Vec2>? vertices = null, (double hx, double hy)? box = null, double density = 0, double friction = 0.2, double restitution = 0, bool isSensor = false, ushort categoryBits = 1, ushort maskBits = 65535, short groupIndex = 0)

Parameters

vertices IReadOnlyList<b2Vec2>

The vertices, or null.

box (double hx, double hy)?

The half-extents of an axis-aligned box, applied after vertices.

density double

The density.

friction double

The friction.

restitution double

The restitution.

isSensor bool

Whether it is a sensor.

categoryBits ushort

The collision category bits.

maskBits ushort

The collision mask bits.

groupIndex short

The collision group.

Returns

b2Fixture

The fixture.

Remarks

Footgun, as in pybox2d: with neither vertices nor box the fixture gets an empty polygon, accepted at density 0 and refused by Box2D's mass assertion (m_count >= 3) above it.

Exceptions

ValueError

Thrown for a vertex count outside 2..16.

AssertionError

Thrown while the world is stepping, or for a degenerate or (with a density) empty polygon.

OverflowError

Thrown for a half-extent or material scalar beyond the float32 range.

DestroyFixture(b2Fixture)

Destroys a fixture and its contacts, then recomputes the mass.

public void DestroyFixture(b2Fixture fixture)

Parameters

fixture b2Fixture

The fixture.

Exceptions

AssertionError

Thrown while the world is stepping, or for a fixture of another body.

GetLinearVelocityFromLocalPoint(in b2Vec2)

The velocity of a local point attached to the body.

public b2Vec2 GetLinearVelocityFromLocalPoint(in b2Vec2 localPoint)

Parameters

localPoint b2Vec2

The local point.

Returns

b2Vec2

The velocity.

GetLinearVelocityFromWorldPoint(in b2Vec2)

The velocity of a world point attached to the body.

public b2Vec2 GetLinearVelocityFromWorldPoint(in b2Vec2 worldPoint)

Parameters

worldPoint b2Vec2

The world point.

Returns

b2Vec2

The velocity.

GetLocalPoint(in b2Vec2)

A world point in local coordinates.

public b2Vec2 GetLocalPoint(in b2Vec2 worldPoint)

Parameters

worldPoint b2Vec2

The world point.

Returns

b2Vec2

The local point.

GetLocalVector(in b2Vec2)

A world vector in local coordinates.

public b2Vec2 GetLocalVector(in b2Vec2 worldVector)

Parameters

worldVector b2Vec2

The world vector.

Returns

b2Vec2

The local vector.

GetWorldPoint(in b2Vec2)

A local point in world coordinates.

public b2Vec2 GetWorldPoint(in b2Vec2 localPoint)

Parameters

localPoint b2Vec2

The local point.

Returns

b2Vec2

The world point.

GetWorldVector(in b2Vec2)

A local vector in world coordinates.

public b2Vec2 GetWorldVector(in b2Vec2 localVector)

Parameters

localVector b2Vec2

The local vector.

Returns

b2Vec2

The world vector.

ResetMassData()

Recomputes the mass, center and inertia from the fixtures' densities; static and kinematic bodies get zero mass, a dynamic body without dense fixtures a mass of 1, and the center's velocity follows the moved center.

public void ResetMassData()

Exceptions

AssertionError

Thrown when a dynamic body's centered inertia is not positive.

SetTransform(in b2Vec2, float)

Moves the body origin to a position and angle at once, re-synchronizing the proxies and finding new contacts immediately; the velocities are kept.

public void SetTransform(in b2Vec2 position, float angle)

Parameters

position b2Vec2

The world position of the body origin.

angle float

The angle in radians.

Exceptions

AssertionError

Thrown while the world is stepping.