Skip to Content
defold-typescript
Get startedGuidesAPILibraries
Defold

b2d.body

Functions for interacting with Box2D bodies.

Functions

b2d.body.apply_angular_impulse(body: Opaque<"b2Body">, impulse: number): void

Apply an angular impulse.

Parameters

  • body: Opaque<"b2Body"> — body
  • impulse: number — impulse the angular impulse in units of kgmm/s

b2d.body.apply_force(body: Opaque<"b2Body">, force: Vector3, point: Vector3): void

Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • force: Vector3 — the world force vector, usually in Newtons (N).
  • point: Vector3 — the world position of the point of application.

b2d.body.apply_force_to_center(body: Opaque<"b2Body">, force: Vector3): void

Apply a force to the center of mass. This wakes up the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • force: Vector3 — the world force vector, usually in Newtons (N).

b2d.body.apply_linear_impulse(body: Opaque<"b2Body">, impulse: Vector3, point: Vector3): void

Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • impulse: Vector3 — the world impulse vector, usually in N-seconds or kg-m/s.
  • point: Vector3 — the world position of the point of application.

b2d.body.apply_torque(body: Opaque<"b2Body">, torque: number): void

Apply a torque. This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • torque: number — torque about the z-axis (out of the screen), usually in N-m.

b2d.body.enable_sleep(body: Opaque<"b2Body">, enable: boolean): void

You can disable sleeping on this body. If you disable sleeping, the body will be woken.

Parameters

  • body: Opaque<"b2Body"> — body
  • enable: boolean — if false, the body will never sleep, and consume more CPU

b2d.body.get_angle(body: Opaque<"b2Body">): number

Get the angle in radians.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • angle: number — the current world rotation angle in radians.

b2d.body.get_angular_damping(body: Opaque<"b2Body">): number

Get the angular damping of the body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • damping: number — the damping

b2d.body.get_angular_velocity(body: Opaque<"b2Body">): number

Get the angular velocity.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • velocity: number — the angular velocity in radians/second.

b2d.body.get_gravity_scale(body: Opaque<"b2Body">): number

Get the gravity scale of the body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • scale: number — the scale

b2d.body.get_linear_damping(body: Opaque<"b2Body">): number

Get the linear damping of the body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • damping: number — the damping

b2d.body.get_linear_velocity(body: Opaque<"b2Body">): Vector3

Get the linear velocity of the center of mass.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • velocity: Vector3 — the linear velocity of the center of mass.

b2d.body.get_linear_velocity_from_local_point(body: Opaque<"b2Body">, local_point: Vector3): Vector3

Get the world velocity of a local point.

Parameters

  • body: Opaque<"b2Body"> — body
  • local_point: Vector3 — a point in local coordinates.

Returns

  • velocity: Vector3 — the world velocity of a point.

b2d.body.get_linear_velocity_from_world_point(body: Opaque<"b2Body">, world_point: Vector3): Vector3

Get the world linear velocity of a world point attached to this body.

Parameters

  • body: Opaque<"b2Body"> — body
  • world_point: Vector3 — a point in world coordinates.

Returns

  • velocity: Vector3 — the world velocity of a point.

b2d.body.get_local_center_of_mass(body: Opaque<"b2Body">): Vector3

Get the local position of the center of mass.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • center: Vector3 — Get the local position of the center of mass.

b2d.body.get_local_point(body: Opaque<"b2Body">, world_point: Vector3): Vector3

Gets a local point relative to the body's origin given a world point.

Parameters

  • body: Opaque<"b2Body"> — body
  • world_point: Vector3 — a point in world coordinates.

Returns

  • vector: Vector3 — the corresponding local point relative to the body's origin.

b2d.body.get_local_vector(body: Opaque<"b2Body">, world_vector: Vector3): Vector3

Gets a local vector given a world vector.

Parameters

  • body: Opaque<"b2Body"> — body
  • world_vector: Vector3 — a vector in world coordinates.

Returns

  • vector: Vector3 — the corresponding local vector.

b2d.body.get_mass(body: Opaque<"b2Body">): number

Get the total mass of the body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • mass: number — the mass, usually in kilograms (kg).

b2d.body.get_position(body: Opaque<"b2Body">): Vector3

Get the world body origin position.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • position: Vector3 — the world position of the body's origin.

b2d.body.get_rotational_inertia(body: Opaque<"b2Body">): number

Get the rotational inertia of the body about the local origin.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • inertia: number — the rotational inertia, usually in kg-m^2.

