Defold Learn logo


Box2D b2Shape documentation

Constants for functional shape tables used with b2d.body.create_fixture and returned from b2d.fixture.get_shape.

Version: alpha

TYPES
b2d.shape.definition Box2D shape definition.
b2Shape Box2D shape
ENUMS
b2d.shape.SHAPE_TYPE Box2D shape types.
FUNCTIONS
b2d.shape.are_contact_events_enabled() Check if contact events are enabled for a shape.
b2d.shape.are_hit_events_enabled() Check if hit events are enabled for a shape.
b2d.shape.are_pre_solve_events_enabled() Check if pre-solve events are enabled for a shape.
b2d.shape.are_sensor_events_enabled() Check if sensor events are enabled for a shape.
b2d.shape.enable_contact_events() Enable or disable contact events for a shape.
b2d.shape.enable_hit_events() Enable or disable hit events for a shape.
b2d.shape.enable_pre_solve_events() Enable or disable pre-solve events for a shape.
b2d.shape.enable_sensor_events() Enable or disable sensor events for a shape.
b2d.shape.get_body() Get the body owning a shape.
b2d.shape.get_closest_point() Get the closest point on a shape.
b2d.shape.get_contact_capacity() Get shape contact capacity.
b2d.shape.get_contact_data() Get touching contact data for a shape.
b2d.shape.get_mass_data() Get mass data for a shape.
b2d.shape.get_material() Get shape material id.
b2d.shape.get_sensor_capacity() Get sensor overlap capacity.
b2d.shape.get_sensor_overlaps() Get sensor overlaps.
b2d.shape.get_shape() Get a shape's geometry.
b2d.shape.get_world() Get the world owning a shape.
b2d.shape.is_valid() Validate a shape handle.
b2d.shape.ray_cast() Ray cast a shape directly.
b2d.shape.set_material() Set shape material id.
b2d.shape.set_shape() Set a shape's geometry.

Types

b2d.shape.definition

b2d.shape.definition = { type:b2d.shape.SHAPE_TYPE, radius:number, center?:vector3 } | { type:b2d.shape.SHAPE_TYPE, radius:number, center1:vector3, center2:vector3 } | { type:b2d.shape.SHAPE_TYPE, v1:vector3, v2:vector3, v0?:vector3, v3?:vector3 } | { type:b2d.shape.SHAPE_TYPE, hx:number, hy:number, center?:vector3, angle?:number } | { type:b2d.shape.SHAPE_TYPE, vertices:vector3[], loop?:boolean, prev_vertex?:vector3, next_vertex?:vector3 }

A reusable table describing Box2D shape geometry. It is accepted by shape creation, query, cast, and update functions, and is returned by b2d.shape.get_shape or b2d.fixture.get_shape. Available fields depend on type:

  • Circle: radius and optional center.
  • Capsule: radius, center1, and center2.
  • Edge or segment: v1, v2, and optional ghost vertices v0 and v3.
  • Box: half-extents hx and hy, with optional center and angle in radians.
  • Polygon: vertices.
  • Chain: vertices, with optional loop and ghost-vertex fields.
The union covers both supported Box2D runtime versions; some shape types are only available with one version.

EXAMPLES

local circle = {
    type = b2d.shape.SHAPE_TYPE_CIRCLE,
    radius = 16,
    center = vmath.vector3(0, 8, 0),
}

local box = {
    type = b2d.shape.SHAPE_TYPE_BOX,
    hx = 32,
    hy = 8,
    angle = math.rad(15),
}

b2Shape

b2Shape = userdata

An opaque handle to one collision shape attached to a b2Body. Obtain shape handles from b2d.body.get_shapes or when creating shapes, then use the functions in b2d.shape to inspect or modify them. A shape is owned by its body and its handle becomes invalid when the shape or body is destroyed.

EXAMPLES

local body = b2d.get_body("#collisionobject")
local shapes = b2d.body.get_shapes(body)
local shape = shapes[1].shape_id
pprint(b2d.shape.get_shape(shape))

Enums

b2d.shape.SHAPE_TYPE

b2d.shape.SHAPE_TYPE: integer

Box2D shape types.

VALUES

b2d.shape.SHAPE_TYPE_BOX Box shape type alias. Uses the polygon enum value, but indicates the hx/hy box convenience format.
b2d.shape.SHAPE_TYPE_CAPSULE Capsule shape type.
b2d.shape.SHAPE_TYPE_CHAIN Chain shape type.
b2d.shape.SHAPE_TYPE_CIRCLE Circle shape type.
b2d.shape.SHAPE_TYPE_EDGE Edge shape type alias. Compatibility alias for b2d.shape.SHAPE_TYPE_SEGMENT.
b2d.shape.SHAPE_TYPE_GRID Grid shape type.
b2d.shape.SHAPE_TYPE_POLYGON Polygon shape type.
b2d.shape.SHAPE_TYPE_SEGMENT Segment shape type.

Functions

b2d.shape.are_contact_events_enabled()

b2d.shape.are_contact_events_enabled(shape_id:b2Shape)→enabled:boolean

Check if contact events are enabled for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

enabled boolean
true if contact events are enabled

b2d.shape.are_hit_events_enabled()

b2d.shape.are_hit_events_enabled(shape_id:b2Shape)→enabled:boolean

Check if hit events are enabled for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

enabled boolean
true if hit events are enabled

b2d.shape.are_pre_solve_events_enabled()

