Box.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "core/Box.h"
  2. #include <stdio.h>
  3. CoreBox* coreSetBox(CoreBox* box, const CoreVector3* size) {
  4. for(size_t i = 0; i < 3; i++) {
  5. if(size->data[i] < 0.0f) {
  6. box->min.data[i] = size->data[i];
  7. box->max.data[i] = 0.0f;
  8. } else {
  9. box->min.data[i] = 0.0f;
  10. box->max.data[i] = size->data[i];
  11. }
  12. }
  13. return box;
  14. }
  15. CoreBox* coreOffsetBox(CoreBox* box, const CoreVector3* offset) {
  16. coreAddSetV3(&box->min, offset);
  17. coreAddSetV3(&box->max, offset);
  18. return box;
  19. }
  20. bool coreCollidesWithBox(const CoreBox* box, const CoreBox* other) {
  21. return box->max.data[0] > other->min.data[0] &&
  22. box->min.data[0] < other->max.data[0] &&
  23. box->max.data[1] > other->min.data[1] &&
  24. box->min.data[1] < other->max.data[1] &&
  25. box->max.data[2] > other->min.data[2] &&
  26. box->min.data[2] < other->max.data[2];
  27. }
  28. CoreBox* coreExpandBox(CoreBox* box, const CoreVector3* offset) {
  29. for(size_t i = 0; i < 3; i++) {
  30. if(offset->data[i] > 0.0f) {
  31. box->max.data[i] += offset->data[i];
  32. } else {
  33. box->min.data[i] += offset->data[i];
  34. }
  35. }
  36. return box;
  37. }
  38. CoreBox* coreGrowBox(CoreBox* box, const CoreVector3* growth) {
  39. CoreVector3 half = {0};
  40. coreMulV3F(&half, growth, 0.5f);
  41. CoreVector3 nMin = {0};
  42. coreSubV3(&nMin, &box->min, &half);
  43. CoreVector3 nMax = {0};
  44. coreAddV3(&nMax, &box->max, &half);
  45. for(size_t i = 0; i < 3; i++) {
  46. if(nMin.data[i] > nMax.data[i]) {
  47. nMin.data[i] = (box->min.data[i] + box->max.data[i]) * 0.5f;
  48. nMax.data[i] = nMin.data[i];
  49. }
  50. }
  51. box->min = nMin;
  52. box->max = nMax;
  53. return box;
  54. }
  55. size_t coreToStringBox(const CoreBox* box, char* buffer, size_t n) {
  56. int w = snprintf(buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
  57. (double)box->min.data[0], (double)box->min.data[1],
  58. (double)box->min.data[2], (double)box->min.data[3],
  59. (double)box->min.data[4], (double)box->min.data[5]);
  60. return w < 0 ? 0 : (size_t)w;
  61. }