Skip to main content

CollisionSystem

The CollisionSystem class in the zygarde::physics::systems namespace is a core part of the 2D physics engine. This system manages collision detection and resolution for entities that have the Rigidbody2D, Position, and BoxCollider2D components, ensuring that objects interact according to physical constraints.

Overview​

CollisionSystem is responsible for identifying and managing collisions between entities by running a series of checks and resolving collisions. This process is essential for creating realistic physics-based interactions in 2D space. It integrates with components such as:

  • Rigidbody2D: Represents the physical body and movement properties of an entity.
  • Position: Manages the entity’s position in the world.
  • BoxCollider2D: Defines the collider, or bounding area, for detecting collisions.

The CollisionSystem uses these components to:

  • Detect overlapping bounding boxes between entities.
  • Check if entities are on compatible collision layers.
  • Resolve collisions by adjusting positions and adding collision events to colliders.

Key Concepts​

The CollisionSystem employs several key techniques and principles:

ConceptDescription
Layer-Based FilteringEntities are assigned collision and include layers, which determine which objects they can interact with. This provides control over what types of entities can collide with each other.
Bounding Box OverlapThe BoxCollider2D component defines a rectangular area around an entity for collision detection. When bounding boxes overlap, it signals a potential collision.
Movement OffsetThe Rigidbody2D's movement offset allows CollisionSystem to simulate an entity's next position in advance, enabling predictive collision checks before movement occurs.
Collision ResolutionOnce a collision is detected, the system either stops movement or queues the collision within the collider, allowing other systems to handle it appropriately.

Core Functionality​

The main method in CollisionSystem, Run, iterates over all entities with the required components and checks each entity for potential collisions with others. If a collision is detected, it is resolved according to layer compatibility and kinematic status.

void Run(std::shared_ptr<Registry> r,
zipper<components::Rigidbody2D, core::components::Position, components::BoxCollider2D> components) override;

Collision Detection Process​

The CollisionSystem performs a few key steps in each run:

  1. Layer Matching: Using HasMatchingCollisionLayers, it checks if two colliders are compatible based on their collision layers.
  2. Simulated Position Calculation: Calculates the projected position of each entity using GetSimulatedPosition, which adds the movement offset to the current position.
  3. Bounding Box Overlap Check: Creates bounding boxes for both entities and checks for overlap using CheckBoundingBoxOverlap.
  4. Collision Handling: If a collision is detected, it is either resolved immediately or stored for later processing, depending on whether the colliding entities are kinematic.

Bounding Box Overlap​

Bounding boxes are created based on the entities' size (from BoxCollider2D) and projected position (calculated with GetSimulatedPosition). The CheckBoundingBoxOverlap method compares each side of the bounding boxes to determine if they overlap on both x and y axes.