Box.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "core/Box.h"
  2. #include "core/Generic.h"
  3. #include "core/ToString.h"
  4. void setBox(Box* box, const Vector3* size) {
  5. for(size_t i = 0; i < 3; i++) {
  6. if(size->data[i] < 0.0f) {
  7. box->min.data[i] = size->data[i];
  8. box->max.data[i] = 0.0f;
  9. } else {
  10. box->min.data[i] = 0.0f;
  11. box->max.data[i] = size->data[i];
  12. }
  13. }
  14. }
  15. void offsetBox(Box* box, const Vector3* offset) {
  16. addSet(&box->min, offset);
  17. addSet(&box->max, offset);
  18. }
  19. bool collidesWithBox(const Box* box, const Box* other) {
  20. return box->max.x > other->min.x && box->min.x < other->max.x &&
  21. box->max.y > other->min.y && box->min.y < other->max.y &&
  22. box->max.z > other->min.z && box->min.z < other->max.z;
  23. }
  24. void expandBox(Box* box, const Vector3* offset) {
  25. for(size_t i = 0; i < 3; i++) {
  26. box->v[offset->data[i] > 0.0f].data[i] += offset->data[i];
  27. }
  28. }
  29. void growBox(Box* box, const Vector3* growth) {
  30. Vector3 half;
  31. mul(&half, growth, 0.5f);
  32. Vector3 nMin;
  33. sub(&nMin, &box->min, &half);
  34. Vector3 nMax;
  35. add(&nMax, &box->max, &half);
  36. for(size_t i = 0; i < 3; i++) {
  37. if(nMin.data[i] > nMax.data[i]) {
  38. nMin.data[i] = (box->min.data[i] + box->max.data[i]) * 0.5f;
  39. nMax.data[i] = nMin.data[i];
  40. }
  41. }
  42. box->min = nMin;
  43. box->max = nMax;
  44. }
  45. size_t toStringBox(const Box* box, char* buffer, size_t n) {
  46. return toString(
  47. buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
  48. (double)box->min.x, (double)box->min.y, (double)box->min.z,
  49. (double)box->max.x, (double)box->max.y, (double)box->max.z);
  50. }