very early prototype of a spaceshooter in SFML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vec2util.hpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Simple utility header for the sf::Vector2f class.
  2. // copied from https://en.sfml-dev.org/forums/index.php?topic=21356.0
  3. #pragma once
  4. #include <math.h>
  5. #include <SFML/System/Vector2.hpp>
  6. #define PI 3.14159265359
  7. #define RAD2DEG 57.2957795056
  8. #define DEG2RAD 0.01745329252
  9. typedef sf::Vector2f Vec2;
  10. namespace Proto4 {
  11. /**
  12. * @brief dot
  13. * @param a vector
  14. * @param b vector
  15. * @return a . b
  16. */
  17. static inline float dot (const Vec2& a, const Vec2& b)
  18. {
  19. return a.x*b.x + a.y*b.y;
  20. }
  21. /**
  22. * @brief magnitudeSq
  23. * @param v vector
  24. * @return |v|^2
  25. */
  26. static inline float magnitudeSq (const Vec2& v)
  27. {
  28. return v.x*v.x + v.y*v.y;
  29. }
  30. /**
  31. * @brief magnitude
  32. * @param v vector
  33. * @return |v|
  34. */
  35. static inline float magnitude (const Vec2& v)
  36. {
  37. return sqrt (v.x*v.x + v.y*v.y);
  38. }
  39. /**
  40. * @brief normalize
  41. * @param v vector
  42. * v' = v / |v|
  43. */
  44. static inline void normalize (Vec2& v)
  45. {
  46. float len = sqrt (v.x*v.x + v.y*v.y);
  47. if (len != 0) {
  48. v.x = v.x / len;
  49. v.y = v.y / len;
  50. }
  51. }
  52. /**
  53. * @brief normalized (faster)
  54. * @param res vector
  55. * @param v vector
  56. * res = v / |v|
  57. */
  58. static inline void normalized (Vec2& res, const Vec2& v)
  59. {
  60. float len = sqrt (v.x*v.x + v.y*v.y);
  61. if (len != 0) {
  62. res.x = v.x / len;
  63. res.y = v.y / len;
  64. }
  65. }
  66. /**
  67. * @brief normalized
  68. * @param v vector
  69. * @return res = v / |v|
  70. */
  71. static inline Vec2 normalized (const Vec2& v)
  72. {
  73. Vec2 res;
  74. normalized(res, v);
  75. return res;
  76. }
  77. /**
  78. * @brief truncate (faster)
  79. * @param res vector
  80. * @param v vector
  81. * @param max magnitude
  82. * res = max * v / |v|
  83. */
  84. static inline void truncate (Vec2& res, const Vec2& v, float max)
  85. {
  86. float len = sqrt (v.x*v.x + v.y*v.y);
  87. if (len > max)
  88. {
  89. res = normalized(v);
  90. res *= max;
  91. } else {
  92. res = v;
  93. }
  94. }
  95. /**
  96. * @brief truncate
  97. * @param v vector
  98. * @param max magnitude
  99. * @return res = max * v / |v|
  100. */
  101. static inline Vec2 truncate (const Vec2& v, float max)
  102. {
  103. Vec2 res;
  104. truncate(res, v, max);
  105. return res;
  106. }
  107. /**
  108. * @brief lerp (faster)
  109. * @param res vector
  110. * @param a vector
  111. * @param b vector
  112. * @param t alpha value
  113. * res = (1-t)*a + t*b
  114. */
  115. static inline void lerp (Vec2& res, const Vec2& a, const Vec2& b, float t)
  116. {
  117. res.x = (1-t)*a.x + t*b.x;
  118. res.y = (1-t)*a.y + t*b.y;
  119. }
  120. /**
  121. * @brief lerp
  122. * @param a vector
  123. * @param b vector
  124. * @param t alpha value
  125. * @return res = (1-t)*a + t*b
  126. */
  127. static inline Vec2 lerp (const Vec2& a, const Vec2& b, float t)
  128. {
  129. Vec2 res;
  130. lerp(res, a, b, t);
  131. return res;
  132. }
  133. /**
  134. * @brief rotate
  135. * @param res vector
  136. * @param v vector
  137. * @param angle
  138. * R = |cos -sin|
  139. * |sin cos|
  140. * res = v * R
  141. */
  142. static inline void rotate (Vec2& res, const Vec2& v, float angle)
  143. {
  144. const float rad = DEG2RAD * angle;
  145. const float sin0 = sin(rad);
  146. const float cos0 = cos(rad);
  147. res.x = v.x * cos0 + v.y * -sin0;
  148. res.y = v.x * sin0 + v.y * cos0;
  149. }
  150. /**
  151. * @brief rotate
  152. * @param v vector
  153. * @param angle
  154. * @return res = v * R
  155. */
  156. static inline Vec2 rotate (Vec2& v, float angle)
  157. {
  158. Vec2 res;
  159. rotate(res, v, angle);
  160. return res;
  161. }
  162. }