physics
Collision object physics API documentation
Functions
physics.create_joint(joint_type: number, collisionobject_a: string | Hash | Url, joint_id: string | Hash, position_a: Vector3, collisionobject_b: string | Hash | Url, position_b: Vector3, properties?: { collide_connected?: boolean }): voidphysics.destroy_joint(collisionobject: string | Hash | Url, joint_id: string | Hash): voidphysics.get_gravity(): Vector3physics.get_group(url: string | Hash | Url): Hashphysics.get_joint_properties(collisionobject: string | Hash | Url, joint_id: string | Hash): { collide_connected: boolean }physics.get_joint_reaction_force(collisionobject: string | Hash | Url, joint_id: string | Hash): Vector3physics.get_joint_reaction_torque(collisionobject: string | Hash | Url, joint_id: string | Hash): numberphysics.get_maskbit(url: string | Hash | Url, group: string | Hash): booleanphysics.get_shape(url: string | Hash | Url, shape: string | Hash): { type: number; diameter?: number; dimensions?: Vector3; height?: number }physics.raycast(from: Vector3, to: Vector3, groups: Hash[], options?: { all?: boolean }): Omit<BuiltinMessages["ray_cast_response"], "request_id">[] | Omit<BuiltinMessages["ray_cast_response"], "request_id"> | undefinedphysics.raycast_async(2 overloads): voidphysics.set_event_listener(callback?: (self: unknown, events: unknown) => void): voidphysics.set_gravity(gravity: Vector3): voidphysics.set_group(url: string | Hash | Url, group: string | Hash): voidphysics.set_hflip(url: string | Hash | Url, flip: boolean): voidphysics.set_joint_properties(collisionobject: string | Hash | Url, joint_id: string | Hash, properties: { collide_connected?: boolean }): voidphysics.set_maskbit(url: string | Hash | Url, group: string | Hash, maskbit: boolean): voidphysics.set_shape(url: string | Hash | Url, shape: string | Hash, table: { type: typeof physics.SHAPE_TYPE_SPHERE; diameter: number } | { type: typeof physics.SHAPE_TYPE_BOX; dimensions: Vector3 } | { type: typeof physics.SHAPE_TYPE_CAPSULE; diameter: number; height: number }): voidphysics.set_vflip(url: string | Hash | Url, flip: boolean): voidphysics.update_mass(collisionobject: string | Hash | Url, mass: number): voidphysics.wakeup(url: string | Hash | Url): void
physics.create_joint(joint_type: number, collisionobject_a: string | Hash | Url, joint_id: string | Hash, position_a: Vector3, collisionobject_b: string | Hash | Url, position_b: Vector3, properties?: { collide_connected?: boolean }): void
Create a physics joint between two collision object components. Note: Currently only supported in 2D physics.
Parameters
joint_type:number— the joint typecollisionobject_a:string | Hash | Url— first collision objectjoint_id:string | Hash— id of the jointposition_a:Vector3— local position where to attach the joint on the first collision objectcollisionobject_b:string | Hash | Url— second collision objectposition_b:Vector3— local position where to attach the joint on the second collision objectproperties?:{ collide_connected?: boolean }— optional joint-specific properties
physics.destroy_joint(collisionobject: string | Hash | Url, joint_id: string | Hash): void
Destroy an already physics joint. The joint has to be created before a destroy can be issued. Note: Currently only supported in 2D physics.
Parameters
collisionobject:string | Hash | Url— collision object where the joint existjoint_id:string | Hash— id of the joint
physics.get_gravity(): Vector3
Get the gravity in runtime. The gravity returned is not global, it will return the gravity for the collection that the function is called from. Note: For 2D physics the z component will always be zero.
export default defineScript({
init(self) {
let gravity = physics.get_gravity();
// Inverse gravity!
gravity = vmath.vector3(-gravity.x, -gravity.y, -gravity.z);
physics.set_gravity(gravity);
},
});function init(self)
local gravity = physics.get_gravity()
-- Inverse gravity!
gravity = -gravity
physics.set_gravity(gravity)
endReturns
gravity:Vector3— gravity vector of collection
physics.get_group(url: string | Hash | Url): Hash
Returns the group name of a collision object as a hash.
function check_is_enemy(): boolean {
const group = physics.get_group("#collisionobject");
return group === hash("enemy");
}local function check_is_enemy()
local group = physics.get_group("#collisionobject")
return group == hash("enemy")
endParameters
url:string | Hash | Url— the collision object to return the group of.
Returns
group:Hash— hash value of the group.
physics.get_joint_properties(collisionobject: string | Hash | Url, joint_id: string | Hash): { collide_connected: boolean }
Get a table for properties for a connected joint. The joint has to be created before properties can be retrieved. Note: Currently only supported in 2D physics.
Parameters
collisionobject:string | Hash | Url— collision object where the joint existjoint_id:string | Hash— id of the joint
Returns
properties:{ collide_connected: boolean }— joint properties
physics.get_joint_reaction_force(collisionobject: string | Hash | Url, joint_id: string | Hash): Vector3
Get the reaction force for a joint. The joint has to be created before the reaction force can be calculated. Note: Currently only supported in 2D physics.
Parameters
collisionobject:string | Hash | Url— collision object where the joint existjoint_id:string | Hash— id of the joint
Returns
force:Vector3— reaction force for the joint
physics.get_joint_reaction_torque(collisionobject: string | Hash | Url, joint_id: string | Hash): number
Get the reaction torque for a joint. The joint has to be created before the reaction torque can be calculated. Note: Currently only supported in 2D physics.
Parameters
collisionobject:string | Hash | Url— collision object where the joint existjoint_id:string | Hash— id of the joint
Returns
torque:number— the reaction torque on bodyB in N*m.
physics.get_maskbit(url: string | Hash | Url, group: string | Hash): boolean
Returns true if the specified group is set in the mask of a collision object, false otherwise.
function is_invincible(): boolean {
// check if the collisionobject would collide with the "bullet" group
const invincible = physics.get_maskbit("#collisionobject", "bullet");
return invincible;
}local function is_invincible()
-- check if the collisionobject would collide with the "bullet" group
local invincible = physics.get_maskbit("#collisionobject", "bullet")
return invincible
endParameters
url:string | Hash | Url— the collision object to check the mask of.group:string | Hash— the name of the group to check for.
Returns
maskbit:boolean— boolean value of the maskbit. 'true' if present, 'false' otherwise.
physics.get_shape(url: string | Hash | Url, shape: string | Hash): { type: number; diameter?: number; dimensions?: Vector3; height?: number }
Gets collision shape data from a collision object. Dimensions are in game units,
independent of the physics.scale setting.
function get_shape_meta() {
const sphere = physics.get_shape("#collisionobject", "my_sphere_shape");
// returns a table with sphere.diameter
return sphere;
}local function get_shape_meta()
local sphere = physics.get_shape("#collisionobject", "my_sphere_shape")
-- returns a table with sphere.diameter
return sphere
endParameters
url:string | Hash | Url— the collision object.shape:string | Hash— the name of the shape to get data for.
Returns
table:{ type: number; diameter?: number; dimensions?: Vector3; height?: number }— collision shape data
physics.raycast(from: Vector3, to: Vector3, groups: Hash[], options?: { all?: boolean }): Omit<BuiltinMessages["ray_cast_response"], "request_id">[] | Omit<BuiltinMessages["ray_cast_response"], "request_id"> | undefined
Ray casts are used to test for intersections against collision objects in the physics world.
Collision objects of types kinematic, dynamic and static are tested against. Trigger objects
do not intersect with ray casts.
Which collision objects to hit is filtered by their collision groups and can be configured
through groups.
NOTE: Ray casts will ignore collision objects that contain the starting point of the ray. This is a limitation in Box2D.
function handle_result(hit: { fraction: number; position: Vector3; normal: Vector3; id: Hash; group: Hash }) {
// act on the hit (see 'ray_cast_response')
}
// How to perform a ray cast synchronously:
export default defineScript({
init() {
return { groups: [hash("world"), hash("enemy")] };
},
update(self, dt) {
// request ray cast
const from = go.get_world_position();
const to = vmath.vector3(from.x, from.y - 100, from.z);
const results = physics.raycast(from, to, self.groups, { all: true });
if (results !== undefined) {
// with `all` set, the ray cast returns every hit as a list
for (const result of Array.isArray(results) ? results : [results]) {
handle_result(result);
}
}
},
});function init(self)
self.groups = {hash("world"), hash("enemy")}
end
function update(self, dt)
-- request ray cast
local result = physics.raycast(from, to, self.groups, {all=true})
if result ~= nil then
-- act on the hit (see 'ray_cast_response')
for _,result in ipairs(results) do
handle_result(result)
end
end
endParameters
from:Vector3— the world position of the start of the rayto:Vector3— the world position of the end of the raygroups:Hash[]— a lua table containing the hashed groups for which to test collisions againstoptions?:{ all?: boolean }— optional ray-cast options
Returns
result:Omit<BuiltinMessages["ray_cast_response"], "request_id">[] | Omit<BuiltinMessages["ray_cast_response"], "request_id"> | undefined— It returns a list. If missed it returnsnil. See ray_cast_response for details on the returned values.
physics.raycast_async(2 overloads): void
Ray casts are used to test for intersections against collision objects in the physics world.
Collision objects of types kinematic, dynamic and static are tested against. Trigger objects
do not intersect with ray casts.
Which collision objects to hit is filtered by their collision groups and can be configured
through groups.
The actual ray cast will be performed during the physics-update.
- If an object is hit, the result will be reported via a ray_cast_response message.
- If there is no object hit, the result will be reported via a ray_cast_missed message.
NOTE: Ray casts will ignore collision objects that contain the starting point of the ray. This is a limitation in Box2D.
(from: Vector3, to: Vector3, groups: Hash[])(from: Vector3, to: Vector3, groups: Hash[], request_id: number)Parameters
from:Vector3— the world position of the start of the rayto:Vector3— the world position of the end of the raygroups:Hash[]— a lua table containing the hashed groups for which to test collisions againstrequest_id:number— a number in range [0,255]. It will be sent back in the response for identification, 0 by default
// How to perform a ray cast asynchronously:
export default defineScript({
init() {
return { my_groups: [hash("my_group1"), hash("my_group2")] };
},
update(self, dt) {
// request ray cast
const my_start = go.get_world_position();
const my_end = vmath.vector3(my_start.x, my_start.y - 100, my_start.z);
physics.raycast_async(my_start, my_end, self.my_groups);
},
on_message(self, message_id, message, sender) {
// check for the response
if (message_id === hash("ray_cast_response")) {
// act on the hit
} else if (message_id === hash("ray_cast_missed")) {
// act on the miss
}
},
});function init(self)
self.my_groups = {hash("my_group1"), hash("my_group2")}
end
function update(self, dt)
-- request ray cast
physics.raycast_async(my_start, my_end, self.my_groups)
end
function on_message(self, message_id, message, sender)
-- check for the response
if message_id == hash("ray_cast_response") then
-- act on the hit
elseif message_id == hash("ray_cast_missed") then
-- act on the miss
end
endphysics.set_event_listener(callback?: (self: unknown, events: unknown) => void): void
Only one physics world event listener can be set at a time.
function physics_world_listener(self: unknown, events: unknown) {
for (const event of events as Record<string, unknown>[]) {
const event_type = event["type"];
if (event_type === hash("contact_point_event")) {
pprint(event);
// {
// distance = 2.1490633487701,
// applied_impulse = 0
// a = { --[[0x113f7c6c0]]
// group = hash: [box],
// id = hash: [/box]
// mass = 0,
// normal = vmath.vector3(0.379, 0.925, -0),
// position = vmath.vector3(517.337, 235.068, 0),
// instance_position = vmath.vector3(480, 144, 0),
// relative_velocity = vmath.vector3(-0, -0, -0),
// },
// b = { --[[0x113f7c840]]
// group = hash: [circle],
// id = hash: [/circle]
// mass = 0,
// normal = vmath.vector3(-0.379, -0.925, 0),
// position = vmath.vector3(517.337, 235.068, 0),
// instance_position = vmath.vector3(-0.0021, 0, -0.0022),
// relative_velocity = vmath.vector3(0, 0, 0),
// },
// }
} else if (event_type === hash("collision_event")) {
pprint(event);
// {
// a = {
// group = hash: [default],
// position = vmath.vector3(183, 666, 0),
// id = hash: [/go1]
// },
// b = {
// group = hash: [default],
// position = vmath.vector3(185, 704.05865478516, 0),
// id = hash: [/go2]
// }
// }
} else if (event_type === hash("trigger_event")) {
pprint(event);
// {
// enter = true,
// b = {
// group = hash: [default],
// id = hash: [/go2]
// },
// a = {
// group = hash: [default],
// id = hash: [/go1]
// }
// },
} else if (event_type === hash("ray_cast_response")) {
pprint(event);
// {
// group = hash: [default],
// request_id = 0,
// position = vmath.vector3(249.92222595215, 249.92222595215, 0),
// fraction = 0.68759721517563,
// normal = vmath.vector3(0, 1, 0),
// id = hash: [/go]
// }
} else if (event_type === hash("ray_cast_missed")) {
pprint(event);
// {
// request_id = 0
// },
}
}
}
export default defineScript({
init(self) {
physics.set_event_listener(physics_world_listener);
},
});local function physics_world_listener(self, events)
for _,event in ipairs(events) do
local event_type = event["type"]
if event_type == hash("contact_point_event") then
pprint(event)
-- {
-- distance = 2.1490633487701,
-- applied_impulse = 0
-- a = { --[[0x113f7c6c0]]
-- group = hash: [box],
-- id = hash: [/box]
-- mass = 0,
-- normal = vmath.vector3(0.379, 0.925, -0),
-- position = vmath.vector3(517.337, 235.068, 0),
-- instance_position = vmath.vector3(480, 144, 0),
-- relative_velocity = vmath.vector3(-0, -0, -0),
-- },
-- b = { --[[0x113f7c840]]
-- group = hash: [circle],
-- id = hash: [/circle]
-- mass = 0,
-- normal = vmath.vector3(-0.379, -0.925, 0),
-- position = vmath.vector3(517.337, 235.068, 0),
-- instance_position = vmath.vector3(-0.0021, 0, -0.0022),
-- relative_velocity = vmath.vector3(0, 0, 0),
-- },
-- }
elseif event_type == hash("collision_event") then
pprint(event)
-- {
-- a = {
-- group = hash: [default],
-- position = vmath.vector3(183, 666, 0),
-- id = hash: [/go1]
-- },
-- b = {
-- group = hash: [default],
-- position = vmath.vector3(185, 704.05865478516, 0),
-- id = hash: [/go2]
-- }
-- }
elseif event_type == hash("trigger_event") then
pprint(event)
-- {
-- enter = true,
-- b = {
-- group = hash: [default],
-- id = hash: [/go2]
-- },
-- a = {
-- group = hash: [default],
-- id = hash: [/go1]
-- }
-- },
elseif event_type == hash("ray_cast_response") then
pprint(event)
--{
-- group = hash: [default],
-- request_id = 0,
-- position = vmath.vector3(249.92222595215, 249.92222595215, 0),
-- fraction = 0.68759721517563,
-- normal = vmath.vector3(0, 1, 0),
-- id = hash: [/go]
-- }
elseif event_type == hash("ray_cast_missed") then
pprint(event)
-- {
-- request_id = 0
--},
end
end
function init(self)
physics.set_event_listener(physics_world_listener)
endParameters
callback?:(self: unknown, events: unknown) => void— A callback that receives information about all physics interactions in this physics world. Passnilto remove the listener.selfscript_instance The calling script instanceeventsphysics.event[] An array of event tables. Each event table contains atypefield with the hashed name of one of these messages, together with fields specific to that event type:- contact_point_event
- collision_event
- trigger_event
- ray_cast_response
- ray_cast_missed
physics.set_gravity(gravity: Vector3): void
Set the gravity in runtime. The gravity change is not global, it will only affect the collection that the function is called from. Note: For 2D physics the z component of the gravity vector will be ignored.
export default defineScript({
init(self) {
// Set "upside down" gravity for this collection.
physics.set_gravity(vmath.vector3(0, 10.0, 0));
},
});function init(self)
-- Set "upside down" gravity for this collection.
physics.set_gravity(vmath.vector3(0, 10.0, 0))
endParameters
gravity:Vector3— the new gravity vector
physics.set_group(url: string | Hash | Url, group: string | Hash): void
Updates the group property of a collision object to the specified string value. The group name should exist i.e. have been used in a collision object in the editor.
function change_collision_group() {
physics.set_group("#collisionobject", "enemy");
}local function change_collision_group()
physics.set_group("#collisionobject", "enemy")
endParameters
url:string | Hash | Url— the collision object affected.group:string | Hash— the new group name to be assigned.
physics.set_hflip(url: string | Hash | Url, flip: boolean): void
Flips the collision shapes horizontally for a collision object
export default defineScript({
init() {
const fliph = true; // set on some condition
physics.set_hflip("#collisionobject", fliph);
return { fliph };
},
});function init(self)
self.fliph = true -- set on some condition
physics.set_hflip("#collisionobject", self.fliph)
endParameters
url:string | Hash | Url— the collision object that should flip its shapesflip:boolean—trueif the collision object should flip its shapes,falseif not
physics.set_joint_properties(collisionobject: string | Hash | Url, joint_id: string | Hash, properties: { collide_connected?: boolean }): void
Updates the properties for an already connected joint. The joint has to be created before properties can be changed. Note: Currently only supported in 2D physics.
Parameters
collisionobject:string | Hash | Url— collision object where the joint existjoint_id:string | Hash— id of the jointproperties:{ collide_connected?: boolean }— joint specific properties table Note: Thecollide_connectedfield cannot be updated/changed after a connection has been made.
physics.set_maskbit(url: string | Hash | Url, group: string | Hash, maskbit: boolean): void
Sets or clears the masking of a group (maskbit) in a collision object.
function make_invincible() {
// no longer collide with the "bullet" group
physics.set_maskbit("#collisionobject", "bullet", false);
}local function make_invincible()
-- no longer collide with the "bullet" group
physics.set_maskbit("#collisionobject", "bullet", false)
endParameters
url:string | Hash | Url— the collision object to change the mask of.group:string | Hash— the name of the group (maskbit) to modify in the mask.maskbit:boolean— boolean value of the new maskbit. 'true' to enable, 'false' to disable.
physics.set_shape(url: string | Hash | Url, shape: string | Hash, table: { type: typeof physics.SHAPE_TYPE_SPHERE; diameter: number } | { type: typeof physics.SHAPE_TYPE_BOX; dimensions: Vector3 } | { type: typeof physics.SHAPE_TYPE_CAPSULE; diameter: number; height: number }): void
Sets collision shape data for a collision object. Dimensions are in game units,
independent of the physics.scale setting. Please note that updating data in 3D
can be quite costly for box and capsules. Because of the physics engine, the cost
comes from having to recreate the shape objects when certain shapes needs to be updated.
function set_shape_data() {
// set capsule shape data
physics.set_shape("#collisionobject", "my_capsule_shape", {
type: physics.SHAPE_TYPE_CAPSULE,
diameter: 10,
height: 20,
});
// set sphere shape data
physics.set_shape("#collisionobject", "my_sphere_shape", {
type: physics.SHAPE_TYPE_SPHERE,
diameter: 10,
});
// set box shape data
physics.set_shape("#collisionobject", "my_box_shape", {
type: physics.SHAPE_TYPE_BOX,
dimensions: vmath.vector3(10, 10, 5),
});
}local function set_shape_data()
-- set capsule shape data
local data = {}
data.type = physics.SHAPE_TYPE_CAPSULE
data.diameter = 10
data.height = 20
physics.set_shape("#collisionobject", "my_capsule_shape", data)
-- set sphere shape data
data = {}
data.type = physics.SHAPE_TYPE_SPHERE
data.diameter = 10
physics.set_shape("#collisionobject", "my_sphere_shape", data)
-- set box shape data
data = {}
data.type = physics.SHAPE_TYPE_BOX
data.dimensions = vmath.vector3(10, 10, 5)
physics.set_shape("#collisionobject", "my_box_shape", data)
endParameters
url:string | Hash | Url— the collision object.shape:string | Hash— the name of the shape to get data for.table:{ type: typeof physics.SHAPE_TYPE_SPHERE; diameter: number } | { type: typeof physics.SHAPE_TYPE_BOX; dimensions: Vector3 } | { type: typeof physics.SHAPE_TYPE_CAPSULE; diameter: number; height: number }— updated collision shape data Hull and mesh geometry cannot be changed with this function.
physics.set_vflip(url: string | Hash | Url, flip: boolean): void
Flips the collision shapes vertically for a collision object
export default defineScript({
init() {
const flipv = true; // set on some condition
physics.set_vflip("#collisionobject", flipv);
return { flipv };
},
});function init(self)
self.flipv = true -- set on some condition
physics.set_vflip("#collisionobject", self.flipv)
endParameters
url:string | Hash | Url— the collision object that should flip its shapesflip:boolean—trueif the collision object should flip its shapes,falseif not
physics.update_mass(collisionobject: string | Hash | Url, mass: number): void
The function recalculates the density of each shape based on the total area of all shapes and the specified mass, then updates the mass of the body accordingly. Note: Currently only supported in 2D physics.
physics.update_mass("#collisionobject", 14); physics.update_mass("#collisionobject", 14)Parameters
collisionobject:string | Hash | Url— the collision object whose mass needs to be updated.mass:number— the new mass value to set for the collision object.
physics.wakeup(url: string | Hash | Url): void
Collision objects tend to fall asleep when inactive for a small period of time for efficiency reasons. This function wakes them up.
export default defineScript({
on_input(self, action_id, action) {
if (action_id === hash("test") && action.pressed) {
physics.wakeup("#collisionobject");
}
},
});function on_input(self, action_id, action)
if action_id == hash("test") and action.pressed then
physics.wakeup("#collisionobject")
end
endParameters
url:string | Hash | Url— the collision object to wake.
Constants
physics.JOINT_TYPE_FIXED: number & { readonly __brand: "physics.JOINT_TYPE_FIXED" }
Fixed joint; uses max_length from physics.joint_properties.
physics.JOINT_TYPE_HINGE: number & { readonly __brand: "physics.JOINT_TYPE_HINGE" }
Hinge joint; uses the angular-limit and motor fields from physics.joint_properties.
physics.JOINT_TYPE_SLIDER: number & { readonly __brand: "physics.JOINT_TYPE_SLIDER" }
Slider joint; uses the translation-limit and motor fields from physics.joint_properties.
physics.JOINT_TYPE_SPRING: number & { readonly __brand: "physics.JOINT_TYPE_SPRING" }
Spring joint; uses length, frequency, and damping from physics.joint_properties.
physics.JOINT_TYPE_WELD: number & { readonly __brand: "physics.JOINT_TYPE_WELD" }
Weld joint; uses reference_angle, frequency, and damping from physics.joint_properties.
physics.JOINT_TYPE_WHEEL: number & { readonly __brand: "physics.JOINT_TYPE_WHEEL" }
Wheel joint; uses the axis, motor, frequency, and damping fields from physics.joint_properties.
physics.SHAPE_TYPE_BOX: number & { readonly __brand: "physics.SHAPE_TYPE_BOX" }
Box shape.
physics.SHAPE_TYPE_CAPSULE: number & { readonly __brand: "physics.SHAPE_TYPE_CAPSULE" }
Capsule shape; supported only by 3D physics.
physics.SHAPE_TYPE_HULL: number & { readonly __brand: "physics.SHAPE_TYPE_HULL" }
Convex hull shape.
physics.SHAPE_TYPE_SPHERE: number & { readonly __brand: "physics.SHAPE_TYPE_SPHERE" }
Sphere shape.
Properties
angular_damping: number
The angular damping value for the collision object. Setting this value alters the damping of angular motion of the object (rotation). Valid values are between 0 (no damping) and 1 (full damping).
angular_velocity: Vector3
The current angular velocity of the collision object component as a vector3. The velocity is measured as a rotation around the vector with a speed equivalent to the vector length in radians/s.
linear_damping: number
The linear damping value for the collision object. Setting this value alters the damping of linear motion of the object. Valid values are between 0 (no damping) and 1 (full damping).
linear_velocity: Vector3
The current linear velocity of the collision object component as a vector3. The velocity is measured in units/s (pixels/s).
mass: number
READ ONLY Returns the defined physical mass of the collision object component as a number.