Class b2World

The world class manages all physics entities, dynamic simulation, and asynchronous queries.

Index

Constructor methods

Properties

Methods

Constructor methods

constructor(gravity: b2Vec2, doSleep: boolean): b2World

Creates a new world.

Parameters

  • gravity: b2Vec2

    The world gravity vector.

  • doSleep: boolean

    Improvie performance by not simulating inactive bodies.

Returns

b2World

Properties

public static e_locked: number

Locked

public static e_newFixture: number

New Fixture

Methods

public AddController(c: b2Controller): b2Controller

Add a controller to the world list.

Parameters

Returns

b2Controller

Controller that was added to the world.

public ClearForces()

Call this after you are done with time steps to clear the forces. You normally call this after each call to Step, unless you are performing sub-steps.

public CreateBody(def: b2BodyDef): b2Body

Create a rigid body given a definition. No reference to the definition is retained.

Parameters

Returns

b2Body

Created rigid body.

public CreateController(controller: b2Controller): b2Controller

Creates a new controller.

Parameters

Returns

b2Controller

New controller.

public CreateJoint(def: b2JointDef): b2Joint

Create a joint to constrain bodies together. No reference to the definition is retained. This may cause the connected bodies to cease colliding.

warning

This function is locked during callbacks.

Parameters

Returns

b2Joint

New created joint.

public DestroyBody(b: b2Body)

Destroy a rigid body given a definition. No reference to the definition is retained. This function is locked during callbacks.

warning

This function is locked during callbacks.

Parameters

public DestroyController(controller: b2Controller)

Destroy a controller given the controller instance.

warning

This function is locked during callbacks.

Parameters

public DestroyJoint(j: b2Joint)

Destroy a joint. This may cause the connected bodies to begin colliding.

Parameters

public DrawDebugData()

Call this to draw shapes and other debug draw data.

public GetBodyCount(): number

Get the number of bodies.

Returns

number

Number of bodies in this world.

public GetBodyList(): b2Body

Get the world body list. With the returned body, use b2Body::GetNext to get the next body in the world list. A NULL body indicates the end of the list.

Returns

b2Body

The head of the world body list.

public GetContactCount(): number

Get the number of contacts (each may have 0 or more contact points).

Returns

number

Number of contacts.

public GetContactList(): b2Contact

Get the world contact list. With the returned contact, use b2Contact::GetNext to get the next contact in the world list. A NULL contact indicates the end of the list.

Returns

b2Contact

The head of the world contact list.

public GetGravity(): b2Vec2

Get the global gravity vector.

Returns

b2Vec2

Global gravity vector.

public GetGroundBody(): b2Body

The world provides a single static ground body with no collision shapes. You can use this to simplify the creation of joints and static shapes.

Returns

b2Body

The ground body.

public GetJointCount(): number

Get the number of joints.

Returns

number

The number of joints in the world.

public GetJointList(): b2Joint

Get the world joint list. With the returned joint, use b2Joint::GetNext to get the next joint in the world list. A NULL joint indicates the end of the list.

Returns

b2Joint

The head of the world joint list.

public GetProxyCount(): number

Get the number of broad-phase proxies.

Returns

number

Number of borad-phase proxies.

public IsLocked(): boolean

Is the world locked (in the middle of a time step).

Returns

boolean

True if the world is locked and in the middle of a time step, otherwise false.

public QueryAABB(callback: (fixutre: Box2D.Dynamics.b2Fixture) => boolean, aabb: b2AABB)

Query the world for all fixtures that potentially overlap the provided AABB.

Parameters

  • callback: (fixutre: Box2D.Dynamics.b2Fixture) => boolean

    A user implemented callback class. It should match signature function Callback(fixture:b2Fixture):Boolean. Return true to continue to the next fixture.

  • aabb: b2AABB

    The query bounding box.

public QueryPoint(callback: (fixture: Box2D.Dynamics.b2Fixture) => boolean, p: b2Vec2)

Query the world for all fixtures that contain a point.

note

This provides a feature specific to this port.

Parameters

  • callback: (fixture: Box2D.Dynamics.b2Fixture) => boolean

    A user implemented callback class. It should match signature function Callback(fixture:b2Fixture):Boolean. Return true to continue to the next fixture.

  • p: b2Vec2

    The query point.

