b2_math.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_MATH_H
  19. #define B2_MATH_H
  20. #include <math.h>
  21. /// This function is used to ensure that a floating point number is not a NaN or infinity.
  22. inline bool b2IsValid(float x)
  23. {
  24. return isfinite(x);
  25. }
  26. #define b2Sqrt(x) sqrtf(x)
  27. #define b2Atan2(y, x) atan2f(y, x)
  28. /// A 2D column vector.
  29. struct b2Vec2
  30. {
  31. /// Default constructor does nothing (for performance).
  32. b2Vec2() {}
  33. /// Construct using coordinates.
  34. b2Vec2(float xIn, float yIn) : x(xIn), y(yIn) {}
  35. /// Set this vector to all zeros.
  36. void SetZero() { x = 0.0f; y = 0.0f; }
  37. /// Set this vector to some specified coordinates.
  38. void Set(float x_, float y_) { x = x_; y = y_; }
  39. /// Negate this vector.
  40. b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
  41. /// Read from and indexed element.
  42. float operator () (int i) const
  43. {
  44. return (&x)[i];
  45. }
  46. /// Write to an indexed element.
  47. float& operator () (int i)
  48. {
  49. return (&x)[i];
  50. }
  51. /// Add a vector to this vector.
  52. void operator += (const b2Vec2& v)
  53. {
  54. x += v.x; y += v.y;
  55. }
  56. /// Subtract a vector from this vector.
  57. void operator -= (const b2Vec2& v)
  58. {
  59. x -= v.x; y -= v.y;
  60. }
  61. /// Multiply this vector by a scalar.
  62. void operator *= (float a)
  63. {
  64. x *= a; y *= a;
  65. }
  66. /// Get the length of this vector (the norm).
  67. float Length() const
  68. {
  69. return b2Sqrt(x * x + y * y);
  70. }
  71. /// Get the length squared. For performance, use this instead of
  72. /// b2Vec2::Length (if possible).
  73. float LengthSquared() const
  74. {
  75. return x * x + y * y;
  76. }
  77. /// Convert this vector into a unit vector. Returns the length.
  78. float Normalize()
  79. {
  80. float length = Length();
  81. if (length < __FLT_EPSILON__)
  82. {
  83. return 0.0f;
  84. }
  85. float invLength = 1.0f / length;
  86. x *= invLength;
  87. y *= invLength;
  88. return length;
  89. }
  90. /// Does this vector contain finite coordinates?
  91. bool IsValid() const
  92. {
  93. return b2IsValid(x) && b2IsValid(y);
  94. }
  95. /// Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
  96. b2Vec2 Skew() const
  97. {
  98. return b2Vec2(-y, x);
  99. }
  100. float x, y;
  101. };
  102. /// Add two vectors component-wise.
  103. inline b2Vec2 operator + (const b2Vec2& a, const b2Vec2& b)
  104. {
  105. return b2Vec2(a.x + b.x, a.y + b.y);
  106. }
  107. /// Subtract two vectors component-wise.
  108. inline b2Vec2 operator - (const b2Vec2& a, const b2Vec2& b)
  109. {
  110. return b2Vec2(a.x - b.x, a.y - b.y);
  111. }
  112. inline b2Vec2 operator * (float s, const b2Vec2& a)
  113. {
  114. return b2Vec2(s * a.x, s * a.y);
  115. }
  116. inline bool operator == (const b2Vec2& a, const b2Vec2& b)
  117. {
  118. return a.x == b.x && a.y == b.y;
  119. }
  120. inline bool operator != (const b2Vec2& a, const b2Vec2& b)
  121. {
  122. return a.x != b.x || a.y != b.y;
  123. }
  124. inline float b2Distance(const b2Vec2& a, const b2Vec2& b)
  125. {
  126. b2Vec2 c = a - b;
  127. return c.Length();
  128. }
  129. /// Perform the cross product on two vectors. In 2D this produces a scalar.
  130. inline float b2Cross(const b2Vec2& a, const b2Vec2& b)
  131. {
  132. return a.x * b.y - a.y * b.x;
  133. }
  134. /// Perform the dot product on two vectors.
  135. inline float b2Dot(const b2Vec2& a, const b2Vec2& b)
  136. {
  137. return a.x * b.x + a.y * b.y;
  138. }
  139. #endif