Search⌘ K
AI Features

Collision Detection

Understand how to implement collision detection in a Rust-based dungeon crawler using ECS architecture. Explore querying player and enemy positions, using command buffers to remove entities after collisions, and integrating this system into the game scheduler to enhance modularity and code reuse.

Removing monsters after collision

Let’s remove the monsters after the player hits them by adding collision detection. We’ll write a combat system in the Health and Melee Combat chapter. For now, walking into a monster will remove it from the dungeon.

Collision detection will be its own system. Adding a new file to the src/systems/collisions.rs, adding mod collisions; to the src/systems/mod.rs file.

Rust 1.40.0
#[system]
#[read_component(Point)]
#[read_component(Player)]
#[read_component(Enemy)]
pub fn collisions(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
  • This system requires access to Point, Player, and Enemy. ...