vector3.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*************************************************************************/
  2. /* vector3.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_VECTOR3_HPP
  31. #define GODOT_VECTOR3_HPP
  32. #include <godot_cpp/core/math.hpp>
  33. #include <godot_cpp/variant/string.hpp>
  34. namespace godot {
  35. class Basis;
  36. class Vector3i;
  37. class Vector3 {
  38. public:
  39. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  40. enum Axis {
  41. AXIS_X,
  42. AXIS_Y,
  43. AXIS_Z,
  44. };
  45. union {
  46. struct {
  47. real_t x;
  48. real_t y;
  49. real_t z;
  50. };
  51. real_t coord[3] = { 0 };
  52. };
  53. inline const real_t &operator[](int p_axis) const {
  54. return coord[p_axis];
  55. }
  56. inline real_t &operator[](int p_axis) {
  57. return coord[p_axis];
  58. }
  59. void set_axis(int p_axis, real_t p_value);
  60. real_t get_axis(int p_axis) const;
  61. int min_axis() const;
  62. int max_axis() const;
  63. inline real_t length() const;
  64. inline real_t length_squared() const;
  65. inline void normalize();
  66. inline Vector3 normalized() const;
  67. inline bool is_normalized() const;
  68. inline Vector3 inverse() const;
  69. inline void zero();
  70. void snap(Vector3 p_val);
  71. Vector3 snapped(Vector3 p_val) const;
  72. void rotate(const Vector3 &p_axis, real_t p_phi);
  73. Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const;
  74. /* Static Methods between 2 vector3s */
  75. inline Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
  76. inline Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
  77. Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
  78. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
  79. inline Vector3 cross(const Vector3 &p_b) const;
  80. inline real_t dot(const Vector3 &p_b) const;
  81. Basis outer(const Vector3 &p_b) const;
  82. Basis to_diagonal_matrix() const;
  83. inline Vector3 abs() const;
  84. inline Vector3 floor() const;
  85. inline Vector3 sign() const;
  86. inline Vector3 ceil() const;
  87. inline Vector3 round() const;
  88. inline real_t distance_to(const Vector3 &p_to) const;
  89. inline real_t distance_squared_to(const Vector3 &p_to) const;
  90. inline Vector3 posmod(const real_t p_mod) const;
  91. inline Vector3 posmodv(const Vector3 &p_modv) const;
  92. inline Vector3 project(const Vector3 &p_to) const;
  93. inline real_t angle_to(const Vector3 &p_to) const;
  94. inline Vector3 direction_to(const Vector3 &p_to) const;
  95. inline Vector3 slide(const Vector3 &p_normal) const;
  96. inline Vector3 bounce(const Vector3 &p_normal) const;
  97. inline Vector3 reflect(const Vector3 &p_normal) const;
  98. bool is_equal_approx(const Vector3 &p_v) const;
  99. /* Operators */
  100. inline Vector3 &operator+=(const Vector3 &p_v);
  101. inline Vector3 operator+(const Vector3 &p_v) const;
  102. inline Vector3 &operator-=(const Vector3 &p_v);
  103. inline Vector3 operator-(const Vector3 &p_v) const;
  104. inline Vector3 &operator*=(const Vector3 &p_v);
  105. inline Vector3 operator*(const Vector3 &p_v) const;
  106. inline Vector3 &operator/=(const Vector3 &p_v);
  107. inline Vector3 operator/(const Vector3 &p_v) const;
  108. inline Vector3 &operator*=(real_t p_scalar);
  109. inline Vector3 operator*(real_t p_scalar) const;
  110. inline Vector3 &operator/=(real_t p_scalar);
  111. inline Vector3 operator/(real_t p_scalar) const;
  112. inline Vector3 operator-() const;
  113. inline bool operator==(const Vector3 &p_v) const;
  114. inline bool operator!=(const Vector3 &p_v) const;
  115. inline bool operator<(const Vector3 &p_v) const;
  116. inline bool operator<=(const Vector3 &p_v) const;
  117. inline bool operator>(const Vector3 &p_v) const;
  118. inline bool operator>=(const Vector3 &p_v) const;
  119. operator String() const;
  120. operator Vector3i() const;
  121. inline Vector3() {}
  122. inline Vector3(real_t p_x, real_t p_y, real_t p_z) {
  123. x = p_x;
  124. y = p_y;
  125. z = p_z;
  126. }
  127. Vector3(const Vector3i &p_ivec);
  128. };
  129. Vector3 Vector3::cross(const Vector3 &p_b) const {
  130. Vector3 ret(
  131. (y * p_b.z) - (z * p_b.y),
  132. (z * p_b.x) - (x * p_b.z),
  133. (x * p_b.y) - (y * p_b.x));
  134. return ret;
  135. }
  136. real_t Vector3::dot(const Vector3 &p_b) const {
  137. return x * p_b.x + y * p_b.y + z * p_b.z;
  138. }
  139. Vector3 Vector3::abs() const {
  140. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  141. }
  142. Vector3 Vector3::sign() const {
  143. return Vector3(Math::sign(x), Math::sign(y), Math::sign(z));
  144. }
  145. Vector3 Vector3::floor() const {
  146. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  147. }
  148. Vector3 Vector3::ceil() const {
  149. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  150. }
  151. Vector3 Vector3::round() const {
  152. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  153. }
  154. Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const {
  155. return Vector3(
  156. x + (p_weight * (p_to.x - x)),
  157. y + (p_weight * (p_to.y - y)),
  158. z + (p_weight * (p_to.z - z)));
  159. }
  160. Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
  161. real_t theta = angle_to(p_to);
  162. return rotated(cross(p_to).normalized(), theta * p_weight);
  163. }
  164. real_t Vector3::distance_to(const Vector3 &p_to) const {
  165. return (p_to - *this).length();
  166. }
  167. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  168. return (p_to - *this).length_squared();
  169. }
  170. Vector3 Vector3::posmod(const real_t p_mod) const {
  171. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  172. }
  173. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  174. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  175. }
  176. Vector3 Vector3::project(const Vector3 &p_to) const {
  177. return p_to * (dot(p_to) / p_to.length_squared());
  178. }
  179. real_t Vector3::angle_to(const Vector3 &p_to) const {
  180. return Math::atan2(cross(p_to).length(), dot(p_to));
  181. }
  182. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  183. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  184. ret.normalize();
  185. return ret;
  186. }
  187. /* Operators */
  188. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  189. x += p_v.x;
  190. y += p_v.y;
  191. z += p_v.z;
  192. return *this;
  193. }
  194. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  195. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  196. }
  197. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  198. x -= p_v.x;
  199. y -= p_v.y;
  200. z -= p_v.z;
  201. return *this;
  202. }
  203. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  204. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  205. }
  206. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  207. x *= p_v.x;
  208. y *= p_v.y;
  209. z *= p_v.z;
  210. return *this;
  211. }
  212. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  213. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  214. }
  215. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  216. x /= p_v.x;
  217. y /= p_v.y;
  218. z /= p_v.z;
  219. return *this;
  220. }
  221. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  222. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  223. }
  224. Vector3 &Vector3::operator*=(real_t p_scalar) {
  225. x *= p_scalar;
  226. y *= p_scalar;
  227. z *= p_scalar;
  228. return *this;
  229. }
  230. inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  231. return p_vec * p_scalar;
  232. }
  233. Vector3 Vector3::operator*(real_t p_scalar) const {
  234. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  235. }
  236. Vector3 &Vector3::operator/=(real_t p_scalar) {
  237. x /= p_scalar;
  238. y /= p_scalar;
  239. z /= p_scalar;
  240. return *this;
  241. }
  242. Vector3 Vector3::operator/(real_t p_scalar) const {
  243. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  244. }
  245. Vector3 Vector3::operator-() const {
  246. return Vector3(-x, -y, -z);
  247. }
  248. bool Vector3::operator==(const Vector3 &p_v) const {
  249. return x == p_v.x && y == p_v.y && z == p_v.z;
  250. }
  251. bool Vector3::operator!=(const Vector3 &p_v) const {
  252. return x != p_v.x || y != p_v.y || z != p_v.z;
  253. }
  254. bool Vector3::operator<(const Vector3 &p_v) const {
  255. if (x == p_v.x) {
  256. if (y == p_v.y) {
  257. return z < p_v.z;
  258. }
  259. return y < p_v.y;
  260. }
  261. return x < p_v.x;
  262. }
  263. bool Vector3::operator>(const Vector3 &p_v) const {
  264. if (x == p_v.x) {
  265. if (y == p_v.y) {
  266. return z > p_v.z;
  267. }
  268. return y > p_v.y;
  269. }
  270. return x > p_v.x;
  271. }
  272. bool Vector3::operator<=(const Vector3 &p_v) const {
  273. if (x == p_v.x) {
  274. if (y == p_v.y) {
  275. return z <= p_v.z;
  276. }
  277. return y < p_v.y;
  278. }
  279. return x < p_v.x;
  280. }
  281. bool Vector3::operator>=(const Vector3 &p_v) const {
  282. if (x == p_v.x) {
  283. if (y == p_v.y) {
  284. return z >= p_v.z;
  285. }
  286. return y > p_v.y;
  287. }
  288. return x > p_v.x;
  289. }
  290. inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  291. return p_a.cross(p_b);
  292. }
  293. inline real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  294. return p_a.dot(p_b);
  295. }
  296. real_t Vector3::length() const {
  297. real_t x2 = x * x;
  298. real_t y2 = y * y;
  299. real_t z2 = z * z;
  300. return Math::sqrt(x2 + y2 + z2);
  301. }
  302. real_t Vector3::length_squared() const {
  303. real_t x2 = x * x;
  304. real_t y2 = y * y;
  305. real_t z2 = z * z;
  306. return x2 + y2 + z2;
  307. }
  308. void Vector3::normalize() {
  309. real_t lengthsq = length_squared();
  310. if (lengthsq == 0) {
  311. x = y = z = 0;
  312. } else {
  313. real_t length = Math::sqrt(lengthsq);
  314. x /= length;
  315. y /= length;
  316. z /= length;
  317. }
  318. }
  319. Vector3 Vector3::normalized() const {
  320. Vector3 v = *this;
  321. v.normalize();
  322. return v;
  323. }
  324. bool Vector3::is_normalized() const {
  325. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  326. return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON);
  327. }
  328. Vector3 Vector3::inverse() const {
  329. return Vector3(1.0 / x, 1.0 / y, 1.0 / z);
  330. }
  331. void Vector3::zero() {
  332. x = y = z = 0;
  333. }
  334. // slide returns the component of the vector along the given plane, specified by its normal vector.
  335. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  336. #ifdef MATH_CHECKS
  337. ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3());
  338. #endif
  339. return *this - p_normal * this->dot(p_normal);
  340. }
  341. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  342. return -reflect(p_normal);
  343. }
  344. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  345. #ifdef MATH_CHECKS
  346. ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3());
  347. #endif
  348. return 2.0 * p_normal * this->dot(p_normal) - *this;
  349. }
  350. } // namespace godot
  351. #endif // GODOT_VECTOR3_HPP