b2Glue.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2GLUE_H
  19. #define B2GLUE_H
  20. #include "math_2d.h"
  21. #include <limits.h>
  22. namespace b2ConvexDecomp {
  23. typedef real_t float32;
  24. typedef int32_t int32;
  25. static inline float32 b2Sqrt(float32 val) { return Math::sqrt(val); }
  26. #define b2_maxFloat FLT_MAX
  27. #define b2_epsilon CMP_EPSILON
  28. #define b2_pi 3.14159265359f
  29. #define b2_maxPolygonVertices 16
  30. #define b2Max MAX
  31. #define b2Min MIN
  32. #define b2Clamp CLAMP
  33. #define b2Abs ABS
  34. /// A small length used as a collision and constraint tolerance. Usually it is
  35. /// chosen to be numerically significant, but visually insignificant.
  36. #define b2_linearSlop 0.005f
  37. /// A small angle used as a collision and constraint tolerance. Usually it is
  38. /// chosen to be numerically significant, but visually insignificant.
  39. #define b2_angularSlop (2.0f / 180.0f * b2_pi)
  40. /// A 2D column vector.
  41. struct b2Vec2
  42. {
  43. /// Default constructor does nothing (for performance).
  44. b2Vec2() {}
  45. /// Construct using coordinates.
  46. b2Vec2(float32 x, float32 y) : x(x), y(y) {}
  47. /// Set this vector to all zeros.
  48. void SetZero() { x = 0.0f; y = 0.0f; }
  49. /// Set this vector to some specified coordinates.
  50. void Set(float32 x_, float32 y_) { x = x_; y = y_; }
  51. /// Negate this vector.
  52. b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
  53. /// Read from and indexed element.
  54. float32 operator () (int32 i) const
  55. {
  56. return (&x)[i];
  57. }
  58. /// Write to an indexed element.
  59. float32& operator () (int32 i)
  60. {
  61. return (&x)[i];
  62. }
  63. /// Add a vector to this vector.
  64. void operator += (const b2Vec2& v)
  65. {
  66. x += v.x; y += v.y;
  67. }
  68. /// Subtract a vector from this vector.
  69. void operator -= (const b2Vec2& v)
  70. {
  71. x -= v.x; y -= v.y;
  72. }
  73. /// Multiply this vector by a scalar.
  74. void operator *= (float32 a)
  75. {
  76. x *= a; y *= a;
  77. }
  78. /// Get the length of this vector (the norm).
  79. float32 Length() const
  80. {
  81. return b2Sqrt(x * x + y * y);
  82. }
  83. /// Get the length squared. For performance, use this instead of
  84. /// b2Vec2::Length (if possible).
  85. float32 LengthSquared() const
  86. {
  87. return x * x + y * y;
  88. }
  89. bool operator==(const b2Vec2& p_v) const {
  90. return x==p_v.x && y==p_v.y;
  91. }
  92. b2Vec2 operator+(const b2Vec2& p_v) const {
  93. return b2Vec2(x+p_v.x,y+p_v.y);
  94. }
  95. b2Vec2 operator-(const b2Vec2& p_v) const {
  96. return b2Vec2(x-p_v.x,y-p_v.y);
  97. }
  98. b2Vec2 operator*(float32 f) const {
  99. return b2Vec2(f*x,f*y);
  100. }
  101. /// Convert this vector into a unit vector. Returns the length.
  102. float32 Normalize()
  103. {
  104. float32 length = Length();
  105. if (length < b2_epsilon)
  106. {
  107. return 0.0f;
  108. }
  109. float32 invLength = 1.0f / length;
  110. x *= invLength;
  111. y *= invLength;
  112. return length;
  113. }
  114. /*
  115. /// Does this vector contain finite coordinates?
  116. bool IsValid() const
  117. {
  118. return b2IsValid(x) && b2IsValid(y);
  119. }
  120. */
  121. float32 x, y;
  122. };
  123. inline b2Vec2 operator*(float32 f,const b2Vec2& p_v) {
  124. return b2Vec2(f*p_v.x,f*p_v.y);
  125. }
  126. /// Perform the dot product on two vectors.
  127. inline float32 b2Dot(const b2Vec2& a, const b2Vec2& b)
  128. {
  129. return a.x * b.x + a.y * b.y;
  130. }
  131. /// Perform the cross product on two vectors. In 2D this produces a scalar.
  132. inline float32 b2Cross(const b2Vec2& a, const b2Vec2& b)
  133. {
  134. return a.x * b.y - a.y * b.x;
  135. }
  136. /// Perform the cross product on a vector and a scalar. In 2D this produces
  137. /// a vector.
  138. inline b2Vec2 b2Cross(const b2Vec2& a, float32 s)
  139. {
  140. return b2Vec2(s * a.y, -s * a.x);
  141. }
  142. }
  143. #endif