Chuyển đến nội dung

Bài 7 · Editor and Scenes

Nodes and 2D Transforms: Position, Rotation, Scale

Learn Godot's 2D coordinate system, Node2D position, rotation and scale, local versus global transforms, parent-child inheritance, Vector2 basics, pivots and common mistakes.

Before you can move a spaceship or place a button, you need to know how Godot describes where things are. In 2D, every Node2D has a transform: a position, a rotation and a scale. Children inherit their parent's transform, which is how a weapon follows a player and a wheel turns with a car. This lesson covers the coordinate system, Vector2, local and global values and the pitfalls that catch almost everyone once.

Godot is not available on this site: the GDScript is read-only. The Python stand-ins implement the same math, so you can check the numbers yourself.

The 2D coordinate system

Godot's 2D space has its origin (0, 0) at the top left. X grows to the right and Y grows downward. This is the usual screen convention, but different from school maths where Y points up. So a point at (100, 50) is 100 pixels right and 50 pixels down.

flowchart LR
    O["Origin (0,0) top-left"] -->|"+x right"| X["(100, 0)"]
    O -->|"+y down"| Y["(0, 50)"]

Rotation is measured in radians by the rotation property (and in degrees by rotation_degrees). A positive rotation turns clockwise on screen because Y points down.

Node2D properties

Node2D and everything built on it (Sprite2D, CharacterBody2D, Camera2D, ...) has:

Property Type Meaning
position Vector2 Offset from the parent's origin (local)
rotation float Radians (rotation_degrees for degrees)
scale Vector2 Size multiplier; (2, 1) doubles the width
global_position Vector2 Position in the world (read and write)
global_rotation float World rotation
skew float Shear
transform Transform2D All three combined in one value
GDSCRIPT
extends Node2D

func _ready() -> void:
    position = Vector2(200, 120)
    rotation_degrees = 45.0
    scale = Vector2(1.5, 1.5)
    print(global_position)     # same as position if the parent is at the origin

Vector2 in a nutshell

Vector2 holds x and y and is used for positions, velocities and directions.

GDSCRIPT
var a := Vector2(3, 4)
var b := Vector2(1, 0)

print(a + b)              # (4, 4)
print(a * 2.0)            # (6, 8)
print(a.length())         # 5.0
print(a.normalized())     # (0.6, 0.8): length 1, same direction
print(a.distance_to(b))   # distance between two points
print(a.angle())          # angle in radians from the x-axis
print(Vector2.RIGHT, Vector2.UP)   # (1, 0)  (0, -1)

Note Vector2.UP is (0, -1): up on screen means a negative y. Normalizing a direction before multiplying by speed prevents the diagonal-is-faster bug: moving (1, 1) has length about 1.41 instead of 1.

Local versus global

position is relative to the parent. Move a player at (300, 200) that has a child Gun at (40, 0): the gun's position stays (40, 0), but its global_position is (340, 200). Rotate the player 90 degrees and the gun's global_position swings to (300, 240), because the gun's offset rotates with its parent.

This is the whole point of the tree: you position the child relative to the parent and let the engine compose the transforms. Use global_position when you need to compare things in different branches, such as "how far is this enemy from the player?":

GDSCRIPT
var distance := global_position.distance_to(player.global_position)

Setting position on a child that you want at a world location is a common bug; set global_position instead.

Pivot and the origin of a sprite

A node rotates and scales around its own origin. A Sprite2D has centered = true by default, so the texture is centered on the origin and rotates around its middle. Turn centered off and the texture's top-left sits at the origin, so it spins around a corner. For a door hinge, a clock hand or a sword swing, you want the origin at the hinge: add a plain Node2D as the pivot and put the sprite as a child, offset away from the origin.

Moving with direction and speed

GDSCRIPT
extends Node2D

var speed := 200.0

func _process(delta: float) -> void:
    var direction := Vector2(1, 1).normalized()
    position += direction * speed * delta

To face a target: rotation = global_position.direction_to(target).angle() or look_at(target.global_position). To move forward in the direction you are facing, use transform.x (the node's local x-axis in the parent's space) or Vector2.RIGHT.rotated(rotation).

Stand-in: transforms in Python

Here is the math for a child inheriting its parent's position, rotation and scale. It is not Godot, but to_global is exactly what global_position computes.

Python Stand-in (Python): local to global

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first result is (340.0, 200.0). After rotating 90 degrees the gun is at (300.0, 240.0): with Y pointing down, a clockwise turn sends the gun down the screen.

Common mistakes

  • Treating Y as pointing up, so "move up" gets the wrong sign.
  • Mixing degrees and radians: rotation takes radians; rotation_degrees takes degrees.
  • Scaling a CollisionShape2D through scale (it can make physics behave strangely); resize its shape resource instead.
  • Not normalizing movement input, so diagonals are 41% faster.
  • Using position where global_position is needed (or the other way around).

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In Godot's 2D space, which way does +Y point?

    Y grows downward, so Vector2.UP is (0, -1).
  2. 2. What unit does the rotation property use?

    rotation is in radians; rotation_degrees is the degree version.
  3. 3. A child at local position (40, 0) has a parent at (300, 200) with no rotation. What is the child's global_position?

    With no rotation or scale, the global position is the parent's position plus the local offset.
  4. 4. Why call normalized() on a movement direction?

    A normalized vector has length 1, so multiplying by speed gives the same speed in every direction.

Summary

  • 2D origin is top-left; X right, Y down; rotation is in radians, clockwise positive.
  • Node2D has position, rotation, scale and global counterparts; children inherit the parent's transform.
  • Use Vector2 for positions and directions; normalize directions before applying speed.
  • Use global_position when comparing across branches.
  • A node rotates around its own origin, so use a pivot node for hinges.

Next: Instancing Scenes

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.