b2d.body.get_type(body: Opaque<"b2Body">): (number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_STATIC_BODY" })

Get the type of this body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • type: (number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_STATIC_BODY" }) — the body type

b2d.body.get_world(body: Opaque<"b2Body">): Opaque<"b2World">

Get the parent world of this body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • world: Opaque<"b2World">

b2d.body.get_world_center_of_mass(body: Opaque<"b2Body">): Vector3

Get the world position of the center of mass.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • center: Vector3 — Get the world position of the center of mass.

b2d.body.get_world_point(body: Opaque<"b2Body">, local_vector: Vector3): Vector3

Get the world coordinates of a point given the local coordinates.

Parameters

  • body: Opaque<"b2Body"> — body
  • local_vector: Vector3 — localPoint a point on the body measured relative the the body's origin.

Returns

  • vector: Vector3 — the same point expressed in world coordinates.

b2d.body.get_world_vector(body: Opaque<"b2Body">, local_vector: Vector3): Vector3

Get the world coordinates of a vector given the local coordinates.

Parameters

  • body: Opaque<"b2Body"> — body
  • local_vector: Vector3 — a vector fixed in the body.

Returns

  • vector: Vector3 — the same vector expressed in world coordinates.

b2d.body.is_active(body: Opaque<"b2Body">): boolean

Get the active state of the body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • enabled: boolean — is the body active

b2d.body.is_awake(body: Opaque<"b2Body">): boolean

Get the sleeping state of this body.

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • enabled: boolean — true if the body is awake, false if it's sleeping.

b2d.body.is_bullet(body: Opaque<"b2Body">): boolean

Is this body in bullet mode

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • enabled: boolean — true if the body is in bullet mode

b2d.body.is_fixed_rotation(body: Opaque<"b2Body">): boolean

Does this body have fixed rotation?

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • enabled: boolean — is the rotation fixed

b2d.body.is_sleeping_enabled(body: Opaque<"b2Body">): boolean

Is this body allowed to sleep

Parameters

  • body: Opaque<"b2Body"> — body

Returns

  • enabled: boolean — true if the body is allowed to sleep

b2d.body.reset_mass_data(body: Opaque<"b2Body">): void

This resets the mass properties to the sum of the mass properties of the fixtures. This normally does not need to be called unless you called SetMassData to override

Parameters

  • body: Opaque<"b2Body"> — body

b2d.body.set_active(body: Opaque<"b2Body">, enable: boolean): void

Set the active state of the body. An inactive body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected. You may continue to create/destroy fixtures and joints on inactive bodies. Fixtures on an inactive body are implicitly inactive and will not participate in collisions, ray-casts, or queries. Joints connected to an inactive body are implicitly inactive. An inactive body is still owned by a b2World object and remains in the body list.

Parameters

  • body: Opaque<"b2Body"> — body
  • enable: boolean — true if the body should be active

b2d.body.set_angular_damping(body: Opaque<"b2Body">, damping: number): void

Set the angular damping of the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • damping: number — the damping

b2d.body.set_angular_velocity(body: Opaque<"b2Body">, omega: number): void

Set the angular velocity.

Parameters

  • body: Opaque<"b2Body"> — body
  • omega: number — the new angular velocity in radians/second.

b2d.body.set_awake(body: Opaque<"b2Body">, enable: boolean): void

Set the sleep state of the body. A sleeping body has very low CPU cost.

Parameters

  • body: Opaque<"b2Body"> — body
  • enable: boolean — flag set to false to put body to sleep, true to wake it.

b2d.body.set_bullet(body: Opaque<"b2Body">, enable: boolean): void

Should this body be treated like a bullet for continuous collision detection?

Parameters

  • body: Opaque<"b2Body"> — body
  • enable: boolean — if true, the body will be in bullet mode

b2d.body.set_fixed_rotation(body: Opaque<"b2Body">, enable: boolean): void

Set this body to have fixed rotation. This causes the mass to be reset.

Parameters

  • body: Opaque<"b2Body"> — body
  • enable: boolean — true if the rotation should be fixed

b2d.body.set_gravity_scale(body: Opaque<"b2Body">, scale: number): void

Set the gravity scale of the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • scale: number — the scale

b2d.body.set_linear_damping(body: Opaque<"b2Body">, damping: number): void

Set the linear damping of the body.

Parameters

  • body: Opaque<"b2Body"> — body
  • damping: number — the damping

b2d.body.set_linear_velocity(body: Opaque<"b2Body">, velocity: Vector3): void

Set the linear velocity of the center of mass.

Parameters

  • body: Opaque<"b2Body"> — body
  • velocity: Vector3 — the new linear velocity of the center of mass.

b2d.body.set_transform(body: Opaque<"b2Body">, position: Vector3, angle: number): void

Set the position of the body's origin and rotation. This breaks any contacts and wakes the other bodies. Manipulating a body's transform may cause non-physical behavior.

Parameters

  • body: Opaque<"b2Body"> — body
  • position: Vector3 — the world position of the body's local origin.
  • angle: number — the world position of the body's local origin.

b2d.body.set_type(body: Opaque<"b2Body">, type: (number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_STATIC_BODY" })): void

Set the type of this body. This may alter the mass and velocity.

Parameters

  • body: Opaque<"b2Body"> — body
  • type: (number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_STATIC_BODY" }) — the body type

Constants

b2d.body.B2_DYNAMIC_BODY: number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }

Dynamic body

b2d.body.B2_KINEMATIC_BODY: number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }

Kinematic body

b2d.body.B2_STATIC_BODY: number & { readonly __brand: "b2d.body.B2_STATIC_BODY" }

Static (immovable) body

Types

type b2d.body.B2Body = Opaque<"b2Body">

type b2d.body.B2World = Opaque<"b2World">

On this page

Functionsb2d.body.apply_angular_impulse(body: Opaque<"b2Body">, impulse: number): voidb2d.body.apply_force(body: Opaque<"b2Body">, force: Vector3, point: Vector3): voidb2d.body.apply_force_to_center(body: Opaque<"b2Body">, force: Vector3): voidb2d.body.apply_linear_impulse(body: Opaque<"b2Body">, impulse: Vector3, point: Vector3): voidb2d.body.apply_torque(body: Opaque<"b2Body">, torque: number): voidb2d.body.enable_sleep(body: Opaque<"b2Body">, enable: boolean): voidb2d.body.get_angle(body: Opaque<"b2Body">): numberb2d.body.get_angular_damping(body: Opaque<"b2Body">): numberb2d.body.get_angular_velocity(body: Opaque<"b2Body">): numberb2d.body.get_gravity_scale(body: Opaque<"b2Body">): numberb2d.body.get_linear_damping(body: Opaque<"b2Body">): numberb2d.body.get_linear_velocity(body: Opaque<"b2Body">): Vector3b2d.body.get_linear_velocity_from_local_point(body: Opaque<"b2Body">, local_point: Vector3): Vector3b2d.body.get_linear_velocity_from_world_point(body: Opaque<"b2Body">, world_point: Vector3): Vector3b2d.body.get_local_center_of_mass(body: Opaque<"b2Body">): Vector3b2d.body.get_local_point(body: Opaque<"b2Body">, world_point: Vector3): Vector3b2d.body.get_local_vector(body: Opaque<"b2Body">, world_vector: Vector3): Vector3b2d.body.get_mass(body: Opaque<"b2Body">): numberb2d.body.get_position(body: Opaque<"b2Body">): Vector3b2d.body.get_rotational_inertia(body: Opaque<"b2Body">): numberb2d.body.get_type(body: Opaque<"b2Body">): (number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_STATIC_BODY" })b2d.body.get_world(body: Opaque<"b2Body">): Opaque<"b2World">b2d.body.get_world_center_of_mass(body: Opaque<"b2Body">): Vector3b2d.body.get_world_point(body: Opaque<"b2Body">, local_vector: Vector3): Vector3b2d.body.get_world_vector(body: Opaque<"b2Body">, local_vector: Vector3): Vector3b2d.body.is_active(body: Opaque<"b2Body">): booleanb2d.body.is_awake(body: Opaque<"b2Body">): booleanb2d.body.is_bullet(body: Opaque<"b2Body">): booleanb2d.body.is_fixed_rotation(body: Opaque<"b2Body">): booleanb2d.body.is_sleeping_enabled(body: Opaque<"b2Body">): booleanb2d.body.reset_mass_data(body: Opaque<"b2Body">): voidb2d.body.set_active(body: Opaque<"b2Body">, enable: boolean): voidb2d.body.set_angular_damping(body: Opaque<"b2Body">, damping: number): voidb2d.body.set_angular_velocity(body: Opaque<"b2Body">, omega: number): voidb2d.body.set_awake(body: Opaque<"b2Body">, enable: boolean): voidb2d.body.set_bullet(body: Opaque<"b2Body">, enable: boolean): voidb2d.body.set_fixed_rotation(body: Opaque<"b2Body">, enable: boolean): voidb2d.body.set_gravity_scale(body: Opaque<"b2Body">, scale: number): voidb2d.body.set_linear_damping(body: Opaque<"b2Body">, damping: number): voidb2d.body.set_linear_velocity(body: Opaque<"b2Body">, velocity: Vector3): voidb2d.body.set_transform(body: Opaque<"b2Body">, position: Vector3, angle: number): voidb2d.body.set_type(body: Opaque<"b2Body">, type: (number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }) | (number & { readonly __brand: "b2d.body.B2_STATIC_BODY" })): voidConstantsb2d.body.B2_DYNAMIC_BODY: number & { readonly __brand: "b2d.body.B2_DYNAMIC_BODY" }b2d.body.B2_KINEMATIC_BODY: number & { readonly __brand: "b2d.body.B2_KINEMATIC_BODY" }b2d.body.B2_STATIC_BODY: number & { readonly __brand: "b2d.body.B2_STATIC_BODY" }Typestype b2d.body.B2Body = Opaque<"b2Body">type b2d.body.B2World = Opaque<"b2World">