vector3.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  39. friend class Variant;
  40. public:
  41. enum Axis {
  42. AXIS_X,
  43. AXIS_Y,
  44. AXIS_Z,
  45. };
  46. union {
  47. struct {
  48. real_t x;
  49. real_t y;
  50. real_t z;
  51. };
  52. real_t coord[3] = { 0 };
  53. };
  54. inline const real_t &operator[](int p_axis) const {
  55. return coord[p_axis];
  56. }
  57. inline real_t &operator[](int p_axis) {
  58. return coord[p_axis];
  59. }
  60. void set_axis(int p_axis, real_t p_value);
  61. real_t get_axis(int p_axis) const;
  62. int min_axis() const;
  63. int max_axis() const;
  64. inline real_t length() const;
  65. inline real_t length_squared() const;
  66. inline void normalize();
  67. inline Vector3 normalized() const;
  68. inline bool is_normalized() const;
  69. inline Vector3 inverse() const;
  70. inline void zero();
  71. void snap(Vector3 p_val);
  72. Vector3 snapped(Vector3 p_val) const;
  73. void rotate(const Vector3 &p_axis, real_t p_phi);
  74. Vector3 rotated(const Vector3 &p_axis, real_t p_phi) const;
  75. /* Static Methods between 2 vector3s */
  76. inline Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
  77. inline Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
  78. Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
  79. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
  80. inline Vector3 cross(const Vector3 &p_b) const;
  81. inline real_t dot(const Vector3 &p_b) const;
  82. Basis outer(const Vector3 &p_b) const;
  83. Basis to_diagonal_matrix() const;
  84. inline Vector3 abs() const;
  85. inline Vector3 floor() const;
  86. inline Vector3 sign() const;
  87. inline Vector3 ceil() const;
  88. inline Vector3 round() const;
  89. inline real_t distance_to(const Vector3 &p_to) const;
  90. inline real_t distance_squared_to(const Vector3 &p_to) const;
  91. inline Vector3 posmod(const real_t p_mod) const;
  92. inline Vector3 posmodv(const Vector3 &p_modv) const;
  93. inline Vector3 project(const Vector3 &p_to) const;
  94. inline real_t angle_to(const Vector3 &p_to) const;
  95. inline Vector3 direction_to(const Vector3 &p_to) const;
  96. inline Vector3 slide(const Vector3 &p_normal) const;
  97. inline Vector3 bounce(const Vector3 &p_normal) const;
  98. inline Vector3 reflect(const Vector3 &p_normal) const;
  99. bool is_equal_approx(const Vector3 &p_v) const;
  100. /* Operators */
  101. inline Vector3 &operator+=(const Vector3 &p_v);
  102. inline Vector3 operator+(const Vector3 &p_v) const;
  103. inline Vector3 &operator-=(const Vector3 &p_v);
  104. inline Vector3 operator-(const Vector3 &p_v) const;
  105. inline Vector3 &operator*=(const Vector3 &p_v);
  106. inline Vector3 operator*(const Vector3 &p_v) const;
  107. inline Vector3 &operator/=(const Vector3 &p_v);
  108. inline Vector3 operator/(const Vector3 &p_v) const;
  109. inline Vector3 &operator*=(real_t p_scalar);
  110. inline Vector3 operator*(real_t p_scalar) const;
  111. inline Vector3 &operator/=(real_t p_scalar);
  112. inline Vector3 operator/(real_t p_scalar) const;
  113. inline Vector3 operator-() 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. inline bool operator>=(const Vector3 &p_v) const;
  120. operator String() const;
  121. operator Vector3i() const;
  122. inline Vector3() {}
  123. inline Vector3(real_t p_x, real_t p_y, real_t p_z) {
  124. x = p_x;
  125. y = p_y;
  126. z = p_z;
  127. }
  128. Vector3(const Vector3i &p_ivec);
  129. };
  130. Vector3 Vector3::cross(const Vector3 &p_b) const {
  131. Vector3 ret(
  132. (y * p_b.z) - (z * p_b.y),
  133. (z * p_b.x) - (x * p_b.z),
  134. (x * p_b.y) - (y * p_b.x));
  135. return ret;
  136. }
  137. real_t Vector3::dot(const Vector3 &p_b) const {
  138. return x * p_b.x + y * p_b.y + z * p_b.z;
  139. }
  140. Vector3 Vector3::abs() const {
  141. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  142. }
  143. Vector3 Vector3::sign() const {
  144. return Vector3(Math::sign(x), Math::sign(y), Math::sign(z));
  145. }
  146. Vector3 Vector3::floor() const {
  147. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  148. }
  149. Vector3 Vector3::ceil() const {
  150. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  151. }
  152. Vector3 Vector3::round() const {
  153. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  154. }
  155. Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const {
  156. return Vector3(
  157. x + (p_weight * (p_to.x - x)),
  158. y + (p_weight * (p_to.y - y)),
  159. z + (p_weight * (p_to.z - z)));
  160. }
  161. Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
  162. real_t theta = angle_to(p_to);
  163. return rotated(cross(p_to).normalized(), theta * p_weight);
  164. }
  165. real_t Vector3::distance_to(const Vector3 &p_to) const {
  166. return (p_to - *this).length();
  167. }
  168. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  169. return (p_to - *this).length_squared();
  170. }
  171. Vector3 Vector3::posmod(const real_t p_mod) const {
  172. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  173. }
  174. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  175. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  176. }
  177. Vector3 Vector3::project(const Vector3 &p_to) const {
  178. return p_to * (dot(p_to) / p_to.length_squared());
  179. }
  180. real_t Vector3::angle_to(const Vector3 &p_to) const {
  181. return Math::atan2(cross(p_to).length(), dot(p_to));
  182. }
  183. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  184. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  185. ret.normalize();
  186. return ret;
  187. }
  188. /* Operators */
  189. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  190. x += p_v.x;
  191. y += p_v.y;
  192. z += p_v.z;
  193. return *this;
  194. }
  195. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  196. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  197. }
  198. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  199. x -= p_v.x;
  200. y -= p_v.y;
  201. z -= p_v.z;
  202. return *this;
  203. }
  204. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  205. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  206. }
  207. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  208. x *= p_v.x;
  209. y *= p_v.y;
  210. z *= p_v.z;
  211. return *this;
  212. }
  213. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  214. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  215. }
  216. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  217. x /= p_v.x;
  218. y /= p_v.y;
  219. z /= p_v.z;
  220. return *this;
  221. }
  222. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  223. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  224. }
  225. Vector3 &Vector3::operator*=(real_t p_scalar) {
  226. x *= p_scalar;
  227. y *= p_scalar;
  228. z *= p_scalar;
  229. return *this;
  230. }
  231. inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  232. return p_vec * p_scalar;
  233. }
  234. Vector3 Vector3::operator*(real_t p_scalar) const {
  235. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  236. }
  237. Vector3 &Vector3::operator/=(real_t p_scalar) {
  238. x /= p_scalar;
  239. y /= p_scalar;
  240. z /= p_scalar;
  241. return *this;
  242. }
  243. Vector3 Vector3::operator/(real_t p_scalar) const {
  244. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  245. }
  246. Vector3 Vector3::operator-() const {
  247. return Vector3(-x, -y, -z);
  248. }
  249. bool Vector3::operator==(const Vector3 &p_v) const {
  250. return x == p_v.x && y == p_v.y && z == p_v.z;
  251. }
  252. bool Vector3::operator!=(const Vector3 &p_v) const {
  253. return x != p_v.x || y != p_v.y || z != p_v.z;
  254. }
  255. bool Vector3::operator<(const Vector3 &p_v) const {
  256. if (x == p_v.x) {
  257. if (y == p_v.y) {
  258. return z < p_v.z;
  259. }
  260. return y < p_v.y;
  261. }
  262. return x < p_v.x;
  263. }
  264. bool Vector3::operator>(const Vector3 &p_v) const {
  265. if (x == p_v.x) {
  266. if (y == p_v.y) {
  267. return z > p_v.z;
  268. }
  269. return y > p_v.y;
  270. }
  271. return x > p_v.x;
  272. }
  273. bool Vector3::operator<=(const Vector3 &p_v) const {
  274. if (x == p_v.x) {
  275. if (y == p_v.y) {
  276. return z <= p_v.z;
  277. }
  278. return y < p_v.y;
  279. }
  280. return x < p_v.x;
  281. }
  282. bool Vector3::operator>=(const Vector3 &p_v) const {
  283. if (x == p_v.x) {
  284. if (y == p_v.y) {
  285. return z >= p_v.z;
  286. }
  287. return y > p_v.y;
  288. }
  289. return x > p_v.x;
  290. }
  291. inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  292. return p_a.cross(p_b);
  293. }
  294. inline real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  295. return p_a.dot(p_b);
  296. }
  297. real_t Vector3::length() const {
  298. real_t x2 = x * x;
  299. real_t y2 = y * y;
  300. real_t z2 = z * z;
  301. return Math::sqrt(x2 + y2 + z2);
  302. }
  303. real_t Vector3::length_squared() const {
  304. real_t x2 = x * x;
  305. real_t y2 = y * y;
  306. real_t z2 = z * z;
  307. return x2 + y2 + z2;
  308. }
  309. void Vector3::normalize() {
  310. real_t lengthsq = length_squared();
  311. if (lengthsq == (real_t)0.0) {
  312. x = y = z = (real_t)0.0;
  313. } else {
  314. real_t length = Math::sqrt(lengthsq);
  315. x /= length;
  316. y /= length;
  317. z /= length;
  318. }
  319. }
  320. Vector3 Vector3::normalized() const {
  321. Vector3 v = *this;
  322. v.normalize();
  323. return v;
  324. }
  325. bool Vector3::is_normalized() const {
  326. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  327. return Math::is_equal_approx(length_squared(), (real_t)1.0, (real_t)UNIT_EPSILON);
  328. }
  329. Vector3 Vector3::inverse() const {
  330. return Vector3((real_t)1.0 / x, (real_t)1.0 / y, (real_t)1.0 / z);
  331. }
  332. void Vector3::zero() {
  333. x = y = z = (real_t)0.0;
  334. }
  335. // slide returns the component of the vector along the given plane, specified by its normal vector.
  336. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  337. #ifdef MATH_CHECKS
  338. ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3());
  339. #endif
  340. return *this - p_normal * this->dot(p_normal);
  341. }
  342. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  343. return -reflect(p_normal);
  344. }
  345. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  346. #ifdef MATH_CHECKS
  347. ERR_FAIL_COND_V(!p_normal.is_normalized(), Vector3());
  348. #endif
  349. return 2.0 * p_normal * this->dot(p_normal) - *this;
  350. }
  351. } // namespace godot
  352. #endif // GODOT_VECTOR3_HPP