Skip to main content

Introduction

Godot provides a comprehensive 2D engine for creating games and applications. All 2D nodes inherit from Node2D, which provides a transform system for position, rotation, scale, and skew.

The 2D Coordinate System

Godot uses a right-handed 2D coordinate system:
  • X-axis: Points to the right (positive values)
  • Y-axis: Points downward (positive values)
  • Origin: Top-left corner at (0, 0)
Unlike some engines where Y points upward, Godot’s Y-axis points down. This matches screen coordinate conventions.

Node2D Base Class

The Node2D class is the foundation for all 2D objects. It provides transformation properties and methods for positioning and orienting nodes in 2D space.

Transform Properties

PropertyTypeDescription
positionVector2Position relative to parent node
rotationfloatRotation in radians
rotation_degreesfloatRotation in degrees (helper property)
scaleVector2Scale relative to parent
skewfloatSkew transformation in radians

Global Transform Properties

Every transform property has a global equivalent:
  • global_position
  • global_rotation / global_rotation_degrees
  • global_scale
  • global_skew
  • global_transform

Basic Movement

extends Node2D

func _ready():
    # Set position
    position = Vector2(100, 200)
    
    # Rotate 45 degrees
    rotation_degrees = 45
    
    # Scale to double size
    scale = Vector2(2.0, 2.0)

func _process(delta):
    # Move right at 100 pixels per second
    position.x += 100 * delta
    
    # Rotate continuously
    rotate(delta * 2.0)

Transform Methods

# Move in local coordinates
translate(Vector2(10, 0))  # Move 10 pixels right in local space

Coordinate Conversion

Convert between local and global coordinates:
# Convert global position to local
var local_pos = to_local(global_position)

# Convert local position to global
var global_pos = to_global(Vector2(10, 10))

# Get angle to a point
var angle = get_angle_to(target.global_position)

Z-Index and Draw Order

The z_index property (inherited from CanvasItem) controls draw order:
# Draw this node above others
z_index = 10

# Draw behind
z_index = -5
Nodes with higher z_index values are drawn on top. Within the same z_index, nodes are drawn in tree order.

Y-Sorting

For top-down games, enable Y-sorting to automatically order nodes by their Y position:
# Enable Y-sorting on parent node
y_sort_enabled = true

2D Cameras

The Camera2D node controls what portion of the 2D scene is visible.

Basic Camera Setup

extends Camera2D

func _ready():
    # Make this the active camera
    enabled = true
    
    # Set zoom (2.0 = zoomed in 2x)
    zoom = Vector2(2.0, 2.0)

Camera Following

Attach a Camera2D as a child of the player node to follow them:
Player (CharacterBody2D)
└── Camera2D

Camera Limits

Constrain camera movement to level boundaries:
extends Camera2D

func _ready():
    limit_left = 0
    limit_top = 0
    limit_right = 1920
    limit_bottom = 1080

Camera Smoothing

Add smooth camera movement:
position_smoothing_enabled = true
position_smoothing_speed = 5.0

# Rotation smoothing
rotation_smoothing_enabled = true
rotation_smoothing_speed = 5.0

Drag Margins

Create a dead-zone where the player can move without moving the camera:
drag_horizontal_enabled = true
drag_vertical_enabled = true

drag_left_margin = 0.2
drag_right_margin = 0.2
drag_top_margin = 0.2
drag_bottom_margin = 0.2

Camera Shake

extends Camera2D

var shake_amount = 0.0
var shake_decay = 5.0

func _process(delta):
    if shake_amount > 0:
        offset = Vector2(
            randf_range(-shake_amount, shake_amount),
            randf_range(-shake_amount, shake_amount)
        )
        shake_amount = max(shake_amount - shake_decay * delta, 0)
    else:
        offset = Vector2.ZERO

func shake(amount: float):
    shake_amount = amount

2D Lighting

Godot provides 2D lighting nodes for creating dynamic lighting effects in 2D games.

Light Types

PointLight2D - Emits light in all directions from a point:
extends PointLight2D

func _ready():
    energy = 1.0  # Light intensity
    texture_scale = 2.0  # Size of light
    color = Color.ORANGE  # Light color
    shadow_enabled = true
DirectionalLight2D - Directional light source (like sunlight):
extends DirectionalLight2D

func _ready():
    energy = 1.0
    height = 0.5  # Pseudo-3D height effect

Light Modes

Lights can use different blend modes:
  • Add - Adds light (default)
  • Sub - Subtracts light (creates shadows)
  • Mix - Mixes with existing lighting

Shadows

Enable shadows with the shadow_enabled property. Configure shadow properties:
shadow_enabled = true
shadow_filter = Light2D.SHADOW_FILTER_PCF5  # Smooth shadows
shadow_filter_smooth = 2.0
Use LightOccluder2D nodes with appropriate shapes to create shadows from sprites and world geometry.

Transform Hierarchy

Transforms are hierarchical. A child node’s transform is relative to its parent:
# Parent at (100, 100)
parent.position = Vector2(100, 100)

# Child at (50, 50) relative to parent
child.position = Vector2(50, 50)

# Child's global position is (150, 150)
print(child.global_position)  # Vector2(150, 150)

Best Practices

When moving nodes relative to their parent, use local transform properties like position and rotate().
For world-space positioning (like spawning projectiles), use global_position.
Attach the camera to the player for simple following. Use a separate camera node with scripting for more complex behavior.
Remember that negative Y values move upward: velocity.y = -500 for jumping.

Next Steps

Sprites and Textures

Learn how to display and animate sprites

Physics

Add physics simulation to your 2D game

TileMaps

Create tile-based levels and environments

Canvas Layers

Organize UI and parallax backgrounds

Build docs developers (and LLMs) love