Skip to main content

ConstraintComponent

Class

Used to apply specific restrictions on how a SceneObject with BodyComponent can move.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Since Lens Scripting v164

Index

Remarks

This component allows developers to simulate certain types of mechanical connections or joints between objects.

Example

// Given two objects, with hangingBox below staticBox
// apply a constraint on hangingBox so that it hangs on the staticBox
// Try assigning a box object for each input!

// @input SceneObject staticBox
// @input SceneObject hangingBox

// Setup the boxes to have physics
var staticBox = script.staticBox;
var staticBoxBody = staticBox.createComponent(BodyComponent.getTypeName());
staticBoxBody.dynamic = false;

var hangingBox = script.hangingBox;
var hangingBoxBody = hangingBox.createComponent(BodyComponent.getTypeName());

// Create a child object that we can set up as our hinge
var hingeObj = global.scene.createSceneObject("hinge");
hingeObj.setParent(hangingBox);

// First we use the child object as a constraint
var hingeConstraint = hingeObj.createComponent(ConstraintComponent.getTypeName());

// Then we set up the constraint as a hinge
hingeConstraint.debugDrawEnabled = true;
hingeConstraint.constraint = Physics.Constraint.create(Physics.ConstraintType.Hinge);

// Attach our hinge to another Physics body
hingeConstraint.target = staticBoxBody;

// Position the hinge
hingeObj.getTransform().setLocalPosition(new vec3(-7.5, 7.5, 0))

// Tell the system to recalculate the simulation based on the above parameters
hingeConstraint.reanchorTarget();

Extends

Properties

constraint

constraint: Constraint

Dictates constraint type and settings. Note, when setting this field it creates a copy of the constraint, rather than referencing it. So if you intend to modify the constraint after assigning it, you must do so on the component's constraint field, rather than the source constraint object.

Since

Lens Scripting Version 164

Default Value

null

debugDrawEnabled

debugDrawEnabled: boolean

Show the constraint with debug-draw.

Since

Lens Scripting Version 164

Default Value

false

enabled

enabled: boolean

If disabled, the Component will stop enacting its behavior.

Default Value

true

Inherited from

Component.enabled


isBeingDestroyed

readonly isBeingDestroyed: boolean

Returns true if this Component is currently being destroyed. Becomes true as soon as destruction begins via Component#destroy or destruction of its owning SceneObject, before any OnDestroyEvent callbacks fire.

Since

Lens Scripting Version 364

Example

if (!script.isBeingDestroyed) {
// Component is still fully alive
}

Inherited from

Component.isBeingDestroyed


isEnabledInHierarchy

readonly isEnabledInHierarchy: boolean

Returns true if this Component, its SceneObject, and all of that SceneObjects parents are enabled.

Inherited from

Component.isEnabledInHierarchy


linkedBodyCollisionEnabled

linkedBodyCollisionEnabled: boolean

Controls whether the two bodies connected by this constraint are allowed to collide with each other. When enabled, connected bodies can collide normally. When disabled, collision detection between them is suppressed. Disable for doors and joints to avoid jitter; keep enabled for ragdolls where adjacent limbs must collide to prevent interpenetration.

Since

Lens Scripting Version 359

Default Value

true

sceneObject

readonly sceneObject: SceneObject

The SceneObject this component is on.

Inherited from

Component.sceneObject


target

target: ColliderComponent

Reference to connected target collider. If null, constraint target is attached to a fixed world transform.

Since

Lens Scripting Version 164

Default Value

null

uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Component.uniqueIdentifier

Methods

destroy()

destroy(): void

Destroys the component.

Returns

void

Inherited from

Component.destroy


getSceneObject()

getSceneObject(): SceneObject

Returns the SceneObject the component is attached to.

Returns

SceneObject

Inherited from

Component.getSceneObject


getTransform()

getTransform(): Transform

Returns the Transform this component is attached to.

Returns

Transform

Inherited from

Component.getTransform


getTypeName()

static getTypeName(): TypeName<ConstraintComponent>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<ConstraintComponent>

Since

Lens Scripting Version 164

Overrides

Component.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

Component.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

Component.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

Component.isSame


reanchorTarget()

reanchorTarget(): void

The target is attached to the constraint by a fixed local-space matrix, calculated from the difference between the target's and the constraint's world-space transforms. This local-space matrix is generated on-load, or whenever the target is changed. Call this function to explicitly recalculate it for the current world-space transforms.

Returns

void

Since

Lens Scripting Version 164

See also

Constrain Types guide.