Vector3.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // file: core/math/vector3.h
  2. // commit: bd282ff43f23fe845f29a3e25c8efc01bd65ffb0
  3. // file: core/math/vector3.cpp
  4. // commit: 7ad14e7a3e6f87ddc450f7e34621eb5200808451
  5. // file: core/variant_call.cpp
  6. // commit: 5ad9be4c24e9d7dc5672fdc42cea896622fe5685
  7. using System;
  8. using System.Runtime.InteropServices;
  9. #if REAL_T_IS_DOUBLE
  10. using real_t = System.Double;
  11. #else
  12. using real_t = System.Single;
  13. #endif
  14. namespace Godot
  15. {
  16. [StructLayout(LayoutKind.Sequential)]
  17. public struct Vector3 : IEquatable<Vector3>
  18. {
  19. public enum Axis
  20. {
  21. X = 0,
  22. Y,
  23. Z
  24. }
  25. public real_t x;
  26. public real_t y;
  27. public real_t z;
  28. public real_t this[int index]
  29. {
  30. get
  31. {
  32. switch (index)
  33. {
  34. case 0:
  35. return x;
  36. case 1:
  37. return y;
  38. case 2:
  39. return z;
  40. default:
  41. throw new IndexOutOfRangeException();
  42. }
  43. }
  44. set
  45. {
  46. switch (index)
  47. {
  48. case 0:
  49. x = value;
  50. return;
  51. case 1:
  52. y = value;
  53. return;
  54. case 2:
  55. z = value;
  56. return;
  57. default:
  58. throw new IndexOutOfRangeException();
  59. }
  60. }
  61. }
  62. internal void Normalize()
  63. {
  64. real_t length = Length();
  65. if (length == 0f)
  66. {
  67. x = y = z = 0f;
  68. }
  69. else
  70. {
  71. x /= length;
  72. y /= length;
  73. z /= length;
  74. }
  75. }
  76. public Vector3 Abs()
  77. {
  78. return new Vector3(Mathf.Abs(x), Mathf.Abs(y), Mathf.Abs(z));
  79. }
  80. public real_t AngleTo(Vector3 to)
  81. {
  82. return Mathf.Atan2(Cross(to).Length(), Dot(to));
  83. }
  84. public Vector3 Bounce(Vector3 n)
  85. {
  86. return -Reflect(n);
  87. }
  88. public Vector3 Ceil()
  89. {
  90. return new Vector3(Mathf.Ceil(x), Mathf.Ceil(y), Mathf.Ceil(z));
  91. }
  92. public Vector3 Cross(Vector3 b)
  93. {
  94. return new Vector3
  95. (
  96. y * b.z - z * b.y,
  97. z * b.x - x * b.z,
  98. x * b.y - y * b.x
  99. );
  100. }
  101. public Vector3 CubicInterpolate(Vector3 b, Vector3 preA, Vector3 postB, real_t t)
  102. {
  103. var p0 = preA;
  104. var p1 = this;
  105. var p2 = b;
  106. var p3 = postB;
  107. real_t t2 = t * t;
  108. real_t t3 = t2 * t;
  109. return 0.5f * (
  110. p1 * 2.0f + (-p0 + p2) * t +
  111. (2.0f * p0 - 5.0f * p1 + 4f * p2 - p3) * t2 +
  112. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3
  113. );
  114. }
  115. public real_t DistanceSquaredTo(Vector3 b)
  116. {
  117. return (b - this).LengthSquared();
  118. }
  119. public real_t DistanceTo(Vector3 b)
  120. {
  121. return (b - this).Length();
  122. }
  123. public real_t Dot(Vector3 b)
  124. {
  125. return x * b.x + y * b.y + z * b.z;
  126. }
  127. public Vector3 Floor()
  128. {
  129. return new Vector3(Mathf.Floor(x), Mathf.Floor(y), Mathf.Floor(z));
  130. }
  131. public Vector3 Inverse()
  132. {
  133. return new Vector3(1.0f / x, 1.0f / y, 1.0f / z);
  134. }
  135. public bool IsNormalized()
  136. {
  137. return Mathf.Abs(LengthSquared() - 1.0f) < Mathf.Epsilon;
  138. }
  139. public real_t Length()
  140. {
  141. real_t x2 = x * x;
  142. real_t y2 = y * y;
  143. real_t z2 = z * z;
  144. return Mathf.Sqrt(x2 + y2 + z2);
  145. }
  146. public real_t LengthSquared()
  147. {
  148. real_t x2 = x * x;
  149. real_t y2 = y * y;
  150. real_t z2 = z * z;
  151. return x2 + y2 + z2;
  152. }
  153. public Vector3 LinearInterpolate(Vector3 b, real_t t)
  154. {
  155. return new Vector3
  156. (
  157. x + t * (b.x - x),
  158. y + t * (b.y - y),
  159. z + t * (b.z - z)
  160. );
  161. }
  162. public Axis MaxAxis()
  163. {
  164. return x < y ? (y < z ? Axis.Z : Axis.Y) : (x < z ? Axis.Z : Axis.X);
  165. }
  166. public Axis MinAxis()
  167. {
  168. return x < y ? (x < z ? Axis.X : Axis.Z) : (y < z ? Axis.Y : Axis.Z);
  169. }
  170. public Vector3 Normalized()
  171. {
  172. var v = this;
  173. v.Normalize();
  174. return v;
  175. }
  176. public Basis Outer(Vector3 b)
  177. {
  178. return new Basis(
  179. x * b.x, x * b.y, x * b.z,
  180. y * b.x, y * b.y, y * b.z,
  181. z * b.x, z * b.y, z * b.z
  182. );
  183. }
  184. public Vector3 Project(Vector3 onNormal)
  185. {
  186. return onNormal * (Dot(onNormal) / onNormal.LengthSquared());
  187. }
  188. public Vector3 Reflect(Vector3 n)
  189. {
  190. #if DEBUG
  191. if (!n.IsNormalized())
  192. throw new ArgumentException(String.Format("{0} is not normalized", n), nameof(n));
  193. #endif
  194. return 2.0f * n * Dot(n) - this;
  195. }
  196. public Vector3 Round()
  197. {
  198. return new Vector3(Mathf.Round(x), Mathf.Round(y), Mathf.Round(z));
  199. }
  200. public Vector3 Rotated(Vector3 axis, real_t phi)
  201. {
  202. return new Basis(axis, phi).Xform(this);
  203. }
  204. public void Set(real_t x, real_t y, real_t z)
  205. {
  206. this.x = x;
  207. this.y = y;
  208. this.z = z;
  209. }
  210. public void Set(Vector3 v)
  211. {
  212. x = v.x;
  213. y = v.y;
  214. z = v.z;
  215. }
  216. public Vector3 Slerp(Vector3 b, real_t t)
  217. {
  218. real_t theta = AngleTo(b);
  219. return Rotated(Cross(b), theta * t);
  220. }
  221. public Vector3 Slide(Vector3 n)
  222. {
  223. return this - n * Dot(n);
  224. }
  225. public Vector3 Snapped(Vector3 by)
  226. {
  227. return new Vector3
  228. (
  229. Mathf.Stepify(x, by.x),
  230. Mathf.Stepify(y, by.y),
  231. Mathf.Stepify(z, by.z)
  232. );
  233. }
  234. public Basis ToDiagonalMatrix()
  235. {
  236. return new Basis(
  237. x, 0f, 0f,
  238. 0f, y, 0f,
  239. 0f, 0f, z
  240. );
  241. }
  242. // Constants
  243. private static readonly Vector3 _zero = new Vector3(0, 0, 0);
  244. private static readonly Vector3 _one = new Vector3(1, 1, 1);
  245. private static readonly Vector3 _negOne = new Vector3(-1, -1, -1);
  246. private static readonly Vector3 _inf = new Vector3(Mathf.Inf, Mathf.Inf, Mathf.Inf);
  247. private static readonly Vector3 _up = new Vector3(0, 1, 0);
  248. private static readonly Vector3 _down = new Vector3(0, -1, 0);
  249. private static readonly Vector3 _right = new Vector3(1, 0, 0);
  250. private static readonly Vector3 _left = new Vector3(-1, 0, 0);
  251. private static readonly Vector3 _forward = new Vector3(0, 0, -1);
  252. private static readonly Vector3 _back = new Vector3(0, 0, 1);
  253. public static Vector3 Zero { get { return _zero; } }
  254. public static Vector3 One { get { return _one; } }
  255. public static Vector3 NegOne { get { return _negOne; } }
  256. public static Vector3 Inf { get { return _inf; } }
  257. public static Vector3 Up { get { return _up; } }
  258. public static Vector3 Down { get { return _down; } }
  259. public static Vector3 Right { get { return _right; } }
  260. public static Vector3 Left { get { return _left; } }
  261. public static Vector3 Forward { get { return _forward; } }
  262. public static Vector3 Back { get { return _back; } }
  263. // Constructors
  264. public Vector3(real_t x, real_t y, real_t z)
  265. {
  266. this.x = x;
  267. this.y = y;
  268. this.z = z;
  269. }
  270. public Vector3(Vector3 v)
  271. {
  272. x = v.x;
  273. y = v.y;
  274. z = v.z;
  275. }
  276. public static Vector3 operator +(Vector3 left, Vector3 right)
  277. {
  278. left.x += right.x;
  279. left.y += right.y;
  280. left.z += right.z;
  281. return left;
  282. }
  283. public static Vector3 operator -(Vector3 left, Vector3 right)
  284. {
  285. left.x -= right.x;
  286. left.y -= right.y;
  287. left.z -= right.z;
  288. return left;
  289. }
  290. public static Vector3 operator -(Vector3 vec)
  291. {
  292. vec.x = -vec.x;
  293. vec.y = -vec.y;
  294. vec.z = -vec.z;
  295. return vec;
  296. }
  297. public static Vector3 operator *(Vector3 vec, real_t scale)
  298. {
  299. vec.x *= scale;
  300. vec.y *= scale;
  301. vec.z *= scale;
  302. return vec;
  303. }
  304. public static Vector3 operator *(real_t scale, Vector3 vec)
  305. {
  306. vec.x *= scale;
  307. vec.y *= scale;
  308. vec.z *= scale;
  309. return vec;
  310. }
  311. public static Vector3 operator *(Vector3 left, Vector3 right)
  312. {
  313. left.x *= right.x;
  314. left.y *= right.y;
  315. left.z *= right.z;
  316. return left;
  317. }
  318. public static Vector3 operator /(Vector3 vec, real_t scale)
  319. {
  320. vec.x /= scale;
  321. vec.y /= scale;
  322. vec.z /= scale;
  323. return vec;
  324. }
  325. public static Vector3 operator /(Vector3 left, Vector3 right)
  326. {
  327. left.x /= right.x;
  328. left.y /= right.y;
  329. left.z /= right.z;
  330. return left;
  331. }
  332. public static bool operator ==(Vector3 left, Vector3 right)
  333. {
  334. return left.Equals(right);
  335. }
  336. public static bool operator !=(Vector3 left, Vector3 right)
  337. {
  338. return !left.Equals(right);
  339. }
  340. public static bool operator <(Vector3 left, Vector3 right)
  341. {
  342. if (left.x == right.x)
  343. {
  344. if (left.y == right.y)
  345. return left.z < right.z;
  346. return left.y < right.y;
  347. }
  348. return left.x < right.x;
  349. }
  350. public static bool operator >(Vector3 left, Vector3 right)
  351. {
  352. if (left.x == right.x)
  353. {
  354. if (left.y == right.y)
  355. return left.z > right.z;
  356. return left.y > right.y;
  357. }
  358. return left.x > right.x;
  359. }
  360. public static bool operator <=(Vector3 left, Vector3 right)
  361. {
  362. if (left.x == right.x)
  363. {
  364. if (left.y == right.y)
  365. return left.z <= right.z;
  366. return left.y < right.y;
  367. }
  368. return left.x < right.x;
  369. }
  370. public static bool operator >=(Vector3 left, Vector3 right)
  371. {
  372. if (left.x == right.x)
  373. {
  374. if (left.y == right.y)
  375. return left.z >= right.z;
  376. return left.y > right.y;
  377. }
  378. return left.x > right.x;
  379. }
  380. public override bool Equals(object obj)
  381. {
  382. if (obj is Vector3)
  383. {
  384. return Equals((Vector3)obj);
  385. }
  386. return false;
  387. }
  388. public bool Equals(Vector3 other)
  389. {
  390. return x == other.x && y == other.y && z == other.z;
  391. }
  392. public override int GetHashCode()
  393. {
  394. return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode();
  395. }
  396. public override string ToString()
  397. {
  398. return String.Format("({0}, {1}, {2})", new object[]
  399. {
  400. x.ToString(),
  401. y.ToString(),
  402. z.ToString()
  403. });
  404. }
  405. public string ToString(string format)
  406. {
  407. return String.Format("({0}, {1}, {2})", new object[]
  408. {
  409. x.ToString(format),
  410. y.ToString(format),
  411. z.ToString(format)
  412. });
  413. }
  414. }
  415. }