#48 - Simple Bounding Box Class C++

Date: 2019-02-16 12:00 - C++

A simple axis-aligned bounding box class in C++.

#pragma once

#include <glm/glm.hpp>

namespace YourNamespace {
	class AABoundingBox {
		glm::vec3 _min;
		glm::vec3 _max;
	public:
		AABoundingBox(glm::vec3 min, glm::vec3 max) : _min(min), _max(max) { }
		const glm::vec3& min() const { return _min; }
		const glm::vec3& max() const { return _max; }
		glm::vec3 halfSize() const { return (_max - _min) / 2.0f; }
		glm::vec3 center() const { return (_min + _max) / 2.0f; }
		bool collides(const AABoundingBox& other) {
			if (_max.x < other._min.x ||  _max.y < other._min.y || _min.x > other._max.x || _min.y > other._max.y)
                return false;
			return true;
		}

		bool contains(const AABoundingBox& other) {
			return _max.x >= other._max.x && _min.x <= other._min.x &&
				_max.y >= other._max.y && _min.y <= other._min.y;
		}
	};
}

Previous snippet | Next snippet