BoundingBox

Bounding box

The BoundingBox class is a template class that represents a box around an object (position and size). It is used to check collision between entities.

Template parameters

  • T: The type of the position and size of the box.

Public member functions

// Construct a new Bounding Box object
BoundingBox(T x_, T y_, T w_, T h_);
// Construct a new Bounding Box object
BoundingBox(const std::pair<T, T> &pos, const std::pair<T, T> &size);

// Compare two bounding boxes
bool operator==(const BoundingBox &other) const;
// Compare two bounding boxes
bool operator!=(const BoundingBox &other) const;

// Check if the bounding box collide with another bounding box
bool collide(const BoundingBox &other) const;
// Check if the bounding box collide with a point
bool collide(const std::pair<T, T> &pos) const;
// Check if the bounding box collide with a point
bool collide(T x_, T y_) const;
// Check if the bounding box collide with another bounding box
bool collide(const std::pair<T, T> &pos, const std::pair<T, T> &size) const;
// Check if the bounding box collide with another bounding box
bool collide(T x_, T y_, T w_, T h_) const;

Public attributes

// The position of the bounding box
T x;
T y;
// The size of the bounding box
T width;
T height;

Member functions documentation

Constructor

The constructor of the BoundingBox class.

BoundingBox(T x_, T y_, T w_, T h_);
BoundingBox(const std::pair<T, T> &pos, const std::pair<T, T> &size);

Parameters

  • x_: The x position of the bounding box.

  • y_: The y position of the bounding box.

  • w_: The width of the bounding box.

  • h_: The height of the bounding box.

  • pos: A pair of T containing the position of the bounding box (x, y).

  • size: A pair of T containing the size of the bounding box (w, h).

operator==

This operator compares two bounding boxes.

bool operator==(const BoundingBox &other) const;

Parameters

  • other: A reference to the other bounding box to compare with.

Returns

A boolean which is true if the two bounding boxes are equal, false otherwise.

operator!=

This operator compares two bounding boxes.

bool operator!=(const BoundingBox &other) const;

Parameters

  • other: A reference to the other bounding box to compare with.

Returns

A boolean which is true if the two bounding boxes are not equal, false otherwise.

collide

This function checks if the bounding box collide with another bounding box or a point. There are 5 overloads of this function.

bool collide(const BoundingBox &other) const;
bool collide(const std::pair<T, T> &pos) const;
bool collide(T x_, T y_) const;
bool collide(const std::pair<T, T> &pos, const std::pair<T, T> &size) const;
bool collide(T x_, T y_, T w_, T h_) const;

Parameters

  • other: A reference to the other bounding box to check collision with.

  • x_: The x position of the point to check collision with.

  • y_: The y position of the point to check collision with.

  • w_: The width of the bounding box to check collision with.

  • h_: The height of the bounding box to check collision with.

  • pos: A pair of T containing the position of the point to check collision with (x, y).

  • size: A pair of T containing the size of the bounding box to check collision with (w, h).

Returns

A boolean which is true if the bounding box collide with the other bounding box or the point, false otherwise.

Attributes documentation

x

The x position of the bounding box.

y

The y position of the bounding box.

width

The width of the bounding box.

height

The height of the bounding box.