Physics

Physics World

The Physics class acts as a central manager that keeps track of all the objects in your game that can interact physically. These objects, represented by the Collider class, are registered and unregistered.

When you want to know if a specific object (collider) is colliding with anything else in the game, the Overlap function within the physics world does the heavy lifting. It iterates through all the other registered Collider objects and checks if they collide with the given collider. The physics world then returns a list of all the Collider objects it found overlapping.

engine/physics.h
#pragma once

#include <unordered_set>
#include <vector>

#include "collider.h"

namespace ng {

/// @brief Manages the physics simulation within a scene, primarily handling collision detection.
class Physics {
  // Collider needs to be able to call AddCollider, and RemoveCollider.
  friend class Collider;

 public:
  /// @brief Checks if a given collider overlaps with any other collider currently in the physics world.
  /// @param collider The Collider to check for overlaps.
  /// @return A vector of pointers to the Colliders that overlaps with the given collider, empty if no overlap is found.
  [[nodiscard]] std::vector<const Collider*> Overlap(
      const Collider& collider) const;

 private:
  /// @brief Adds a collider to the physics world for collision detection. Called by Collider during its addition to a scene.
  /// @param collider A pointer to the Collider to add. This pointer must not be null and the Collider's lifetime should be managed externally to this class.
  void AddCollider(const Collider* collider);

  /// @brief Removes a collider from the physics world. Called by Collider during its removal from a scene.
  /// @param collider A pointer to the Collider to remove. This pointer must not be null and the Collider's lifetime should be managed externally to this class.
  void RemoveCollider(const Collider* collider);

  // A set containing pointers to all colliders in the physics world. The Physics class does not own these pointers.
  std::unordered_set<const Collider*> colliders_;
};

}  // namespace ng

Last updated