Skip to content

Transform

The Transform class represents an object's position, rotation, and scale within a 3D space. It facilitates common operations such as moving an object to a new position or by a specified offset, rotating it to face a target point and much more making it essential for handling object transformations in 3D environments and game development scenarios.

Usage

To use this class, add the following require at the top of your script:

local Transform = require 'engine/math/transform'

Reference

new

static new(position, rotation, scale)

Creates a transform object from existing position, rotation, and scale.

Parameters

  • position (Vector3): The position.
  • rotation (Quaternion): The rotation.
  • scale (Vector3): The scale.

Returns

  • Transform: new Transform object.

fromJson

static fromJson(json)

Creates a transform object from JSON.

Parameters

  • json (string): The JSON string.

Returns

  • Transform: new Transform object.

fromData

static fromData(data)

Creates a transform object from data.

Parameters

  • data (table): The data object containing position, rotation, and scale.

Returns

  • Transform: new Transform object.

toData

toData()

Converts the transform into a table representation.

Returns

  • table: data table.

clone

clone()

Clones the transform

Returns

  • table: cloned transform

moveTo

moveTo(x, y, z)

Moves the transform to a new position.

Parameters

  • x (number|Vector3): The x coordinate or a Vector3 position.
  • y (number): The y coordinate (if x is a number).
  • z (number): The z coordinate (if x is a number).

moveBy

moveBy(x, y, z)

Moves the transform by a given offset.

Parameters

  • x (number|Vector3): The x offset or a Vector3 offset.
  • y (number): The y offset (if x is a number).
  • z (number): The z offset (if x is a number).

lookAt

lookAt(x, y, z, up)

Rotates the transform to look at a target position.

Parameters

  • x (number|Vector3): The target x coordinate or a Vector3 target.
  • y (number|Vector3): The target y coordinate (if x is a number) or the up vector in case x is the look at Vector.
  • z (number): The target z coordinate (if x is a number).
  • up (Vector3): The up direction (default is Vector3(0,1,0)).

rotateTo

rotateTo(x, y, z)

Rotates the transform to a specific orientation. Angles are specified in degrees.

Parameters

  • x (number|Vector3): Rotation around the x-axis, or a Vector3 representing rotation.
  • y (number): Rotation around the y-axis (if x is a number).
  • z (number): Rotation around the z-axis (if x is a number).

rotateBy

rotateBy(x, y, z)

Rotates the transform by the given amount. Angles are specified in degrees.

Parameters

  • x (number|Vector3): Rotation to apply around the x-axis, or a Vector3 representing rotation.
  • y (number): Rotation to apply around the y-axis (if x is a number).
  • z (number): Rotation to apply around the z-axis (if x is a number).

Class: Transform

new

static new(position, rotation, scale)

Creates a transform object from existing position, rotation, and scale.

Parameters

  • position (Vector3): The position.
  • rotation (Quaternion): The rotation.
  • scale (Vector3): The scale.

Returns

  • Transform: new Transform object.

fromJson

static fromJson(json)

Creates a transform object from JSON.

Parameters

  • json (string): The JSON string.

Returns

  • Transform: new Transform object.

fromData

static fromData(data)

Creates a transform object from data.

Parameters

  • data (table): The data object containing position, rotation, and scale.

Returns

  • Transform: new Transform object.

toData

toData()

Converts the transform into a table representation.

Returns

  • table: data table.

clone

clone()

Clones the transform

Returns

  • table: cloned transform

moveTo

moveTo(x, y, z)

Moves the transform to a new position.

Parameters

  • x (number|Vector3): The x coordinate or a Vector3 position.
  • y (number): The y coordinate (if x is a number).
  • z (number): The z coordinate (if x is a number).

moveBy

moveBy(x, y, z)

Moves the transform by a given offset.

Parameters

  • x (number|Vector3): The x offset or a Vector3 offset.
  • y (number): The y offset (if x is a number).
  • z (number): The z offset (if x is a number).

lookAt

lookAt(x, y, z, up)

Rotates the transform to look at a target position.

Parameters

  • x (number|Vector3): The target x coordinate or a Vector3 target.
  • y (number|Vector3): The target y coordinate (if x is a number) or the up vector in case x is the look at Vector.
  • z (number): The target z coordinate (if x is a number).
  • up (Vector3): The up direction (default is Vector3(0,1,0)).

rotateTo

rotateTo(x, y, z)

Rotates the transform to a specific orientation. Angles are specified in degrees.

Parameters

  • x (number|Vector3): Rotation around the x-axis, or a Vector3 representing rotation.
  • y (number): Rotation around the y-axis (if x is a number).
  • z (number): Rotation around the z-axis (if x is a number).

rotateBy

rotateBy(x, y, z)

Rotates the transform by the given amount. Angles are specified in degrees.

Parameters

  • x (number|Vector3): Rotation to apply around the x-axis, or a Vector3 representing rotation.
  • y (number): Rotation to apply around the y-axis (if x is a number).
  • z (number): Rotation to apply around the z-axis (if x is a number).