b2d.shape.are_pre_solve_events_enabled(shape_id:b2Shape)→enabled:boolean

Check if pre-solve events are enabled for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

enabled boolean
true if pre-solve events are enabled

b2d.shape.are_sensor_events_enabled()

b2d.shape.are_sensor_events_enabled(shape_id:b2Shape)→enabled:boolean

Check if sensor events are enabled for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

enabled boolean
true if sensor events are enabled

b2d.shape.enable_contact_events()

b2d.shape.enable_contact_events(shape_id:b2Shape, enable:boolean)

Enable or disable contact events for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
enable boolean
true to enable contact events

b2d.shape.enable_hit_events()

b2d.shape.enable_hit_events(shape_id:b2Shape, enable:boolean)

Enable or disable hit events for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
enable boolean
true to enable hit events

b2d.shape.enable_pre_solve_events()

b2d.shape.enable_pre_solve_events(shape_id:b2Shape, enable:boolean)

Enable or disable pre-solve events for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
enable boolean
true to enable pre-solve events

b2d.shape.enable_sensor_events()

b2d.shape.enable_sensor_events(shape_id:b2Shape, enable:boolean)

Enable or disable sensor events for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
enable boolean
true to enable sensor events

b2d.shape.get_body()

b2d.shape.get_body(shape_id:b2Shape)→body:b2Body

Get the body owning a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

body b2Body
owning body

b2d.shape.get_closest_point()

b2d.shape.get_closest_point(shape_id:b2Shape, target:vector3)→point:vector3

Get the closest point on a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
target vector3
world target point

RETURNS

point vector3
closest world point on the shape

b2d.shape.get_contact_capacity()

b2d.shape.get_contact_capacity(shape_id:b2Shape)→capacity:integer

Get shape contact capacity.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

capacity integer
maximum contact data count

b2d.shape.get_contact_data()

b2d.shape.get_contact_data(shape_id:b2Shape)→contacts:b2d.contact_data[]

Get touching contact data for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

contacts b2d.contact_data[]
touching contacts

b2d.shape.get_mass_data()

b2d.shape.get_mass_data(shape_id:b2Shape)→data:b2d.mass_data

Get mass data for a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

data b2d.mass_data
shape mass data

b2d.shape.get_material()

b2d.shape.get_material(shape_id:b2Shape)→material:integer

Get shape material id.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

material integer
shape material id

b2d.shape.get_sensor_capacity()

b2d.shape.get_sensor_capacity(shape_id:b2Shape)→capacity:integer

Get sensor overlap capacity.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

capacity integer
maximum sensor overlap count

b2d.shape.get_sensor_overlaps()

b2d.shape.get_sensor_overlaps(shape_id:b2Shape)→overlaps:b2d.shape_info[]

Get sensor overlaps.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

overlaps b2d.shape_info[]
overlapping shapes

b2d.shape.get_shape()

b2d.shape.get_shape(shape_id:b2Shape)→shape:b2d.shape.definition

Get a shape's geometry.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

shape b2d.shape.definition
shape table with numeric type from b2d.shape.SHAPE_TYPE_*

b2d.shape.get_world()

b2d.shape.get_world(shape_id:b2Shape)→world:b2World

Get the world owning a shape.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

world b2World
owning world

b2d.shape.is_valid()

b2d.shape.is_valid(shape_id:b2Shape)→valid:boolean

Validate a shape handle.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index

RETURNS

valid boolean
true if the shape handle still refers to a live Box2D shape

b2d.shape.ray_cast()

b2d.shape.ray_cast(shape_id:b2Shape, origin:vector3, translation:vector3, [max_fraction:number])→hit:b2d.shape_cast_output|nil

Ray cast a shape directly.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
origin vector3
world ray origin
translation vector3
world ray translation
[max_fraction] number
optional maximum translation fraction, defaults to 1

RETURNS

hit b2d.shape_cast_output
nil
cast result, or nil

b2d.shape.set_material()

b2d.shape.set_material(shape_id:b2Shape, material:integer)

Set shape material id.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
material integer
shape material id

b2d.shape.set_shape()

b2d.shape.set_shape(shape_id:b2Shape, definition:b2d.shape.definition, update_mass:boolean)

This updates the shape geometry using the same table format as b2d.body.create_shape and b2d.shape.get_shape. The body mass is not updated unless update_mass is true.

PARAMETERS

shape_id b2Shape
shape handle from a shape info table, or pass body, shape_index
definition b2d.shape.definition
shape table with numeric type from b2d.shape.SHAPE_TYPE_*
update_mass boolean
true to reset body mass from shapes

EXAMPLES

local body = b2d.get_body("#collisionobject")

-- Move a circle shape relative to the body origin.
local circle = b2d.shape.get_shape(body, 1)
circle.center = vmath.vector3(24, 0, 0)
b2d.shape.set_shape(body, 1, circle, true)

-- Replace a segment shape's local endpoints.
b2d.shape.set_shape(body, 2, {
    type = b2d.shape.SHAPE_TYPE_SEGMENT,
    v1 = vmath.vector3(-32, 0, 0),
    v2 = vmath.vector3( 32, 0, 0),
})

-- Update a box shape using the polygon box convenience format.
b2d.shape.set_shape(body, 3, {
    type = b2d.shape.SHAPE_TYPE_BOX,
    hx = 16,
    hy = 8,
    center = vmath.vector3(0, 20, 0),
    angle = math.rad(30),
}, true)