GameTransform
Transform component attached to GameObjects. Supports parent-child hierarchies with local and world coordinates.
using Xengine.Scene;
World vs Local Space
GameTransform distinguishes between world space (absolute) and local space (relative to parent):
- World: Position/rotation in the scene
- Local: Position/rotation relative to parent
Properties
| Property | Type | Description |
|---|---|---|
Position | Vector3 | World position |
Rotation | Quaternion | World rotation |
Scale | Vector3 | Local scale |
LocalPosition | Vector3 | Position relative to parent |
LocalRotation | Quaternion | Rotation relative to parent |
LocalScale | Vector3 | Scale (always local) |
Forward | Vector3 | Forward direction (world) |
Right | Vector3 | Right direction (world) |
Up | Vector3 | Up direction (world) |
EulerAngles | Vector3 | World rotation as euler |
LocalEulerAngles | Vector3 | Local rotation as euler |
Methods
Translate
void Translate(Vector3 translation, bool worldSpace = true)
// Move in world space
Transform.Translate(new Vector3(1, 0, 0));
// Move in local space (relative to rotation)
Transform.Translate(new Vector3(0, 0, 1), false); // Forward
Rotate
void Rotate(Vector3 eulerAngles, bool worldSpace = true)
// Rotate around world Y axis
Transform.Rotate(new Vector3(0, 45, 0));
// Rotate around local axis
Transform.Rotate(new Vector3(0, 45, 0), false);
LookAt
void LookAt(Vector3 target, Vector3? up = null)
Transform.LookAt(player.Transform.Position);
Transform.LookAt(target, Vector3.Up);
TransformPoint
Vector3 TransformPoint(Vector3 localPoint)
Convert local point to world space.
// Get world position of gun barrel (1 unit forward)
var barrelPos = Transform.TransformPoint(new Vector3(0, 0, 1));
InverseTransformPoint
Vector3 InverseTransformPoint(Vector3 worldPoint)
Convert world point to local space.
var localPos = Transform.InverseTransformPoint(worldPosition);
TransformDirection
Vector3 TransformDirection(Vector3 localDirection)
Convert local direction to world space (ignores position/scale).
var worldForward = Transform.TransformDirection(Vector3.Forward);
Parent-Child Hierarchy
// Set parent
child.Parent = parent;
// Child's local position is relative to parent
child.Transform.LocalPosition = new Vector3(2, 0, 0); // 2 units right of parent
// World position is calculated automatically
var worldPos = child.Transform.Position;
// Clear parent
child.Parent = null;
Common Patterns
Move Forward
// Move in direction object is facing
Transform.Position += Transform.Forward * speed * Time.Delta;
Orbit Around Point
float angle = Time.Now * orbitSpeed;
float radius = 5f;
Transform.Position = center + new Vector3(
MathF.Cos(angle) * radius,
0,
MathF.Sin(angle) * radius
);
Transform.LookAt(center);
Smooth Follow
var target = Scene.FindByName("Target");
Transform.Position = Vector3.Lerp(
Transform.Position,
target.Transform.Position,
5f * Time.Delta
);
Child Offset
// Create gun attached to player
var gun = scene.CreateGameObject("Gun");
gun.Parent = player;
gun.Transform.LocalPosition = new Vector3(0.5f, 0.2f, 0.8f);
// Gun moves with player automatically