Physics¶
The Physics Lua package is a module designed to manipulate physical interactions in a given environment, providing an intuitive API for handling these operations. It enables developers to simulate real-world physics in applications created with the Lua programming language. This package provides functionalities for controlling physical entities, their properties, and their interactions.
The Physics package allows manipulation of the environment’s global gravity vector, adds or removes rigid bodies, manages their physical properties, applies forces, impulses, and torques, and more. Additionally, it allows the definition of constraints between bodies, such as hinge, slider, and cone twist constraints, providing a rich toolset for physics simulation in 3D applications.
Reference¶
A module to handle physical actions in the environment
- physics.addPointToPointConstraint(bodyId1, bodyId2, point1, point2, disableCollisions)¶
Adds a point-to-point constraint between two bodies
- Parameters:¶
bodyId1 (
number
) – The id of the first bodybodyId2 (
number
) – The id of the second bodypoint1 (
Vector3
) – The point of the first body where the constraint is attachedpoint2 (
Vector3
) – The point of the second body where the constraint is attacheddisableCollisions (
boolean
) – Whether to disable collisions between the two bodies
- Returns:¶
The id of the constraint added
- Return type:¶
number
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
local physics = require 'engine/physics' -- This example creates a chain of boxes linked by a pointTopoint constraint local box1ID = addBody("Box1", 1) -- Ensure "Box1" is created local box2ID = addBody("Box2", 1) -- Ensure "Box2" is created local box3ID = addBody("Box3", 1) -- Ensure "Box3" is created local box4ID = addBody("Box4", 1) -- Ensure "Box4" is created local box5ID = addBody("Box5", 1) -- Ensure "Box5" is created local pivot1 = Vector3.new(0, -0.5, 0) -- For Box1 (bottom box) local pivot2 = Vector3.new(0, 0.5, 0) -- For Box2 (top box) -- P2P Constraint physics.addPointToPointConstraint(box1ID, box2ID, pivot1, pivot2, true) physics.addPointToPointConstraint(box2ID, box3ID, pivot1, pivot2, true) physics.addPointToPointConstraint(box3ID, box4ID, pivot1, pivot2, true) physics.addPointToPointConstraint(box4ID, box5ID, pivot1, pivot2, true)
- physics.addHingeConstraint(bodyId1, bodyId2, point1, point2, axis1, axis2, useReferenceFrameA, disableCollisions)¶
Adds a hinge constraint between two bodies
- Parameters:¶
bodyId1 (
number
) – The id of the first bodybodyId2 (
number
) – The id of the second bodypoint1 (
Vector3
) – The point of the first body where the constraint is attachedpoint2 (
Vector3
) – The point of the second body where the constraint is attachedaxis1 (
Vector3
) – The hinge axis of the first bodyaxis2 (
Vector3
) – The hinge axis of the second bodyuseReferenceFrameA (
boolean
) – Whether to use the reference frame of the first bodydisableCollisions (
boolean
) – Whether to disable collisions between the two bodies
- Returns:¶
The id of the constraint added
- Return type:¶
number
- physics.addSliderConstraint(bodyId1, bodyId2, frame1, frame2, useLinearReferenceFrameA, disableCollisions)¶
Adds a slider constraint between two bodies
- Parameters:¶
bodyId1 (
number
) – Identifier of the first body to be constrainedbodyId2 (
number
) – Identifier of the second body to be constrainedframe1 (
Transform
) – Location and orientation on the first body where the constraint is attachedframe2 (
Transform
) – Location and orientation on the second body where the constraint is attacheduseLinearReferenceFrameA (
boolean
) – Defines linear part of the constraint in the frame of first body if truedisableCollisions (
boolean
) – Allows the bodies to pass through each other if set to true
- Returns:¶
The id of the newly added constraint
- Return type:¶
number
- physics.addConeTwistConstraint(bodyId1, bodyId2, frame1, frame2, disableCollisions)¶
Adds a cone twist constraint between two bodies
- Parameters:¶
bodyId1 (
number
) – The id of the first bodybodyId2 (
number
) – The id of the second bodyframe1 (
Transform
) – Location and orientation on the first body where the constraint is attachedframe2 (
Transform
) – Location and orientation on the second body where the constraint is attacheddisableCollisions (
boolean
) – Whether to disable collisions between the two bodies
- Returns:¶
The id of the constraint added
- Return type:¶
number
- physics.removeConstraint(id)¶
Removes a constraint from the physics simulation.
- Parameters:¶
id (
number
) – The id of the constraint to be removed
- physics.raycast(startVector, endVector, type, returnObjects)¶
Do a raycast to test for any objects along a ray
- Parameters:¶
- Returns:¶
raycast result containing an array of hit points or a single hit point depending on type
- Return type:¶
Promise
Usage:
1 2 3 4 5 6
local Physics = require 'engine/physics' local Console = require 'engine/console' local Tools = require 'engine/tools' Physics.raycast(Vector3.new(0, 0, 0), Vector3.new(1, 0, 0), Physics.RaycastType.Closest, true):next(function(hits) Console.log(Tools.dump(hits)) end)
- physics.raycastMany(startVectors, endVectors, type, returnObjects)¶
Do many raycasts to test for any objects along a ray. The usage is similar to a single raycast, but
the single parameter takes an array of raycast options
- Parameters:¶
startVectors (
array
) – An array of start vectorsendVectors (
array
) – An array of end vectorstype (
str
) – type of raycast. See RaycastType enumreturnObjects (
boolean
) – return the hit objects, otherwise only point and normal is returned
- Returns:¶
array An array of raycast results
- Return type:¶
Promise
Usage:
1 2 3 4 5 6
local Physics = require 'engine/physics' local Console = require 'engine/console' local Tools = require 'engine/tools' Physics.raycastMany({{ startVec=Vector3.new(0, 0, 0), endVec=Vector3.new(1, 0, 0) }}, Physics.RaycastType.Closest, true):next(function(hits) Console.log(Tools.dump(hits)) end)
- class physics.RaycastType¶
Enumeration of all raycast types
Name
Description
Closest
Only the closest hit is returned
AllHits
All hits along the ray are returned