Transform

The transform component provides an entity with a rotation, position and uniform scale in 3D space.

All entities have a transform component; they cannot be detached from entities.

Properties

The transform component properties that are accessible through the inspector.

Rotation

The local rotation of the entity. It is represented internally as a quaternion but displayed in the inspector using XYZ Euler angles.

Position

The local position of the entity.

Scale

The local uniform scale of the entity.

Coordinate System

The transform represents a coordinate system. Any children of the entity have rotation, position and scale in the coordinate system of the parent. Scaling the parent will affect the world scale of all descendant entities in the hierarchy. ProtoTwin does not support non-uniform scaling for transforms. This was a deliberate decision, since non-uniform scaling combined with rotation causes skewing. Non-uniform scaling is supported on the graphics mesh through a property on the graphics component.

Scripting

Properties

You can set the rotation, position and scale of the entity in world-space using the properties worldRotation, worldPosition and worldScale respectively. Similarly, the properties rotation, position and scale can be used to set the rotation, position and scale of the entity local to the coordinate system of the parent. Making the distinction of whether to set the local-space position or world-space worldPosition is critical when the entity has a parent. If the entity doesn’t have a parent, then setting the position or worldPosition are equivalent, since the entity is local to the world.

Methods

The methods provided below can be used to transform objects between local space and world space coordinate systems.

  • The method localToWorldPoint() can be used to transform a point from local-space to world-space.
  • The method worldToLocalPoint() can be used to transform a point from world-space to local-space.
  • The method localToWorldRotation() can be used to transform a unit quaternion from local-space to world-space.
  • The method worldToLocalRotation() can be used to transform a unit quaternion from world-space to local-space.
  • The method localToWorldAxis() can be used to transform a unit direction vector from local-space to world-space.
  • The method worldToLocalAxis() can be used to transform a unit direction vector from world-space to local-space.

Example

The scripted component provided below demonstrates how to set the local rotation of the entity to a unit quaternion that rotates 45 degrees about the x-axis and the world position of the entity to (0, 1, 5) at initialization.

import { type Entity, Component, Quat, Vec3 } from "prototwin";

export class Test extends Component {
    constructor(entity: Entity) {
        super(entity);
    }

    public override initialize(): void {
        this.entity.rotation = Quat.x(Math.PI / 4);
        this.entity.worldPosition = new Vec3(0, 1, 5);
    }
}