12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #include "math/Box.h"
- Box::Box(const Vector3& min_, const Vector3& max_) : min(min_), max(max_) {
- }
- Box::Box(const Vector3& size) {
- for(int i = 0; i < 3; i++) {
- if(size[i] < 0.0f) {
- min[i] = size[i];
- } else {
- max[i] = size[i];
- }
- }
- }
- Box Box::offset(const Vector3& offset) const {
- return Box(min + offset, max + offset);
- }
- bool Box::collidesWith(const Box& other) const {
- return max[0] > other.min[0] && min[0] < other.max[0] &&
- max[1] > other.min[1] && min[1] < other.max[1] &&
- max[2] > other.min[2] && min[2] < other.max[2];
- }
- Box Box::expand(const Vector3& offset) const {
- Vector3 add(offset[0] > 0.0f ? offset[0] : 0.0f,
- offset[1] > 0.0f ? offset[1] : 0.0f,
- offset[2] > 0.0f ? offset[2] : 0.0f);
- Vector3 sub(offset[0] < 0.0f ? offset[0] : 0.0f,
- offset[1] < 0.0f ? offset[1] : 0.0f,
- offset[2] < 0.0f ? offset[2] : 0.0f);
- return Box(min + sub, max + add);
- }
- Box Box::grow(const Vector3& growth) const {
- Vector3 half = growth * 0.5f;
- Vector3 nMin = min - half;
- Vector3 nMax = max + half;
- for(int i = 0; i < 3; i++) {
- if(nMin[i] > nMax[i]) {
- nMin[i] = (min[i] + max[i]) * 0.5f;
- nMax[i] = nMin[i];
- }
- }
- return Box(nMin, nMax);
- }
- const Vector3& Box::getMin() const {
- return min;
- }
- const Vector3& Box::getMax() const {
- return max;
- }
|