TExcelTAD
Home › Godot 4 with GDScript › Node3D, Transform3D & Basis

Node3D, Transform3D & Basis

Every 3D node (meshes, cameras, lights, bodies) extends Node3D, and every Node3D has a transform: where it is, how it is rotated and how it is scaled, relative to its parent. In 2D that was Transform2D; in 3D it is Transform3D, made of two parts:

Part Type Meaning
basis Basis (3x3 matrix) rotation and scale: three column vectors x, y, z, the node's local axes expressed in parent space
origin Vector3 position in parent space

The friendly properties position, rotation (Euler angles in radians), rotation_degrees, scale and quaternion are views of that same transform. global_position, global_basis and global_transform give the same data in world space.

Godot 3D is right-handed and Y-up, and forward is −Z: a camera or a CharacterBody3D "looks" along its negative z axis. Vector3.FORWARD is (0, 0, -1). Imported models usually face +Z instead, which is why Godot 4.3+ also has Vector3.MODEL_FRONT ((0, 0, 1)). Most "my character walks backwards" bugs come from mixing those two. See Math for Games for the general theory of spaces and handedness.

Basis: three axes

A Basis is easiest to read column by column. The identity basis has x = (1,0,0), y = (0,1,0), z = (0,0,1). Rotating 90° around Y turns the axes, and multiplying a vector by the basis rotates the vector:

gdscript · try it yourself
extends SceneTree

func _init():
	var b := Basis()
	print("identity x=", b.x, " y=", b.y, " z=", b.z)
	var yaw := Basis(Vector3.UP, deg_to_rad(90))
	print("forward after 90 deg yaw: ", (yaw * Vector3.FORWARD).snappedf(0.001))
	print("x column: ", yaw.x.snappedf(0.001), " z column: ", yaw.z.snappedf(0.001))
	var t := Transform3D(yaw, Vector3(10, 0, 0))
	var local_point := Vector3(0, 0, -2)
	var world := t * local_point
	print("local ", local_point, " -> world ", world.snappedf(0.001))
	print("back to local: ", (t.affine_inverse() * world).snappedf(0.001))
	var s := Basis.from_scale(Vector3(2, 2, 2))
	print("scale: ", (yaw * s).get_scale().snappedf(0.001))
	quit()
identity x=(1.0, 0.0, 0.0) y=(0.0, 1.0, 0.0) z=(0.0, 0.0, 1.0)
forward after 90 deg yaw: (-1.0, 0.0, 0.0)
x column: (0.0, 0.0, -1.0) z column: (1.0, 0.0, 0.0)
local (0.0, 0.0, -2.0) -> world (8.0, 0.0, 0.0)
back to local: (0.0, 0.0, -2.0)
scale: (2.0, 2.0, 2.0)

What happened:

  • A positive rotation around UP is counter-clockwise seen from above, so "forward" (−Z) turned into −X (left).
  • Transform3D * Vector3 first rotates/scales with the basis, then adds the origin: a point 2 m in front of the node ends up at (8, 0, 0) in world space.
  • affine_inverse() goes back from world to local. Use inverse() only when there is no scale; affine_inverse() is always safe.
  • snappedf(0.001) hides float noise such as -0.0 or 4.37e-08. Print it this way whenever you print rotated vectors.

Tip: The local axes are your best friends in gameplay code. -global_basis.z is "where I am facing", global_basis.x is "my right", global_basis.y is "my up". No trigonometry needed.

Hierarchies, look_at and quaternions

A child's transform is relative to its parent, so moving or rotating a ship carries its turret along. look_at() rotates a node so its −Z axis points at a target. To blend rotations smoothly, use quaternions and slerp; interpolating Euler angles takes odd paths and suffers gimbal lock.

gdscript · try it yourself
extends Node3D

# frames: 0
func _ready():
	var ship := Node3D.new()
	ship.name = "Ship"
	add_child(ship)
	var turret := Node3D.new()
	turret.name = "Turret"
	turret.position = Vector3(0, 1, -2)
	ship.add_child(turret)
	ship.position = Vector3(5, 0, 0)
	ship.rotate_y(deg_to_rad(90))
	print("turret local ", turret.position, " global ", turret.global_position.snappedf(0.001))
	print("ship forward (-basis.z): ", (-ship.global_basis.z).snappedf(0.001))
	var enemy := Vector3(5, 0, 10)
	turret.look_at(enemy)
	print("turret now faces ", (-turret.global_basis.z).snappedf(0.001))
	var a := Quaternion(Vector3.UP, 0.0)
	var b := Quaternion(Vector3.UP, deg_to_rad(90))
	for w in [0.0, 0.5, 1.0]:
		var q := a.slerp(b, w)
		print("slerp ", w, ": ", snappedf(rad_to_deg(q.get_euler().y), 0.01), " deg")
	var t := Transform3D.IDENTITY.looking_at(Vector3(3, 0, -3), Vector3.UP)
	print("looking_at yaw ", snappedf(rad_to_deg(t.basis.get_euler().y), 0.01))
turret local (0.0, 1.0, -2.0) global (3.0, 1.0, 0.0)
ship forward (-basis.z): (-1.0, 0.0, 0.0)
turret now faces (0.195, -0.098, 0.976)
slerp 0.0: 0.0 deg
slerp 0.5: 45.0 deg
slerp 1.0: 90.0 deg
looking_at yaw -45.0

The turret's local position never changed; only its global position did. look_at needs the node to be inside the tree (it uses the global transform), and it fails with an error if the target is straight up along the up vector. Transform3D.looking_at() is the pure-math version that returns a new transform instead of changing a node.

Common operations

gdscript
# Move in the direction the node faces (units per second)
global_position += -global_basis.z * speed * delta

# Turn smoothly toward a target, frame-rate independent
var target_basis := global_transform.looking_at(target_pos, Vector3.UP).basis
global_basis = global_basis.slerp(target_basis, 1.0 - exp(-turn_speed * delta)).orthonormalized()

# Yaw only (keep the character upright): rotate around world Y
rotate_y(deg_to_rad(-mouse_dx * 0.2))

# Convert between spaces
var p_world := to_global(Vector3(0, 0, -1))   # a point 1 m in front of me
var p_local := to_local(enemy.global_position)
  • Basis.slerp needs orthonormal bases; call orthonormalized() on the result to stop scale creeping in from rounding errors after thousands of frames.
  • rotation uses Euler angles in YXZ order by default (rotation_order). Fine for setting a yaw/pitch camera; avoid it for blending.
  • Don't scale physics bodies or their collision shapes non-uniformly; resize the shape resource instead (see CharacterBody3D & 3D Physics).

Test yourself

Which direction is "forward" for a Godot Camera3D or Node3D?

  1. +Z
  2. −Z (-basis.z)
  3. +X
  4. +Y

What does transform * Vector3(0, 0, -2) compute?

  1. The world position of a point 2 m in front of the node (basis applied, then origin added)
  2. The node's scale
  3. Only the rotation, without the origin
  4. The inverse transform

Why blend rotations with Quaternion.slerp (or Basis.slerp) instead of lerping Euler angles?

  1. Euler lerps are slower to type
  2. Slerp follows the shortest arc at constant speed and avoids gimbal lock; Euler interpolation can take strange paths
  3. Euler angles don't exist in Godot
  4. Slerp also moves the position