{"schemaVersion":1,"record":{"key":"lesson/collision-basics","type":"lesson","id":"collision-basics","lang":"en","title":"Collision fundamentals","summary":"Separate overlap detection, physical response, and gameplay events; check boundaries, fast objects, and repeated damage.","updatedAt":"2026-10-20","status":"active","url":"https://gamemechanics.pro/en/lessons/collision-basics/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lesson/collision-basics.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lesson/collision-basics.md","aliases":["столкновения","коллизии","collider","collision"],"tags":["collision","physics","testing"],"sourceIds":["source-7c0748c4a86f7409","source-8cc54cd9f9a5c8e8"],"sources":[{"id":"source-7c0748c4a86f7409","title":"MDN: 2D collision detection","url":"https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection","verifiedAt":null},{"id":"source-8cc54cd9f9a5c8e8","title":"Godot: Physics introduction","url":"https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html","verifiedAt":null}],"sections":[{"id":"prerequisites","title":"Prerequisites","body":"You need coordinates and basic conditions. Begin with two unrotated rectangles. The goal is to define exactly what contact means without confusing geometry with damage rules."},{"id":"principle","title":"The principle","body":"Detection asks whether shapes intersect. Collision resolution decides how to remove penetration and change movement. Gameplay response decides what contact means: damage, picking up an item, or opening a door. These tasks can share an event while following different rules.\n\nFor axis-aligned rectangles, compare their intervals on both axes. A larger scene can first select possible neighbors and then run more precise checks. [MDN: 2D collision detection](https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection) introduces these techniques.\n\nWhen using an engine, first learn its physics object types and interaction filters. The [Godot physics introduction](https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html), for example, distinguishes detection areas from physical bodies and explains collision layers and masks. Do not place custom movement on top of a physics solver without understanding their update order.\n\n<span id=\"example-a-hazard-area\"></span>"},{"id":"example","title":"Practical example","body":"### A hazard area\n\nAssume rectangle coordinates describe the top-left corner and dimensions are positive. For this exercise, touching edges without overlapping area does not count as a hit.\n\n```text\noverlap(a, b):\n    separatedX = a.right <= b.left or b.right <= a.left\n    separatedY = a.bottom <= b.top or b.bottom <= a.top\n    return not (separatedX or separatedY)\n```\n\nNow define the gameplay rule: entering a hazard causes one hit. Keep a set of pairs that overlapped during the previous step. For each current pair, emit an entry event only if that pair was absent before. After processing, replace the previous set with the current one. Identify pairs with stable object identifiers, not positions.\n\nThis deliberately differs from applying damage every frame. If the hazard needs periodic damage, give it a separate interval measured in seconds. If a projectile must hit only once during its entire lifetime, entry history is insufficient: mark it consumed after the first accepted hit."},{"id":"mistakes","title":"Common mistakes","body":"A visible sprite and its collision shape do not have to match exactly, but the difference should be intentional. Enable collision debugging and inspect local coordinates, scale, and anchor points.\n\nA fast object may cross a thin wall between two discrete checks. For that case, investigate the engine's continuous collision detection, a sweep over the traveled segment, or another method suited to your shapes. Making every wall thicker hides the symptom without explaining it.\n\nAvoid deleting items from a collection while iterating if doing so shifts the indices of objects that still need processing. Collect events, apply responses, and remove objects in a defined order. Clear stored contact information when an object is destroyed."},{"id":"checks","title":"Verify it","body":"- Separated, overlapping, and exactly touching shapes produce the intended outcomes.\n- A shape fully contained inside another is detected.\n- Entry causes one hit; exit and re-entry follow the written rule.\n- Deleting an object does not leave a stale contact.\n- The fastest projectile is checked against the thinnest supported wall.\n- Collision layers exclude pairs that should never interact."},{"id":"agent-task","title":"Prompt for an AI agent","body":"> Locate collision shapes and response rules. Document the edge-touch convention, event ordering, and damage frequency. Check containment, re-entry, deletion during contact, and a fast projectile. Use the selected engine's capabilities; do not introduce a custom physics engine for a single trigger."}],"relations":[{"type":"requires","direction":"outgoing","label":"Before you start","key":"lesson/game-loop","title":"The game loop and time","url":"https://gamemechanics.pro/en/lessons/game-loop/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lesson/game-loop.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lesson/game-loop.md"},{"type":"requires","direction":"incoming","label":"Continue learning","key":"lesson/level-blockout","title":"Blockout: test space before art","url":"https://gamemechanics.pro/en/lessons/level-blockout/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lesson/level-blockout.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lesson/level-blockout.md"},{"type":"requires","direction":"incoming","label":"Continue learning","key":"lesson/npc-pathfinding","title":"Pathfinding and following a route","url":"https://gamemechanics.pro/en/lessons/npc-pathfinding/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lesson/npc-pathfinding.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lesson/npc-pathfinding.md"},{"type":"explains","direction":"outgoing","label":"Apply the principle","key":"mechanic/point-and-click--family-20","title":"Physics, environmental properties and experiment","url":"https://gamemechanics.pro/en/knowledge/mechanic/point-and-click--family-20/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/mechanic/point-and-click--family-20.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/mechanic/point-and-click--family-20.md"},{"type":"contains","direction":"incoming","label":"Learning paths","key":"path/first-prototype","title":"Your first verifiable prototype","url":"https://gamemechanics.pro/en/learn/first-prototype/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/path/first-prototype.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/path/first-prototype.md"},{"type":"requires","direction":"incoming","label":"Continue learning","key":"path/npc-behavior","title":"NPC behavior","url":"https://gamemechanics.pro/en/learn/npc-behavior/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/path/npc-behavior.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/path/npc-behavior.md"},{"type":"uses","direction":"outgoing","label":"Sources in the selection","key":"resource/collision-2d","title":"MDN: 2D collision detection","url":"https://gamemechanics.pro/en/resources/#resource-collision-2d","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/resource/collision-2d.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/resource/collision-2d.md"},{"type":"uses","direction":"outgoing","label":"Sources in the selection","key":"resource/godot-physics","title":"Godot: physics introduction","url":"https://gamemechanics.pro/en/resources/#resource-godot-physics","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/resource/godot-physics.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/resource/godot-physics.md"}],"placements":[],"supersededBy":[],"attributes":{"category":"engineering","level":"intermediate","minutes":6},"endsAt":null,"provenance":"Original gamemechanics.pro material. Check illustrative examples in your own project.","license":"CC-BY-4.0","codeLicense":"MIT","attribution":"gamemechanics.pro contributors"}}