CollisionBox.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. #include "common/utils/CollisionBox.h"
  2. CollisionBox::CollisionBox(const Vector3& min, const Vector3& max)
  3. : min(min), max(max) {
  4. }
  5. CollisionBox::CollisionBox(const Vector3& size) : max(size) {
  6. }
  7. CollisionBox CollisionBox::offset(const Vector3& offset) const {
  8. return CollisionBox(offset + min, offset + max);
  9. }
  10. bool CollisionBox::collidesWith(const CollisionBox& other) const {
  11. return max[0] > other.min[0] && min[0] < other.max[0] &&
  12. max[1] > other.min[1] && min[1] < other.max[1] &&
  13. max[2] > other.min[2] && min[2] < other.max[2];
  14. }
  15. CollisionBox CollisionBox::expand(const Vector3& offset) const {
  16. Vector3 add(offset[0] * (offset[0] >= 0.0), offset[1] * (offset[1] >= 0.0),
  17. offset[2] * (offset[2] >= 0.0));
  18. Vector3 sub(offset[0] * (offset[0] <= 0.0), offset[1] * (offset[1] <= 0.0),
  19. offset[2] * (offset[2] <= 0.0));
  20. return CollisionBox(min + sub, max + add);
  21. }
  22. const Vector3& CollisionBox::getMin() const {
  23. return min;
  24. }
  25. const Vector3& CollisionBox::getMax() const {
  26. return max;
  27. }