Skip to main content

BoxCollider2D

The BoxCollider2D class is part of the zygarde::physics::components namespace and is designed to represent a 2D box-shaped collider within the physics engine. It provides methods to manage collision detection and response for rectangular objects.

Overview​

BoxCollider2D is a final class that allows the definition and manipulation of a 2D box collider. It holds data about the size of the collider and supports specifying collision and inclusion layers to manage how collisions are detected with other objects.

Properties​

  • size: A Vector2f representing the width and height of the box collider.
  • collision_layers: A vector of integers specifying the collision layers the box collider is considered part of.
  • include_layers: A vector of integers specifying the inclusion layers for the collider, these are the layers the collider will check against.

Example Usage​

#include "box_collider_2_d.hpp"

using namespace zygarde::physics::components;

core::types::Vector2f boxSize(50.0f, 100.0f);
std::vector<int> collisionLayers = {1, 2};
std::vector<int> includeLayers = {1};

// Creating a BoxCollider2D with size and collision layers
BoxCollider2D collider(boxSize, collisionLayers, includeLayers);

// Set a new size
collider.SetSize(core::types::Vector2f(60.0f, 120.0f));

//> For the collider to be filled with collisions it needs to run through the collision system

// Check for collisions
if (collider.HasCollision()) {
auto collision = collider.GetNextCollision();
// Process collision
}