Class b2Body
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
Reference: Box2D/Dynamics/b2Body.cpp.
Properties
Active
Whether the body is active (pybox2d's active).
public bool Active { get; set; }
Property Value
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
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
AngularVelocity
The angular velocity (pybox2d's angularVelocity); setting a non-zero one wakes the body.
public float AngularVelocity { get; set; }
Property Value
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
Bullet
Whether the body is a bullet (pybox2d's bullet).
public bool Bullet { get; set; }
Property Value
Contacts
The contact edges, newest first (pybox2d's contacts); a new list on each read.
public IReadOnlyList<b2ContactEdge> Contacts { get; }
Property Value
FixedRotation
Whether the body may not rotate (pybox2d's fixedRotation); changing it recomputes the mass.
public bool FixedRotation { get; set; }
Property Value
Fixtures
The fixtures, newest first (pybox2d's fixtures); a new list on each read.
public IReadOnlyList<b2Fixture> Fixtures { get; }
Property Value
GravityScale
The gravity scale (pybox2d's gravityScale).
public float GravityScale { get; set; }
Property Value
Inertia
The rotational inertia about the body origin (pybox2d's inertia).
public float Inertia { get; }
Property Value
Joints
The joint edges, newest first (pybox2d's joints); a new list on each read.
public IReadOnlyList<b2JointEdge> Joints { get; }
Property Value
LinearDamping
The linear damping (pybox2d's linearDamping).
public float LinearDamping { get; set; }
Property Value
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
LocalCenter
The center of mass in local coordinates (pybox2d's localCenter).
public b2Vec2 LocalCenter { get; }
Property Value
Mass
The mass (pybox2d's mass).
public float Mass { get; }
Property Value
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
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
Transform
The body origin transform (pybox2d's transform); trans * vertex places a fixture vertex in the world.
public b2Transform Transform { get; }
Property Value
Type
The body type (pybox2d's type); changing it destroys the body's contacts.
public b2BodyType Type { get; set; }
Property Value
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
World
The world (pybox2d's world).
public b2World World { get; }
Property Value
WorldCenter
The center of mass in world coordinates (pybox2d's worldCenter).
public b2Vec2 WorldCenter { get; }
Property Value
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
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
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
forceb2Vec2The force in newtons.
pointb2Vec2The world point of application.
wakeboolWhether 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
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
impulseb2Vec2The impulse in N·s.
pointb2Vec2The world point of application.
wakeboolWhether 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
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
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
radiusdoubleThe radius.
posb2Vec2?The center in the body's frame (origin when null).
densitydoubleThe density.
frictiondoubleThe friction.
restitutiondoubleThe restitution.
isSensorboolWhether it is a sensor.
categoryBitsushortThe collision category bits.
maskBitsushortThe collision mask bits.
groupIndexshortThe 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
verticesIReadOnlyList<b2Vec2?>Two to four vertices, as Vertices takes them.
densitydoubleThe density.
frictiondoubleThe friction.
restitutiondoubleThe restitution.
isSensorboolWhether it is a sensor.
categoryBitsushortThe collision category bits.
maskBitsushortThe collision mask bits.
groupIndexshortThe 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
defb2FixtureDefThe 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
shapeb2ShapeThe shape; it is cloned.
densitydoubleThe density.
frictiondoubleThe friction.
restitutiondoubleThe restitution.
isSensorboolWhether it is a sensor.
categoryBitsushortThe collision category bits.
maskBitsushortThe collision mask bits.
groupIndexshortThe collision group.
userDataobjectThe 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
shapesIEnumerable<b2Shape>The shapes.
shapeFixtureb2FixtureDefThe 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
verticesIReadOnlyList<b2Vec2>The vertices, or null.
box(double hx, double hy)?The half-extents of an axis-aligned box, applied after
vertices.densitydoubleThe density.
frictiondoubleThe friction.
restitutiondoubleThe restitution.
isSensorboolWhether it is a sensor.
categoryBitsushortThe collision category bits.
maskBitsushortThe collision mask bits.
groupIndexshortThe 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
fixtureb2FixtureThe 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
localPointb2Vec2The 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
worldPointb2Vec2The world point.
Returns
- b2Vec2
The velocity.
GetLocalPoint(in b2Vec2)
A world point in local coordinates.
public b2Vec2 GetLocalPoint(in b2Vec2 worldPoint)
Parameters
worldPointb2Vec2The world point.
Returns
- b2Vec2
The local point.
GetLocalVector(in b2Vec2)
A world vector in local coordinates.
public b2Vec2 GetLocalVector(in b2Vec2 worldVector)
Parameters
worldVectorb2Vec2The world vector.
Returns
- b2Vec2
The local vector.
GetWorldPoint(in b2Vec2)
A local point in world coordinates.
public b2Vec2 GetWorldPoint(in b2Vec2 localPoint)
Parameters
localPointb2Vec2The local point.
Returns
- b2Vec2
The world point.
GetWorldVector(in b2Vec2)
A local vector in world coordinates.
public b2Vec2 GetWorldVector(in b2Vec2 localVector)
Parameters
localVectorb2Vec2The 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
Exceptions
- AssertionError
Thrown while the world is stepping.