Skip to content

transform

A hierarchical transform storing 3D or 2D position, rotation (as a quaternion), and scale. Can have a parent transform. Freed automatically on GC.

pos accessor

Get or set the transform's position as a 3D vector [x, y, z].

value: (when setting) [x, y, z].

Returns: The current position vector (when getting), or None (when setting).

scale accessor

Get or set the transform's scale as a 3D vector [x, y, z]. For 2D usage, z is often 1.

value: (when setting) [sx, sy, sz].

Returns: The current scale (when getting), or None (when setting).

rotation accessor

Get or set the transform's rotation as a quaternion [x, y, z, w]. Angles in degrees or radians must first be converted prior to making a quaternion.

value: (when setting) [qx, qy, qz, qw].

Returns: The current quaternion (when getting), or None (when setting).

parent accessor

Get or set the transform's parent. If set, this transform becomes a child of the parent (re-parenting). Must be another transform object or undefined.

value: (when setting) Another transform or undefined.

Returns: The current parent transform (when getting), or None (when setting).

change_hook accessor

A user-supplied function that's called whenever the transform's local matrix changes. If undefined, no hook is called.

value: (when setting) A function.

Returns: The current function or undefined.

trs(pos, quat, scale) function

Set the transform's position, rotation, and scale in one call.

pos: [x,y,z] for position, or undefined to keep existing.

quat: [qx,qy,qz,qw] for rotation, or undefined.

scale: [sx,sy,sz] for scale, or undefined.

Returns: None

phys2d(velocity, angularVel, dt) function

Apply simple 2D velocity and angular velocity to this transform.

velocity: [vx, vy] added to 'pos' each frame.

angularVel: A scalar for rotation in (radians/second).

dt: The time delta in seconds.

Returns: None

move(delta) function

Translate this transform by the specified vector.

delta: [dx, dy, dz] to add to .pos

Returns: None

rotate(axis, angle) function

Rotate this transform by an axis+angle.

axis: [ax, ay, az] the axis of rotation.

angle: The angle in turns or radians (depending on usage).

Returns: None

angle(axis) function

Return the transform's rotation about a specified axis (x, y, or z). For example, angle([1,0,0]) returns the roll about the X-axis.

axis: Which axis [1,0,0] or [0,1,0] or [0,0,1].

Returns: The numeric angle in 'turns' or radians, depending on usage.

lookat(target) function

Rotate this transform so it looks toward the given world position.

target: [x, y, z] position in world coords.

Returns: None

direction(localDir) function

Rotate a vector by this transform's rotation, effectively "transforming" a direction from local space to world space.

localDir: [dx, dy, dz] in local transform coordinates.

Returns: [dx', dy', dz'] direction in world space.

unit() function

Reset position, rotation, and scale to [0,0,0], identity rotation, and [1,1,1].

Returns: None

rect(rect) function

Set this transform's pos and scale from a 2D rect object {x, y, w, h}.

rect: Object with .x, .y, .w, .h

Returns: None

array() function

Return this transform's matrix as a 16-element float array in column-major order.

Returns: An array of 16 floats.

torect() function

Convert transform's 2D position/scale to a rect {x, y, w, h}. Rotation is currently ignored.

Returns: A rect object {x, y, w, h}.

children() function

Return an array of child transforms belonging to this transform.

Returns: An array of transform objects.