TransformComponent

TransformComponent

Holds the transform of an entity. It also allows for a scene graph to be created,
in which transforms are inherited down the tree.

Constructor

new TransformComponent()

Members

children :Array.<TransformComponent>

Child TransformComponents in the "scene graph".

move

Adds to the translation in a local direction.

Example
// Move the spaceShip entity in its own forward direction
spaceShip.transformComponent.move(new Vector3(0, 0, -1));

parent :TransformComponent

Parent TransformComponent in the "scene graph".

sync

Update the local and world transforms of the entity tree above this component (and the component itself).

transform :Transform

The entity's transform in local space.

updateWorldTransform

Update component's world transform (resulting transform considering parent transformations).

(readonly) worldTransform :Transform

The entity's transform in world space.
TODO: add function to get up-to-dated worldTransform

Methods

addRotation(…args) → {TransformComponent}

Adds to this transform's rotation using Euler angles (in radians, Euler order YZX).

Parameters:
Name Type Attributes Description
args Vector | Array.<number> <repeatable>

Component values.

Example
entity.transformComponent.addRotation(Math.PI / 4, 0, 0);
entity.transformComponent.addRotation(new Vector3(Math.PI / 4, 0, 0));
entity.transformComponent.addRotation([Math.PI / 4, 0, 0]);

addTranslation(…args) → {TransformComponent}

Adds to this transform's local translation.

Parameters:
Name Type Attributes Description
args Vector | Array.<number> <repeatable>

Component values.

Example
entity.transformComponent.addTranslation(1, 2, 1);
entity.transformComponent.addTranslation(new Vector3(1, 2, 1));
entity.transformComponent.addTranslation([1, 2, 1]);

attachChild(childComponent, keepTransformopt)

Attach a child transform to this component tree

Injected into entity when adding component.

Parameters:
Name Type Attributes Default Description
childComponent TransformComponent

Child transform component to attach

keepTransform boolean <optional>
false

If enabled, the child's position, rotation and scale will appear unaffected

clone() → {TransformComponent}

Clone the this transform in the transformComponent and return it
NOTE: it does not clone children.

detachChild(childComponent, keepTransformopt)

Detach a child transform from this component tree.

Injected into entity when adding component.

Parameters:
Name Type Attributes Default Description
childComponent TransformComponent

child transform component to detach

keepTransform boolean <optional>
false

If enabled, the child's position, rotation and scale will appear unaffected

ensureCanReplaceComponent(replaceToEntity)

Validate if the the entity contains the component can be replaced.

Parameters:
Name Type Description
replaceToEntity Entity

getRotation(targetopt) → {Vector3}

Gets the local rotation in Euler angles (in radians, Euler order YZX).

Parameters:
Name Type Attributes Description
target Vector3 <optional>

Target vector for storage. If not provided, a new vector object will be created and
returned.

Example
var localRotation = entity.transformComponent.getRotation(); // warning: creates a new Vector3 object
var localRotation2 = new Vector3();
entity.transformComponent.getRotation(localRotation2); // stores the result without creating a new object

getRotationMatrix() → {Matrix3}

Returns the local rotation matrix. Do not modify the returned value, use .setRotationMatrix() instead.

Example
var matrix = entity.transformComponent.getRotationMatrix();

getScale() → {Vector3}

Get the local transform scale. Do not modify the returned value, use .setScale() instead.

Example
var scale = entity.transformComponent.getScale();

getTranslation() → {Vector3}

Returns the local translation vector. Do not modify the returned value, use .setTranslation() instead.

Example
var translation = entity.transformComponent.getTranslation();

getWorldRotationMatrix() → {Matrix3}

Returns the world rotation matrix. Do not modify the returned value, use .setRotationMatrix() instead.

Example
var worldRotation = entity.transformComponent.getWorldRotationMatrix();

getWorldScale() → {Vector3}

Get the world transform scale. Do not modify the returned value, use .setScale() instead.

Example
var scale = entity.transformComponent.getWorldScale();

getWorldTranslation() → {Vector3}

Returns the world translation vector. Do not modify the returned value, use .setTranslation() instead.

Example
var worldTranslation = entity.transformComponent.getWorldTranslation();

lookAt(position, upopt) → {TransformComponent}

Sets the transform to look in a specific direction in world space.

Injected into entity when adding component.

Parameters:
Name Type Attributes Default Description
position Vector3 | Entity

Target position in world space.

up Vector3 <optional>
(0, 1, 0)

Up vector in world space.

Example
// Omitted up vector assumes Y is up:
entity.transformComponent.lookAt(1, 2, 3);
entity.transformComponent.lookAt([1, 2, 3]);
entity.transformComponent.lookAt(new Vector3(1, 2, 3));
entity.transformComponent.lookAt(otherEntity);

// However, you can pass the up vector as well:
entity.transformComponent.lookAt([1, 2, 3], [0, 1, 0]);
entity.transformComponent.lookAt(new Vector3(1, 2, 3), new Vector3(0, 1, 0));
entity.transformComponent.lookAt(otherEntity, new Vector3(0, 1, 0));

setRotation(…args) → {TransformComponent}

Sets this transform's rotation around X, Y and Z axis (in radians, Euler order YZX). The rotation is applied in X,
Y, Z order.

Parameters:
Name Type Attributes Description
args Vector | Array.<number> <repeatable>

Component values.

Example
entity.transformComponent.setRotation(Math.PI / 4, 0, 0);
entity.transformComponent.setRotation(new Vector3(Math.PI / 4, 0, 0));
entity.transformComponent.setRotation([Math.PI / 4, 0, 0]);

setRotationMatrix() → {TransformComponent}

Sets the local rotation matrix.

Example
entity.transformComponent.setRotationMatrix(new Matrix3());

setScale(…args) → {TransformComponent}

Sets this transform local scale.

Parameters:
Name Type Attributes Description
args Vector | Array.<number> <repeatable>

Component values.

Example
entity.transformComponent.setScale(1, 1, 0);
entity.transformComponent.setScale(new Vector3(1, 1, 0));
entity.transformComponent.setScale([1, 1, 0]);

setTranslation(…args) → {TransformComponent}

Set the local translation vector.

Parameters:
Name Type Attributes Description
args Vector | Array.<number> <repeatable>

translation

Example
entity.transformComponent.setTranslation(1, 1, 0);
entity.transformComponent.setTranslation(new Vector3(1, 1, 0));
entity.transformComponent.setTranslation([1, 1, 0]);

setUpdated()

Mark the component for updates of world transform. Needs to be called after manually changing the transform
without using helper functions.

updateTransform()

Update component's transform.