Namespace: bullet3d.constraint
Language: Lua
Type: Defold Lua
File: script_bullet3d_constraint.cpp
Source: engine/gamesys/src/gamesys/scripts/bullet3d/script_bullet3d_constraint.cpp
Creates and controls Bullet constraints between Defold rigid bodies. A
constraint belongs to the supplied world and is destroyed automatically
with either body, with the world, or when the module is finalized. It is
temporarily removed from the native world while either linked body is
disabled and is restored when both bodies are enabled again. Dropping its
Lua userdata does not destroy the native constraint; call destroy for
early release.
Creator positions and all other linear values use Defold units and are
converted with physics.scale. Angles are radians. Axes are one-based in
Lua: axes 1-3 are linear and axes 4-6 are angular. Mutating functions cannot
be called while the physics world is stepping. Floating-point and vector
inputs must be finite. Axis vectors must be non-zero and are normalized.
Input rotations must be finite, non-zero quaternions and are normalized by
the binding.
CONSTRAINT_TYPE_* values identify the concrete constraint exposed by this
binding. This deliberately distinguishes universal, hinge2, and spring 6-DOF
constraints independently of Bullet’s internal constraint type hierarchy.
Type: TYPEDEF Bullet typed constraint
Parameters
value (userdata) - opaque constraint handleType: STRUCT Universal and hinge2 constraint parameters
Members
anchor (vector3) - world-space anchoraxis1 (vector3) - first non-zero world-space axisaxis2 (vector3) - second non-zero world-space axis, orthogonal to axis1collide_connected? (boolean) - whether connected bodies can collide; defaults to falseType: STRUCT The frame-B fields are required for a two-body constraint.
Members
frame_a_position (vector3) - local body-A frame positionframe_a_rotation (quaternion) - local body-A frame rotationframe_b_position? (vector3) - local body-B frame positionframe_b_rotation? (quaternion) - local body-B frame rotationangular_only? (boolean) - whether to constrain angular motion onlycollide_connected? (boolean) - whether connected bodies can collide; defaults to falseType: ENUM Constraint types
Members
bullet3d.constraint.CONSTRAINT_TYPE_CONE_TWIST - Cone-twist constraint typebullet3d.constraint.CONSTRAINT_TYPE_GENERIC_6DOF - Generic 6-DOF constraint typebullet3d.constraint.CONSTRAINT_TYPE_GENERIC_6DOF_SPRING - Generic spring 6-DOF constraint typebullet3d.constraint.CONSTRAINT_TYPE_HINGE - Hinge constraint typebullet3d.constraint.CONSTRAINT_TYPE_HINGE2 - Hinge2 constraint typebullet3d.constraint.CONSTRAINT_TYPE_POINT_TO_POINT - Point-to-point constraint typebullet3d.constraint.CONSTRAINT_TYPE_SLIDER - Slider constraint typebullet3d.constraint.CONSTRAINT_TYPE_UNIVERSAL - Universal constraint typeType: FUNCTION The world is derived from body_a.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody |
nil) - second body or world |
params (bullet3d.constraint.cone_twist_params) - local frames and optionsReturns
constraint (btTypedConstraint) - cone-twist constraintType: FUNCTION The params table requires local frame A and, for a two-body constraint, local frame B. It optionally accepts collide_connected. The world is derived from body_a. The active 6-DOF solver ignores its legacy linear-reference-frame selector, so that field is rejected rather than silently accepted.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody |
nil) - second body or world |
params (bullet3d.constraint.generic_6dof_params) - local frames and optionsReturns
constraint (btTypedConstraint) - generic 6-DOF constraintType: FUNCTION Both bodies and both local frames are required. The params table optionally accepts collide_connected. The world is derived from body_a. The active spring 6-DOF solver ignores its legacy linear-reference-frame selector, so that field is rejected rather than silently accepted.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody) - second bodyparams (bullet3d.constraint.generic_6dof_spring_params) - local frames and optionsReturns
constraint (btTypedConstraint) - spring 6-DOF constraintExamples
Create a spring that moves along its first linear axis:
function init(self)
local body_a = bullet3d.get_rigid_body("/body_a#collisionobject")
local body_b = bullet3d.get_rigid_body("/body_b#collisionobject")
self.spring = bullet3d.constraint.create_generic_6dof_spring(body_a, body_b, {
frame_a_position = vmath.vector3(),
frame_a_rotation = vmath.quat(),
frame_b_position = vmath.vector3(),
frame_b_rotation = vmath.quat(),
})
bullet3d.constraint.set_limit(self.spring, 1, -1, 1)
bullet3d.constraint.enable_spring(self.spring, 1, true)
bullet3d.constraint.set_spring_stiffness(self.spring, 1, 20)
bullet3d.constraint.set_spring_damping(self.spring, 1, 0.5)
bullet3d.constraint.set_spring_equilibrium_point(self.spring, 1, 0)
end
function final(self)
if self.spring and bullet3d.constraint.is_valid(self.spring) then
bullet3d.constraint.destroy(self.spring)
end
end
Type: FUNCTION The world is derived from body_a.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody |
nil) - second body or world |
params (bullet3d.constraint.hinge_params) - local frames and optionsReturns
constraint (btTypedConstraint) - hinge constraintExamples
Create a motorized hinge with a 90-degree range:
function init(self)
local body_a = bullet3d.get_rigid_body("/door#collisionobject")
local body_b = bullet3d.get_rigid_body("/frame#collisionobject")
self.hinge = bullet3d.constraint.create_hinge(body_a, body_b, {
frame_a_position = vmath.vector3(-0.5, 0, 0),
frame_a_rotation = vmath.quat(),
frame_b_position = vmath.vector3(0.5, 0, 0),
frame_b_rotation = vmath.quat(),
})
bullet3d.constraint.set_hinge_limits(self.hinge, -math.pi / 4, math.pi / 4)
bullet3d.constraint.set_hinge_motor(self.hinge, true, 1.5, 2.5)
end
function final(self)
if self.hinge and bullet3d.constraint.is_valid(self.hinge) then
bullet3d.constraint.destroy(self.hinge)
end
end
Type: FUNCTION Both bodies are required. Its initial linear suspension travel is one Defold unit in either direction. The world is derived from body_a.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody) - second bodyparams (bullet3d.constraint.anchor_axes_params) - anchor, axes, and optionsReturns
constraint (btTypedConstraint) - hinge2 constraintType: FUNCTION The world is derived from body_a; both bodies must belong to that same world.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody |
nil) - second body or world |
params (bullet3d.constraint.point_to_point_params) - pivots and optionsReturns
constraint (btTypedConstraint) - point-to-point constraintExamples
Join two bodies at matching local pivots and explicitly destroy the constraint when the script is finalized:
function init(self)
local body_a = bullet3d.get_rigid_body("/body_a#collisionobject")
local body_b = bullet3d.get_rigid_body("/body_b#collisionobject")
self.constraint = bullet3d.constraint.create_point_to_point(body_a, body_b, {
pivot_a = vmath.vector3(0.5, 0, 0),
pivot_b = vmath.vector3(-0.5, 0, 0),
})
end
function final(self)
if self.constraint and bullet3d.constraint.is_valid(self.constraint) then
bullet3d.constraint.destroy(self.constraint)
end
end
Type: FUNCTION The world is derived from body_a.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody |
nil) - second body or world |
params (bullet3d.constraint.slider_params) - local frames and optionsReturns
constraint (btTypedConstraint) - slider constraintType: FUNCTION Both bodies are required. The world is derived from body_a.
Parameters
body_a (btRigidBody) - first bodybody_b (btRigidBody) - second bodyparams (bullet3d.constraint.anchor_axes_params) - anchor, axes, and optionsReturns
constraint (btTypedConstraint) - universal constraintType: FUNCTION Destroy a constraint
Parameters
constraint (btTypedConstraint) - constraintType: FUNCTION Enable or disable the cone-twist motor
Parameters
constraint (btTypedConstraint) - cone-twist constraintenabled (boolean) - motor stateType: FUNCTION Enable or disable a spring axis
Parameters
constraint (btTypedConstraint) - spring 6-DOF or hinge2 constraintaxis (integer) - one-based axis from 1 to 6enabled (boolean) - spring stateType: STRUCT The frame-B fields are required for a two-body constraint.
Members
frame_a_position (vector3) - local body-A frame positionframe_a_rotation (quaternion) - local body-A frame rotationframe_b_position? (vector3) - local body-B frame positionframe_b_rotation? (quaternion) - local body-B frame rotationcollide_connected? (boolean) - whether connected bodies can collide; defaults to falseType: STRUCT Generic spring 6-DOF constraint parameters
Members
frame_a_position (vector3) - local body-A frame positionframe_a_rotation (quaternion) - local body-A frame rotationframe_b_position (vector3) - local body-B frame positionframe_b_rotation (quaternion) - local body-B frame rotationcollide_connected? (boolean) - whether connected bodies can collide; defaults to falseType: FUNCTION Get a current 6-DOF angle
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based angular-axis index from 1 to 3Returns
angle (number) - current angle in radiansType: FUNCTION Get a current 6-DOF angular axis
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based angular-axis index from 1 to 3Returns
direction (vector3) - world-space unit axisType: FUNCTION Axes 1-3 are linear and axes 4-6 are angular. Generic 6-DOF, generic spring 6-DOF, and universal constraints support bounce only on angular axes; hinge2 supports it on every axis. Linear target velocity uses Defold units per second and angular target velocity uses radians per second. max_force is a force for linear axes and a torque in Defold squared units for angular axes.
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based axis from 1 to 6Returns
enabled (boolean) - motor statetarget_velocity (number) - linear or angular target velocitymax_force (number) - maximum motor force for linear axes or torque for angular axesbounce (number) - bounce from 0 to 1Type: FUNCTION Get a current 6-DOF linear position
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based linear-axis index from 1 to 3Returns
position (number) - relative position in Defold unitsType: FUNCTION Get universal or hinge2 anchors
Parameters
constraint (btTypedConstraint) - universal or hinge2 constraintReturns
anchor_a (vector3) - world-space anchor on body Aanchor_b (vector3) - world-space anchor on body BType: FUNCTION Get universal or hinge2 angles
Parameters
constraint (btTypedConstraint) - universal or hinge2 constraintReturns
angle_1 (number) - first angle in radiansangle_2 (number) - second angle in radiansType: FUNCTION Get universal or hinge2 axes
Parameters
constraint (btTypedConstraint) - universal or hinge2 constraintReturns
axis_1 (vector3) - first world-space unit axisaxis_2 (vector3) - second world-space unit axisType: FUNCTION Get the first linked body
Parameters
constraint (btTypedConstraint) - constraintReturns
body (btRigidBody) - first bodyType: FUNCTION Get the second linked body
Parameters
constraint (btTypedConstraint) - constraintReturns
body (btRigidBody |
nil) - second body, or nil for a world constraint |
Type: FUNCTION Get whether connected bodies can collide
Parameters
constraint (btTypedConstraint) - constraintReturns
collide (boolean) - whether connected bodies can collideType: FUNCTION Get cone-twist angular spans
Parameters
constraint (btTypedConstraint) - cone-twist constraintReturns
swing_span_1 (number) - first swing span in radiansswing_span_2 (number) - second swing span in radianstwist_span (number) - twist span in radiansType: FUNCTION Supported constraint types are hinge, cone-twist, generic 6-DOF, generic spring 6-DOF, slider, universal, and hinge2. Point-to-point constraints use get_pivots instead. Returns position and rotation. For one-body generic 6-DOF and slider constraints this is the user-body frame, despite Bullet storing it as its native frame B.
Parameters
constraint (btTypedConstraint) - framed constraintReturns
position (vector3) - local positionrotation (quaternion) - local rotationType: FUNCTION Supports the same constraint types as get_frame_a. For a one-body constraint, this is the frame attached to the fixed world body.
Parameters
constraint (btTypedConstraint) - framed constraintReturns
position (vector3) - local position or world frame positionrotation (quaternion) - local rotation or world frame rotationType: FUNCTION Get the current hinge angle
Parameters
constraint (btTypedConstraint) - hinge constraintReturns
angle (number) - angle in radiansType: FUNCTION Get hinge angular limits
Parameters
constraint (btTypedConstraint) - hinge constraintReturns
lower (number) - lower angle in radiansupper (number) - upper angle in radiansType: FUNCTION Get hinge motor settings
Parameters
constraint (btTypedConstraint) - hinge constraintReturns
enabled (boolean) - motor statetarget_velocity (number) - angular target velocity in radians per secondmax_impulse (number) - maximum angular motor impulse in Defold squared unitsType: FUNCTION Axes 1-3 return linear limits in Defold units. Axes 4-6 return angular limits in radians.
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based axis from 1 to 6Returns
lower (number) - lower limitupper (number) - upper limitType: FUNCTION Get point-to-point pivots
Parameters
constraint (btTypedConstraint) - point-to-point constraintReturns
pivot_a (vector3) - local body-A pivotpivot_b (vector3) - local body-B pivot or world anchorType: FUNCTION Get slider limits
Parameters
constraint (btTypedConstraint) - slider constraintReturns
lower_linear (number) - lower linear limit in Defold unitsupper_linear (number) - upper linear limit in Defold unitslower_angular (number) - lower angular limit in radiansupper_angular (number) - upper angular limit in radiansType: FUNCTION The linear motor uses Defold units per second and maximum force. The angular motor uses radians per second and maximum torque in Defold squared units.
Parameters
constraint (btTypedConstraint) - slider constraintmotor (string) - linear or angularReturns
enabled (boolean) - motor statetarget_velocity (number) - linear or angular target velocitymax_force (number) - maximum linear force or angular torqueType: FUNCTION Get the current slider position
Parameters
constraint (btTypedConstraint) - slider constraintReturns
position (number) - current linear position in Defold unitsType: FUNCTION Get the current cone-twist twist angle
Parameters
constraint (btTypedConstraint) - cone-twist constraintReturns
angle (number) - twist angle in radiansType: FUNCTION Get the constraint type
Parameters
constraint (btTypedConstraint) - constraintReturns
type (bullet3d.constraint.CONSTRAINT_TYPE) - constraint typeType: FUNCTION Returns a stable lowercase diagnostic name such as “hinge” or “generic_6dof_spring”.
Parameters
constraint (btTypedConstraint) - constraintReturns
name (string) - constraint type nameType: FUNCTION Get the slider linear reference-frame choice
Parameters
constraint (btTypedConstraint) - slider constraintReturns
use_frame_a (boolean) - true when linear calculations reference frame AType: FUNCTION Get the owning world
Parameters
constraint (btTypedConstraint) - constraintReturns
world (btDiscreteDynamicsWorld) - owning worldType: STRUCT The frame-B fields are required for a two-body constraint.
Members
frame_a_position (vector3) - local body-A frame positionframe_a_rotation (quaternion) - local body-A frame rotationframe_b_position? (vector3) - local body-B frame positionframe_b_rotation? (quaternion) - local body-B frame rotationuse_reference_frame_a? (boolean) - whether angular calculations reference frame Aangular_only? (boolean) - whether to constrain angular motion onlycollide_connected? (boolean) - whether connected bodies can collide; defaults to falseType: FUNCTION Test whether a constraint is active in its world
Parameters
constraint (btTypedConstraint) - constraintReturns
active (boolean) - false while a linked body is disabledType: FUNCTION Test angular-only mode
Parameters
constraint (btTypedConstraint) - hinge or cone-twist constraintReturns
angular_only (boolean) - angular-only stateType: FUNCTION Both a ranged and a locked axis are considered limited; a free axis is not.
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based axis from 1 to 6Returns
limited (boolean) - limit stateType: FUNCTION Test whether a cone-twist is past its swing limit
Parameters
constraint (btTypedConstraint) - cone-twist constraintReturns
past_limit (boolean) - swing-limit stateType: FUNCTION Test whether a constraint handle is valid
Parameters
constraint (btTypedConstraint) - constraint handleReturns
valid (boolean) - true while the native constraint existsType: STRUCT pivot_b is required for a two-body constraint. For a one-body constraint, it is an optional world-space anchor.
Members
pivot_a (vector3) - local body-A pivotpivot_b? (vector3) - local body-B pivot or world-space anchorcollide_connected? (boolean) - whether connected bodies can collide; defaults to falseType: FUNCTION Linear and angular values use the units described by get_6dof_motor.
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based axis from 1 to 6enabled (boolean) - motor statetarget_velocity (number) - linear or angular target velocitymax_force (number) - non-negative maximum motor force for linear axes or torque for angular axesbounce (number |
nil) (optional) - optional bounce from 0 to 1; defaults to 0 |
Type: FUNCTION Set angular-only mode
Parameters
constraint (btTypedConstraint) - hinge or cone-twist constraintangular_only (boolean) - angular-only stateType: FUNCTION Set cone-twist angular spans
Parameters
constraint (btTypedConstraint) - cone-twist constraintswing_span_1 (number) - non-negative first swing span in radiansswing_span_2 (number) - non-negative second swing span in radianstwist_span (number) - non-negative twist span in radianssoftness (number |
nil) (optional) - optional softness from 0 to 1; defaults to 1 |
bias (number |
nil) (optional) - optional bias from 0 to 1; defaults to 0.3 |
relaxation (number |
nil) (optional) - optional relaxation from 0 to 1; defaults to 1 |
Type: FUNCTION By default, target is the desired rotation of body A relative to body B. With constraint_space set, it is the desired rotation of frame A relative to frame B in constraint space.
Parameters
constraint (btTypedConstraint) - cone-twist constrainttarget (quaternion) - finite, non-zero target orientation; normalized by the bindingconstraint_space (boolean |
nil) (optional) - optional target-is-in-constraint-space flag; defaults to false |
Type: FUNCTION Frame mutation is supported for hinge, generic 6-DOF, generic spring 6-DOF, and slider constraints. Cone-twist, universal, and hinge2 frames are read-only through this API.
Parameters
constraint (btTypedConstraint) - mutable framed constraintposition (vector3) - finite local positionrotation (quaternion) - finite, non-zero local rotation; normalized by the bindingType: FUNCTION Supports the same constraint types as set_frame_a. For a one-body constraint, this changes the frame attached to the fixed world body.
Parameters
constraint (btTypedConstraint) - mutable framed constraintposition (vector3) - finite local position or world frame positionrotation (quaternion) - finite, non-zero local or world frame rotation; normalized by the bindingType: FUNCTION This function only supports hinges attached to the world. For a two-body hinge, change both local frames with set_frame_a and set_frame_b.
Parameters
constraint (btTypedConstraint) - one-body hinge constraintaxis (vector3) - non-zero axis in body-A spaceType: FUNCTION Set hinge angular limits
Parameters
constraint (btTypedConstraint) - hinge constraintlower (number) - lower angle in radiansupper (number) - upper angle in radiansbias (number |
nil) (optional) - optional limit bias from 0 to 1; defaults to 0.3 |
relaxation (number |
nil) (optional) - optional relaxation from 0 to 1; defaults to 1 |
Type: FUNCTION Set hinge motor settings
Parameters
constraint (btTypedConstraint) - hinge constraintenabled (boolean) - motor statetarget_velocity (number) - angular target velocity in radians per secondmax_impulse (number) - non-negative maximum angular motor impulse in Defold squared unitsType: FUNCTION Set a hinge motor angle target
Parameters
constraint (btTypedConstraint) - hinge constrainttarget_angle (number) - target angle in radianstime_step (number) - positive step duration in secondsType: FUNCTION Axes 1-3 use Defold units and axes 4-6 use radians. A lower value less than the upper value creates a limited range, equal values lock the axis, and a lower value greater than the upper value makes the axis free.
Parameters
constraint (btTypedConstraint) - 6-DOF-derived constraintaxis (integer) - one-based axis from 1 to 6lower (number) - lower limitupper (number) - upper limitType: FUNCTION Set point-to-point pivots
Parameters
constraint (btTypedConstraint) - point-to-point constraintpivot_a (vector3) - local body-A pivotpivot_b (vector3) - local body-B pivot or world anchorType: FUNCTION Each lower/upper pair follows Bullet’s limit convention: lower less than upper creates a limited range, equal values lock that axis, and lower greater than upper makes it free. Bullet normalizes the angular limits.
Parameters
constraint (btTypedConstraint) - slider constraintlower_linear (number) - lower linear limit in Defold unitsupper_linear (number) - upper linear limit in Defold unitslower_angular (number) - lower angular limit in radiansupper_angular (number) - upper angular limit in radiansType: FUNCTION Linear and angular values use the units described by get_slider_motor.
Parameters
constraint (btTypedConstraint) - slider constraintmotor (string) - linear or angularenabled (boolean) - motor statetarget_velocity (number) - linear or angular target velocitymax_force (number) - non-negative maximum linear force or angular torqueType: FUNCTION Generic spring 6-DOF constraints use a scale-independent damping factor from 0 to 1, where 1 means no damping. Hinge2 constraints use a damping coefficient where 0 means no damping and any non-negative value is accepted. Hinge2 angular damping is automatically converted using physics.scale squared.
Parameters
constraint (btTypedConstraint) - spring 6-DOF or hinge2 constraintaxis (integer) - one-based axis from 1 to 6damping (number) - damping value in the range required by the constraint typeType: FUNCTION With no axis, captures all current transforms. With an axis and no value, captures that axis. Linear values use Defold units and angular values use radians.
Parameters
constraint (btTypedConstraint) - spring 6-DOF or hinge2 constraintaxis (integer |
nil) (optional) - optional one-based axis from 1 to 6 |
value (number |
nil) (optional) - optional explicit equilibrium value |
Type: FUNCTION Linear stiffness values are independent of physics.scale. Angular stiffness values are automatically converted using physics.scale squared.
Parameters
constraint (btTypedConstraint) - spring 6-DOF or hinge2 constraintaxis (integer) - one-based axis from 1 to 6stiffness (number) - non-negative stiffnessType: STRUCT The frame-B fields are required for a two-body constraint.
Members
frame_a_position (vector3) - local body-A frame positionframe_a_rotation (quaternion) - local body-A frame rotationframe_b_position? (vector3) - local body-B frame positionframe_b_rotation? (quaternion) - local body-B frame rotationuse_linear_reference_frame_a? (boolean) - whether linear calculations reference frame Acollide_connected? (boolean) - whether connected bodies can collide; defaults to false