Ball.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /******************************************************************************
  2. Use 'Ball' to handle ball shapes, Flt type
  3. Use 'BallD' to handle ball shapes, Dbl type
  4. /******************************************************************************/
  5. struct Ball // Ball Shape
  6. {
  7. Flt r ; // radius
  8. Vec pos; // center position
  9. Ball& zero( ) {T.r=0; T.pos.zero(); return T;}
  10. Ball& set (Flt r, C Vec &pos=VecZero) {T.r=r; T.pos=pos ; return T;}
  11. #if EE_PRIVATE
  12. Ball& setAnimated(C Extent &ext, C Matrix &matrix ); // set ball from 'ext' animated by 'matrix'
  13. #endif
  14. Ball& setAnimated(C Extent &ext, C AnimatedSkeleton &anim_skel); // set approximate ball encapsulating 'ext' animated by 'anim_skel' skeleton
  15. Ball& operator+=(C Vec &v) {pos+=v; return T;}
  16. Ball& operator-=(C Vec &v) {pos-=v; return T;}
  17. Ball& operator*=( Flt f);
  18. Ball& operator/=( Flt f);
  19. Ball& operator*=(C Matrix3 &m);
  20. Ball& operator/=(C Matrix3 &m);
  21. Ball& operator*=(C Matrix &m);
  22. Ball& operator/=(C Matrix &m);
  23. friend Ball operator+ (C Ball &ball, C Vec &v) {return Ball(ball)+=v;}
  24. friend Ball operator- (C Ball &ball, C Vec &v) {return Ball(ball)-=v;}
  25. friend Ball operator* (C Ball &ball, Flt f) {return Ball(ball)*=f;}
  26. friend Ball operator/ (C Ball &ball, Flt f) {return Ball(ball)/=f;}
  27. friend Ball operator* (C Ball &ball, C Matrix3 &m) {return Ball(ball)*=m;}
  28. friend Ball operator/ (C Ball &ball, C Matrix3 &m) {return Ball(ball)/=m;}
  29. friend Ball operator* (C Ball &ball, C Matrix &m) {return Ball(ball)*=m;}
  30. friend Ball operator/ (C Ball &ball, C Matrix &m) {return Ball(ball)/=m;}
  31. // get
  32. Flt area ()C {return (PI*4 )*r*r ;} // get surface area
  33. Flt volume()C {return (PI*4/3)*r*r*r;} // get volume
  34. Vec nearest(C Vec &normal)C; // get nearest point on ball towards normal
  35. Str asText()C {return S+"Radius: "+r+", Pos: "+pos;} // get text description
  36. // operations
  37. Ball& extend(Flt e) {r+=e; return T;} // extend
  38. Bool from (C Vec *point, Int points ); // set ball from an array of points , false on fail (if there are no points)
  39. Bool from (C Vec *point, Int points, C Matrix &matrix); // set ball from an array of points transformed by 'matrix', false on fail (if there are no points)
  40. // draw
  41. #if EE_PRIVATE
  42. void drawVI ( Bool fill=false, C VecI2 &resolution=VecI2(-1))C; // draw, this relies on active object matrix which can be set using 'SetMatrix' function
  43. #endif
  44. void draw (C Color &color=WHITE, Bool fill=false, C VecI2 &resolution=VecI2(-1))C; // draw , this relies on active object matrix which can be set using 'SetMatrix' function
  45. void drawAngle(C Color &color , Flt from, Flt to, C Vec &up, Bool fill=false, C VecI2 &resolution=VecI2(-1))C; // draw with angle ranges, this relies on active object matrix which can be set using 'SetMatrix' function
  46. void draw2 (C Color &color=WHITE, Bool fill=false, Int resolution= -1 )C; // draw box based , this relies on active object matrix which can be set using 'SetMatrix' function
  47. Ball() {}
  48. Ball(Flt r, C Vec &pos=VecZero) {set(r, pos);}
  49. Ball(C Box &box );
  50. Ball(C OBox &obox );
  51. Ball(C Extent &ext );
  52. Ball(C Capsule &capsule);
  53. Ball(C Shape &shape );
  54. Ball(C MeshBase &mesh );
  55. };
  56. /******************************************************************************/
  57. struct BallM // Ball Shape (mixed precision)
  58. {
  59. Flt r ; // radius
  60. VecD pos; // center position
  61. BallM& set(Flt r, C VecD &pos) {T.r=r; T.pos=pos; return T;}
  62. BallM& extend(Flt e) {r+=e; return T;} // extend
  63. BallM() {}
  64. BallM(Flt r, C VecD &pos=0) {set(r, pos);}
  65. };
  66. /******************************************************************************/
  67. struct BallD // Ball Shape (double precision)
  68. {
  69. Dbl r ; // radius
  70. VecD pos; // center position
  71. BallD& set(Dbl r, C VecD &pos) {T.r=r; T.pos=pos; return T;}
  72. BallD& extend(Dbl e) {r+=e; return T;} // extend
  73. BallD() {}
  74. BallD(Dbl r, C VecD &pos=0) {set(r, pos);}
  75. };
  76. /******************************************************************************/
  77. Ball Avg(C Ball &a, C Ball &b);
  78. // distance
  79. Flt Dist (C Vec &point, C Ball &ball ); // distance between point and a ball
  80. Flt Dist (C Edge &edge , C Ball &ball ); // distance between edge and a ball
  81. Flt Dist (C Tri &tri , C Ball &ball ); // distance between triangle and a ball
  82. Flt Dist (C Box &box , C Ball &ball ); // distance between box and a ball
  83. Flt Dist (C OBox &obox , C Ball &ball ); // distance between box and a ball
  84. Flt Dist (C Ball &a , C Ball &b ); // distance between ball and a ball
  85. Flt DistBallPlane(C Ball &ball , C Vec &plane, C Vec &normal); // distance between ball and a plane
  86. Dbl DistBallPlane(C Ball &ball , C VecD &plane, C Vec &normal); // distance between ball and a plane
  87. Dbl DistBallPlane(C BallM &ball , C VecD &plane, C Vec &normal); // distance between ball and a plane
  88. inline Flt Dist (C Ball &ball , C Plane &plane ) {return DistBallPlane(ball, plane.pos, plane.normal);} // distance between ball and a plane
  89. inline Dbl Dist (C Ball &ball , C PlaneM &plane ) {return DistBallPlane(ball, plane.pos, plane.normal);} // distance between ball and a plane
  90. inline Dbl Dist (C BallM &ball , C PlaneM &plane ) {return DistBallPlane(ball, plane.pos, plane.normal);} // distance between ball and a plane
  91. // cuts
  92. Bool Cuts(C Vec &point, C Ball &ball); // if point cuts a ball
  93. Bool Cuts(C VecD &point, C Ball &ball); // if point cuts a ball
  94. Bool Cuts(C VecD &point, C BallM &ball); // if point cuts a ball
  95. Bool Cuts(C Edge &edge , C Ball &ball); // if edge cuts a ball
  96. Bool Cuts(C Tri &tri , C Ball &ball); // if triangle cuts a ball
  97. Bool Cuts(C Box &box , C Ball &ball); // if box cuts a ball
  98. Bool Cuts(C OBox &obox , C Ball &ball); // if box cuts a ball
  99. Bool Cuts(C Extent &ext , C Ball &ball); // if extent cuts a ball
  100. Bool Cuts(C Ball &a , C Ball &b ); // if ball cuts a ball
  101. Int CutsStrBall (C Vec & str_pos , C Vec & str_dir, C Ball &ball, Vec *contact_a=null, Vec *contact_b=null); // if straight infinite line cuts a ball, return number of contacts, 'str_dir'=straight line direction (must be normalized)
  102. Int CutsEdgeBall(C Vec &edge_start, C Vec &edge_end, C Ball &ball, Vec *contact_a=null, Vec *contact_b=null); // if edge cuts a ball, return number of contacts
  103. Bool Inside(C Box &a, C Ball &b); // if 'a' is fully inside 'b'
  104. Bool Inside(C Extent &a, C Ball &b); // if 'a' is fully inside 'b'
  105. // sweep
  106. Bool SweepPointBall(C Vec &point, C Vec &move, C Ball &ball , Flt *hit_frac=null, Vec *hit_normal=null); // if moving point cuts through a static ball
  107. Bool SweepPointBall(C VecD &point, C VecD &move, C BallD &ball , Dbl *hit_frac=null, VecD *hit_normal=null); // if moving point cuts through a static ball
  108. Bool SweepBallPoint(C Ball &ball, C Vec &move, C Vec &point, Flt *hit_frac=null, Vec *hit_normal=null); // if moving ball cuts through a static point
  109. Bool SweepBallPoint(C BallD &ball, C VecD &move, C VecD &point, Dbl *hit_frac=null, VecD *hit_normal=null); // if moving ball cuts through a static point
  110. Bool SweepBallEdge (C Ball &ball, C Vec &move, C Edge &edge , Flt *hit_frac=null, Vec *hit_normal=null); // if moving ball cuts through a static edge
  111. Bool SweepBallEdge (C BallD &ball, C VecD &move, C EdgeD &edge , Dbl *hit_frac=null, VecD *hit_normal=null); // if moving ball cuts through a static edge
  112. Bool SweepBallBall (C Ball &ball, C Vec &move, C Ball &ball2, Flt *hit_frac=null, Vec *hit_normal=null); // if moving ball cuts through a static ball
  113. Bool SweepBallBall (C BallD &ball, C VecD &move, C BallD &ball2, Dbl *hit_frac=null, VecD *hit_normal=null); // if moving ball cuts through a static ball
  114. /******************************************************************************/