Home

Physics Scripting API

Scripts are plain JavaScript that run once per simulation frame. Add one from the Scripts panel in the editor and use the globals below to read and move objects.

Read-Only Globals

Parameters

obj.idstringUnique identifier for the object. Use this in all API calls.
obj.namestringHuman-readable name set in the sidebar.
obj.type"cube" | "sphere"Shape type of the object.
obj.colorstringHex color string, e.g. "#f87171".
obj.currentPosition[x, y, z]Live world-space position updated every frame.

Example

if (self) {\n  const [x, y, z] = self.currentPosition;\n  log('My position:', x.toFixed(2), y.toFixed(2), z.toFixed(2));\n}

Example

// Count frames\nif (!__state.frame) __state.frame = 0;\n__state.frame++;\nif (__state.frame % 60 === 0) log('tick', __state.frame / 60);

Example

// Oscillate gravity every 2 seconds\nconst dir = Math.sin(time * Math.PI) > 0 ? 1 : -1;

Example

// Move 1 unit per second regardless of frame rate\nconst speed = 1 * delta;

Console Logging

Example

log('time:', time.toFixed(2));\nlog('object count:', objects.length);\nlog('pos:', objects[0]?.currentPosition);

Example

if (objects.length === 0) {\n  warn('No objects in scene!');\n}

Example

if (!rigidBody) {\n  error('Missing rigid body for', obj.id);\n}

Spawning Objects

Parameters

type"cube" | "sphere"Shape of the object to spawn.
xnumber?Initial X position. Defaults to random.
ynumber?Initial Y position. Defaults to ~4–7 units above floor.
znumber?Initial Z position. Defaults to random.
colorstring?Hex color, e.g. "#22d3ee". Defaults to next palette color.

Example

// Spawn a sphere every 2 seconds
if (Math.floor(time) % 2 === 0 && Math.floor(time) !== Math.floor(time - delta)) {
  const obj = addObject('sphere', 0, 6, 0, '#22d3ee');
  if (obj) log('Spawned', obj.name, 'id:', obj.id);
}

Velocity & Forces

Parameters

idstringThe object's id.
vxnumberVelocity along the X axis (left/right) in units/sec.
vynumberVelocity along the Y axis (up/down) in units/sec.
vznumberVelocity along the Z axis (forward/back) in units/sec.

Example

// Launch all objects upward at 10 units/sec\nfor (const obj of objects) {\n  setVelocity(obj.id, 0, 10, 0);\n}

Parameters

idstringThe object's id.
ixnumberImpulse magnitude along X. Positive = right.
iynumberImpulse magnitude along Y. Positive = up.
iznumberImpulse magnitude along Z. Positive = back.

Example

// Push objects outward from origin\nfor (const obj of objects) {\n  const [x,,z] = obj.currentPosition;\n  applyImpulse(obj.id, x * 0.1, 0, z * 0.1);\n}

Parameters

idstringThe object's id.
txnumberTorque around X axis (pitch/tumble).
tynumberTorque around Y axis (yaw/spin).
tznumberTorque around Z axis (roll).

Example

// Spin all cubes on the Y axis\nfor (const obj of objects) {\n  if (obj.type === 'cube') {\n    applyTorque(obj.id, 0, 1, 0);\n  }\n}

Parameters

idstringThe object's id.
scalenumber0 = floating. 1 = normal. -1 = reversed. 2 = double gravity.

Example

// Make spheres float, cubes fall normally\nfor (const obj of objects) {\n  if (obj.type === 'sphere') {\n    setGravityScale(obj.id, 0);\n  }\n}

Vertex Manipulation

Parameters

idstring?Object id. Omit to use the script's bound object.

Example

const verts = getVertices();\nif (verts) log('vertex count:', verts.length / 3);

Parameters

indexnumberVertex index (0-based).
x, y, znumberNew position for this vertex.
idstring?Object id. Omit to use bound object.

Example

// Displace every vertex by a sine wave\nconst verts = getVertices();\nconst count = verts.length / 3;\nfor (let i = 0; i < count; i++) {\n  const ox = verts[i*3], oy = verts[i*3+1], oz = verts[i*3+2];\n  setVertex(i, ox, oy + Math.sin(time + ox) * 0.1, oz);\n}\nupdateVertices();

Example

updateVertices(); // after all setVertex calls

Script Sliders

Parameters

namestringLabel shown in the UI.
minnumberMinimum value.
maxnumberMaximum value.
defaultnumberInitial value before user adjusts it.
type"float" | "int"Whether to snap to whole numbers. Defaults to "float".

Example

// Expose gravity and wave speed as sliders\nconst gravity = slider('Gravity', -0.1, 0, -0.015);\nconst speed = slider('Wave Speed', 0, 5, 1, 'float');\nconst amp = slider('Amplitude', 0, 2, 0.3);