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¶
- class Transform¶
- module:
- position: Vector3¶
The position of the transform.
- rotation: Quaternion¶
The rotation of the transform.
- scale: Vector3¶
The scale of the transform.
- static new()¶
Creates a new transform object.
- Returns:
The new Transform object.
- Return type:
- 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:
The new Transform object.
- Return type:
- static fromJson(json)¶
Creates a transform object from JSON.
- Parameters:
json (
str) – The JSON string.- Returns:
The new Transform object.
- Return type:
- static fromData(data)¶
Creates a transform object from data.
- Parameters:
data (
table) – The data object containing position, rotation, and scale.- Returns:
The new Transform object.
- Return type:
- toData()¶
Converts the transform into a table representation.
- Returns:
The data table.
- Return type:
table
- clone()¶
Clones the transform
- Returns:
The cloned transform
- Return type:
table
- moveTo(x, y, z)¶
Moves the transform to a new position.
- Parameters:
x (
number or 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(x, y, z)¶
Moves the transform by a given offset.
- Parameters:
x (
number or 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(x, y, z, up)¶
Rotates the transform to look at a target position.
- Parameters:
x (
number or Vector3) – The target x coordinate or a Vector3 target.y (
number or 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(x, y, z)¶
Rotates the transform to a specific orientation.
Angles are specified in degrees.
- Parameters:
x (
number or 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(x, y, z)¶
Rotates the transform by the given amount.
Angles are specified in degrees.
- Parameters:
x (
number or 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).