bumpshape.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef bumpshape_H_INCLUDED
  2. #define bumpshape_H_INCLUDED
  3. /** Shapes and bounds boxes for the bump physics system.
  4. * These are all small structs intended to be used as value types.
  5. */
  6. #include <stdarg.h>
  7. #include "bevec.h"
  8. struct BumpAABB;
  9. typedef struct BumpAABB_ BumpAABB;
  10. typedef struct BumpCircle_ BumpCircle;
  11. typedef struct BumpSegment_ BumpSegment;
  12. typedef struct BumpShapeHeader_ BumpShapeHeader;
  13. typedef struct BumpBoxShape_ BumpBoxShape;
  14. typedef struct BumpCircleShape_ BumpCircleShape;
  15. typedef struct BumpSegmentShape_ BumpSegmentShape;
  16. typedef struct BumpErrorShape_ BumpErrorShape;
  17. typedef union BumpShape_ BumpShape;
  18. /* Axis Aligned bounds box. */
  19. struct BumpAABB_ { BeVec p; BeVec hs; };
  20. /* Circle. */
  21. struct BumpCircle_ { BeVec p; double radius; };
  22. /* Line segment. Segment stretches from p1 to p2 */
  23. struct BumpSegment_ { BeVec p1; BeVec p2; };
  24. /* Types of BumpShapes */
  25. enum BumpShapeType_ {
  26. BUMPSHAPE_BOX = 0,
  27. BUMPSHAPE_CIRCLE = 1,
  28. BUMPSHAPE_SEGMENT = 2,
  29. BUMPSHAPE_MAX = 3,
  30. BUMPSHAPE_ERROR = 69,
  31. };
  32. struct BumpShapeHeader_ {
  33. int type;
  34. };
  35. /* Shapes */
  36. struct BumpErrorShape_ {
  37. int type;
  38. int code;
  39. };
  40. /* Shapes */
  41. struct BumpBoxShape_ {
  42. int type;
  43. BumpAABB shape;
  44. };
  45. /* Shapes */
  46. struct BumpCircleShape_ {
  47. int type;
  48. BumpCircle shape;
  49. };
  50. /* Shapes */
  51. struct BumpSegmentShape_ {
  52. int type;
  53. BumpSegment shape;
  54. };
  55. /* Shape type. Uses the overlapping structs in a union trick. */
  56. union BumpShape_ {
  57. BumpShapeHeader header;
  58. BumpBoxShape box;
  59. BumpCircleShape circle;
  60. BumpSegmentShape segment;
  61. BumpErrorShape error;
  62. };
  63. BumpAABB bumpaabb(double cx, double cy, double w, double h);
  64. BumpAABB bumpaabb_make_int(int x, int y, int w, int h);
  65. BumpCircle bumpcircle(double cx, double cy, double radius);
  66. BumpCircle bumpcircle_make_int(int x, int y, int radius);
  67. BumpSegment bumpsegment(double x1, double y1, double x2, double y2);
  68. BumpSegment bumpsegment_make_int(int x1, int y1, int x2, int y2);
  69. BumpShape bumpcircleshape(double cx, double cy, double radius);
  70. BumpShape bumpsegmentshape(double x1, double y1, double x2, double y2);
  71. BumpShape bumpboxshape(double cx, double cy, double w, double h);
  72. BumpShape bumperrorshape(int errorcode);
  73. BumpShape bumpshape_va(int type, va_list args);
  74. BumpShape bumpshape(int type, ...);
  75. double bumpaabb_left(BumpAABB * self);
  76. double bumpaabb_right(BumpAABB * self);
  77. double bumpaabb_top(BumpAABB * self);
  78. double bumpaabb_down(BumpAABB * self);
  79. double bumpaabb_width(BumpAABB * self);
  80. double bumpaabb_height(BumpAABB * self);
  81. double bumpaabb_overlap_x(BumpAABB self, BumpAABB other);
  82. /* Returns overlap of the bounds boxes of self and other in y direction.
  83. * May be zero or negative if no overlap.
  84. */
  85. double bumpaabb_overlap_y(BumpAABB self, BumpAABB other);
  86. /* Returns overlap of the bounds boxes of self and other in x direction.
  87. * May be zero or negative if no overlap.
  88. */
  89. double bumpaabb_overlap_x_raw(BumpAABB self, BumpAABB other);
  90. /* Returns overlap of the bounds boxes of self and other in y direction.
  91. * May be zero or negative if no overlap.
  92. */
  93. double bumpaabb_overlap_y_raw(BumpAABB self, BumpAABB other);
  94. /* Returns the overlap vector between two bounds boxes. */
  95. BeVec bumpaabb_overlap_vector(BumpAABB self, BumpAABB other);
  96. /* Returns whether or not there is overlap between two bounds boxes. */
  97. int bumpaabb_overlap_p(BumpAABB self, BumpAABB other);
  98. #endif