Skip to main content

Chapter 7a - Areas and layers

Area2D vs bodies​

NodeCollides / blocks?Typical use
CharacterBody2D / RigidBody2DSolid (in real physics)Walking, falling crates
Area2DDetection onlyPickups, hurtboxes, triggers

In the kit, Area2DMock (Kit.make_area) checks circle overlap. Detection is one-way like Godot: the area’s collision_mask must include the body’s collision_layer (the body does not need to β€œsee” the area).

var Kit = preload("res://game_kit.gd")
var hero = Kit.make_character(Vector2(0, 0))
var coin = Kit.make_area(Vector2(10, 0), 8.0)
print(coin.overlaps_body(hero))
πŸ§ͺ Overlap demo

Collision layers and masks​

Godot uses bitmasks:

  • collision_layer β€” which layers this object lives on
  • collision_mask β€” which layers it looks for

A solid body pair interacts only if both see each other:

(A.mask & B.layer) != 0 AND (B.mask & A.layer) != 0

An Area2D detecting a body only needs:

(area.mask & body.layer) != 0

Kit helpers: area.overlaps_body(body), body.can_collide_with(other), Kit.bodies_overlap(a, b).

πŸ§ͺ Layer mismatch

Mini quiz β€” areas​

Quiz

Areas and layers

1 / 3

Area2D is best for

Pick an answer to see if you are right.

Challenge: claim the coin​

Hero at (0,0) with radius = 8. Coin area at (12, 0) with radius 8. Print true if the coin overlaps the hero.

true

Exercises​

Activity: Too far

Hero at origin (radius 8). Area at (100, 0) radius 8. Print whether they overlap (false).

false