public QueryShape(callback: (fixture: Box2D.Dynamics.b2Fixture) => boolean, shape: b2Shape, transform?: b2Transform)

Query the world for all fixtures that precisely overlap the provided transformed shape.

note

This provides a feature specific to this port.

Parameters

  • callback: (fixture: Box2D.Dynamics.b2Fixture) => boolean

    A user implemented callback class. It should match signature function Callback(fixture:b2Fixture):Boolean. Return true to continue to the next fixture.

  • shape: b2Shape

    The query shape.

  • transform?: b2Transform optional

    Optional transform, default = null.

public RayCast(callback: (fixture: Box2D.Dynamics.b2Fixture, point: Box2D.Common.Math.b2Vec2, normal: Box2D.Common.Math.b2Vec2, fraction: number) => number, point1: b2Vec2, point2: b2Vec2)

Ray-cast the world for all fixtures in the path of the ray. Your callback Controls whether you get the closest point, any point, or n-points The ray-cast ignores shapes that contain the starting point.

Parameters

  • callback: (fixture: Box2D.Dynamics.b2Fixture, point: Box2D.Common.Math.b2Vec2, normal: Box2D.Common.Math.b2Vec2, fraction: number) => number

    A callback function which must be of signature: function Callback( fixture:b2Fixture, // The fixture hit by the ray point:b2Vec2, // The point of initial intersection normal:b2Vec2, // The normal vector at the point of intersection fraction:Number // The fractional length along the ray of the intersection ):Number Callback should return the new length of the ray as a fraction of the original length. By returning 0, you immediately terminate. By returning 1, you continue wiht the original ray. By returning the current fraction, you proceed to find the closest point.

  • point1: b2Vec2

    The ray starting point.

  • point2: b2Vec2

    The ray ending point.

public RayCastAll(point1: b2Vec2, point2: b2Vec2): b2Fixture[]

Ray-cast the world for all fixture in the path of the ray.

Parameters

  • point1: b2Vec2

    The ray starting point.

  • point2: b2Vec2

    The ray ending point.

Returns

b2Fixture[]

Array of all the fixtures intersected by the ray.

public RayCastOne(point1: b2Vec2, point2: b2Vec2): b2Fixture

Ray-cast the world for the first fixture in the path of the ray.

Parameters

  • point1: b2Vec2

    The ray starting point.

  • point2: b2Vec2

    The ray ending point.

Returns

b2Fixture

First fixture intersected by the ray.

public RemoveController(c: b2Controller)

Removes the controller from the world.

Parameters

public SetBroadPhase(broadPhase: IBroadPhase)

Use the given object as a broadphase. The old broadphase will not be cleanly emptied.

warning

This function is locked during callbacks.

Parameters

public SetContactFilter(filter: b2ContactFilter)

Register a contact filter to provide specific control over collision. Otherwise the default filter is used (b2_defaultFilter).

Parameters

public SetContactListener(listener: b2ContactListener)

Register a contact event listener.

Parameters

public SetContinuousPhysics(flag: boolean)

Enable/disable continuous physics. For testing.

Parameters

  • flag: boolean

    True for continuous physics, otherwise false.

public SetDebugDraw(debugDraw: b2DebugDraw)

Register a routine for debug drawing. The debug draw functions are called inside the b2World::Step method, so make sure your renderer is ready to consume draw commands when you call Step().

Parameters

public SetDestructionListener(listener: b2DestructionListener)

Destruct the world. All physics entities are destroyed and all heap memory is released.

Parameters

public SetGravity(gravity: b2Vec2)

Change the global gravity vector.

Parameters

  • gravity: b2Vec2

    New global gravity vector.

public SetWarmStarting(flag: boolean)

Enable/disable warm starting. For testing.

Parameters

  • flag: boolean

    True for warm starting, otherwise false.

public Step(dt: number, velocityIterations: number, positionIterations: number)

Take a time step. This performs collision detection, integration, and constraint solution.

Parameters

  • dt: number

    The amout of time to simulate, this should not vary.

  • velocityIterations: number

    For the velocity constraint solver.

  • positionIterations: number

    For the position constraint solver.

public Validate()

Perform validation of internal data structures.