vec2.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * 2D Physic Engine
  3. * vec2.h: Definition and implementation of the 2D vector
  4. * Based on pikuma.com Learn Game Physics Engine Programming course.
  5. * Copyright (c) 2022 986-Studio. All Right Reserved
  6. *
  7. * Created by Manoël Trapier on 07/06/2022.
  8. */
  9. #ifndef PHYSICENGINE_VEC2_H
  10. #define PHYSICENGINE_VEC2_H
  11. #include <cmath>
  12. struct vec2
  13. {
  14. double x, y;
  15. vec2() : x(0.0), y(0.0) {};
  16. vec2(double x, double y) : x(x), y(y) {};
  17. ~vec2() = default;
  18. void add(const vec2 &v)
  19. {
  20. this->x += v.x;
  21. this->y += v.y;
  22. };
  23. void sub(const vec2 &v)
  24. {
  25. this->x -= v.x;
  26. this->y -= v.y;
  27. };
  28. void scale(const double n)
  29. {
  30. this->x *= n;
  31. this->y *= n;
  32. };
  33. vec2 rotate(const double angle) const
  34. {
  35. vec2 result(x * cos(angle) - y * sin(angle),
  36. x * sin(angle) + y * cos(angle));
  37. return result;
  38. };
  39. double magnitude() const
  40. {
  41. return sqrt(this->x * this->x + this->y * this->y);
  42. };
  43. double magnitudeSquared() const
  44. {
  45. return this->x * this->x + this->y * this->y;
  46. };
  47. vec2 &normalise()
  48. {
  49. double m = this->magnitude();
  50. if (m != 0.0)
  51. {
  52. this->x /= m;
  53. this->y /= m;
  54. }
  55. return *this;
  56. };
  57. vec2 unitVector() const
  58. {
  59. double m = this->magnitude();
  60. vec2 ret;
  61. if (m != 0.0)
  62. {
  63. ret.x = this->x / m;
  64. ret.y = this->y / m;
  65. }
  66. return ret;
  67. };
  68. vec2 normal() const
  69. {
  70. vec2 ret(this->y, -this->x);
  71. return ret.normalise();
  72. };
  73. double dot(const vec2 &v) const
  74. {
  75. return (this->x * v.x) + (this->y + v.y);
  76. };
  77. double cross(const vec2 &v) const
  78. {
  79. /* return the imaginary Z component */
  80. return (this->x * v.y) - (this->y * v.x);
  81. };
  82. vec2 & operator = (const vec2 &v)
  83. {
  84. this->x = v.x;
  85. this->y = v.y;
  86. return *this;
  87. };
  88. bool operator == (const vec2 &v) const
  89. {
  90. return (this->x == v.x) && (this->y == v.y);
  91. };
  92. bool operator != (const vec2 &v) const
  93. {
  94. return (this->x != v.x) || (this->y != v.y);
  95. };
  96. vec2 operator + (const vec2 &v) const
  97. {
  98. return vec2(this->x + v.x,
  99. this->y + v.y);
  100. }
  101. vec2 operator - (const vec2 &v) const
  102. {
  103. return vec2(this->x - v.x,
  104. this->y - v.y);
  105. };
  106. vec2 operator * (const double n) const
  107. {
  108. return vec2(this->x * n,
  109. this->y * n);
  110. };
  111. vec2 operator / (const double n) const
  112. {
  113. return vec2(this->x / n,
  114. this->y / n);
  115. };
  116. vec2 operator - ()
  117. {
  118. return vec2(-this->x,
  119. -this->y);
  120. };
  121. vec2 & operator += (const vec2 &v)
  122. {
  123. this->x += v.x;
  124. this->y += v.y;
  125. return *this;
  126. };
  127. vec2 & operator -= (const vec2 &v)
  128. {
  129. this->x -= v.x;
  130. this->y -= v.y;
  131. return *this;
  132. }
  133. vec2 & operator *= (const double n)
  134. {
  135. this->x *= n;
  136. this->y *= n;
  137. return *this;
  138. }
  139. vec2 & operator /= (const double n)
  140. {
  141. this->x /= n;
  142. this->y /= n;
  143. return *this;
  144. }
  145. };
  146. #endif /* PHYSICENGINE_VEC2_H */