vector2.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/wwmath/vector2.h 24 7/06/01 9:43a Byon_g $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Westwood 3D *
  24. * *
  25. * File Name : VECTOR2.H *
  26. * *
  27. * Programmer : Greg Hjelstrom *
  28. * *
  29. * Start Date : 02/24/97 *
  30. * *
  31. * Last Update : February 24, 1997 [GH] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Scalar Division Operator -- Divide a vector by a scalar *
  36. * Scalar Multiply Operator -- Multiply a vector by a scalar *
  37. * Vector Addition Operator -- Add two vectors *
  38. * Vector Subtraction Operator -- Subract two vectors *
  39. * Vector Inner Product Operator -- Compute the inner or dot product *
  40. * Vector Equality Operator -- Detemine if two vectors are identical *
  41. * Equal_Within_Epsilon -- Determine if two vectors are identical within *
  42. * Vector Inequality Operator -- Detemine if two vectors are identical *
  43. * Swap -- swap two Vector2's *
  44. * Vector2::Is_Valid -- Verifies that all components are valid floats *
  45. * Vector2::Update_Min -- sets each component of the vector to the min of this and a. *
  46. * Vector2::Update_Max -- sets each component of the vector to the max of this and a. *
  47. * Vector2::Scale -- multiply components of a vector by independant scaling factors. *
  48. * Vector2::Lerp -- linearly interpolates two Vector2's *
  49. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  50. #if defined(_MSC_VER)
  51. #pragma once
  52. #endif
  53. #ifndef VECTOR2_H
  54. #define VECTOR2_H
  55. #include "always.h"
  56. #include "wwmath.h"
  57. #include <math.h>
  58. /*
  59. ** 2-Dimensional Vectors
  60. */
  61. class Vector2
  62. {
  63. public:
  64. union {
  65. float X;
  66. float U;
  67. };
  68. union {
  69. float Y;
  70. float V;
  71. };
  72. // Constructors
  73. WWINLINE Vector2(void) {};
  74. WWINLINE Vector2(const Vector2 & v) { X = v.X; Y = v.Y; }
  75. WWINLINE Vector2(float x, float y) { X = x; Y = y; }
  76. WWINLINE Vector2(const float vector[2]) { X = vector[0]; Y = vector[1]; }
  77. // Assignment
  78. WWINLINE Vector2 & operator = (const Vector2 & v) { X = v[0]; Y = v[1]; return *this; }
  79. WWINLINE void Set(float x, float y) { X = x; Y = y; }
  80. WWINLINE void Set(const Vector2 & v) { X = v.X; Y = v.Y; }
  81. // Array access
  82. WWINLINE float & operator [](int i) { return (&X)[i]; }
  83. WWINLINE const float & operator [](int i) const { return (&X)[i]; }
  84. // normalize, compute length
  85. WWINLINE void Normalize(void);
  86. WWINLINE float Length(void) const;
  87. WWINLINE float Length2(void) const;
  88. // unary operators
  89. WWINLINE Vector2 operator-() const { return Vector2(-X,-Y); }
  90. WWINLINE Vector2 operator+() const { return *this; }
  91. WWINLINE Vector2 & operator += (const Vector2 & v) { X += v.X; Y += v.Y; return *this; }
  92. WWINLINE Vector2 & operator -= (const Vector2 & v) { X -= v.X; Y -= v.Y; return *this; }
  93. WWINLINE Vector2 & operator *= (float k) { X = (float)(X*k); Y=(float)(Y*k); return *this; }
  94. WWINLINE Vector2 & operator /= (float k) { k=1.0f/k; X*=k; Y*=k; return *this; }
  95. // scalar multiplication, division
  96. WWINLINE friend Vector2 operator * (const Vector2 &a,float k);
  97. WWINLINE friend Vector2 operator * (float k,const Vector2 &a);
  98. WWINLINE friend Vector2 operator / (const Vector2 &a,float k);
  99. // vector addition,subtraction
  100. WWINLINE friend Vector2 operator + (const Vector2 &a,const Vector2 &b);
  101. WWINLINE friend Vector2 operator - (const Vector2 &a,const Vector2 &b);
  102. // dot product / inner product
  103. WWINLINE friend float operator * (const Vector2 &a,const Vector2 &b);
  104. static WWINLINE float Dot_Product(const Vector2 &a,const Vector2 &b);
  105. // dot product between a and perpendicular vector to b
  106. static float Perp_Dot_Product(const Vector2 &a,const Vector2 &b);
  107. // Equality operators
  108. friend bool operator == (const Vector2 &a,const Vector2 &b);
  109. friend bool operator != (const Vector2 &a,const Vector2 &b);
  110. WWINLINE friend bool Equal_Within_Epsilon(const Vector2 &a,const Vector2 &b,float epsilon);
  111. // Rotation
  112. WWINLINE void Rotate(float theta);
  113. WWINLINE void Rotate(float s, float c);
  114. WWINLINE bool Rotate_Towards_Vector(Vector2 &target, float max_theta, bool & positive_turn);
  115. WWINLINE bool Rotate_Towards_Vector(Vector2 &target, float max_s, float max_c, bool & positive_turn);
  116. // verify that none of the members of this vector are invalid floats
  117. WWINLINE bool Is_Valid(void) const;
  118. // make this vector the min or max of itself and the passed vector
  119. WWINLINE void Update_Min (const Vector2 & a);
  120. WWINLINE void Update_Max (const Vector2 & a);
  121. WWINLINE void Scale (float a, float b);
  122. WWINLINE void Scale (const Vector2 & a);
  123. static WWINLINE float Distance(const Vector2 &p1, const Vector2 &p2);
  124. static WWINLINE float Quick_Distance(const Vector2 &p1, const Vector2 &p2);
  125. // interpolate between two Vector2's
  126. static void Lerp(const Vector2 & a,const Vector2 & b,float t,Vector2 * set_result);
  127. };
  128. /**************************************************************************
  129. * Scalar Multiply Operator -- Multiply a vector by a scalar *
  130. * *
  131. * INPUT: *
  132. * *
  133. * OUTPUT: *
  134. * *
  135. * WARNINGS: *
  136. * *
  137. * HISTORY: *
  138. * 02/24/1997 GH : Created. *
  139. *========================================================================*/
  140. WWINLINE Vector2 operator * (const Vector2 &a,float k)
  141. {
  142. float a0k(a[0] * k);
  143. float a1k(a[1] * k);
  144. return Vector2(a0k,a1k);
  145. }
  146. WWINLINE Vector2 operator * (float k, const Vector2 &a)
  147. {
  148. float a0k(a[0] * k);
  149. float a1k(a[1] * k);
  150. return Vector2(a0k,a1k);
  151. }
  152. /**************************************************************************
  153. * Scalar Division Operator -- Divide a vector by a scalar *
  154. * *
  155. * INPUT: *
  156. * *
  157. * OUTPUT: *
  158. * *
  159. * WARNINGS: *
  160. * *
  161. * HISTORY: *
  162. *========================================================================*/
  163. WWINLINE Vector2 operator / (const Vector2 &a,float k)
  164. {
  165. float ook=1.0f/k;
  166. float a0ook(a[0] * ook);
  167. float a1ook(a[1] * ook);
  168. return Vector2(a0ook,a1ook);
  169. }
  170. /**************************************************************************
  171. * Vector Addition Operator -- Add two vectors *
  172. * *
  173. * INPUT: *
  174. * *
  175. * OUTPUT: *
  176. * *
  177. * WARNINGS: *
  178. * *
  179. * HISTORY: *
  180. * 02/24/1997 GH : Created. *
  181. *========================================================================*/
  182. WWINLINE Vector2 operator + (const Vector2 &a,const Vector2 &b)
  183. {
  184. return Vector2(
  185. a.X + b.X,
  186. a.Y + b.Y
  187. );
  188. }
  189. /**************************************************************************
  190. * Vector Subtraction Operator -- Subract two vectors *
  191. * *
  192. * INPUT: *
  193. * *
  194. * OUTPUT: *
  195. * *
  196. * WARNINGS: *
  197. * *
  198. * HISTORY: *
  199. * 02/24/1997 GH : Created. *
  200. *========================================================================*/
  201. WWINLINE Vector2 operator - (const Vector2 &a,const Vector2 &b)
  202. {
  203. return Vector2(
  204. a.X - b.X,
  205. a.Y - b.Y
  206. );
  207. }
  208. /**************************************************************************
  209. * Vector Inner Product -- Compute the inner or dot product of two vector *
  210. * *
  211. * INPUT: *
  212. * *
  213. * OUTPUT: *
  214. * *
  215. * WARNINGS: *
  216. * *
  217. * HISTORY: *
  218. *========================================================================*/
  219. WWINLINE float operator * (const Vector2 &a,const Vector2 &b)
  220. {
  221. return a.X*b.X + a.Y*b.Y;
  222. }
  223. WWINLINE float Vector2::Dot_Product(const Vector2 &a,const Vector2 &b)
  224. {
  225. return a*b;
  226. }
  227. WWINLINE float Vector2::Perp_Dot_Product(const Vector2 &a,const Vector2 &b)
  228. {
  229. return a.X * -b.Y + a.Y * b.X;
  230. }
  231. /**************************************************************************
  232. * Vector Equality Operator -- Detemine if two vectors are identical *
  233. * *
  234. * INPUT: *
  235. * *
  236. * OUTPUT: *
  237. * *
  238. * WARNINGS: *
  239. * *
  240. * HISTORY: *
  241. *========================================================================*/
  242. WWINLINE bool operator == (const Vector2 &a,const Vector2 &b)
  243. {
  244. bool a0b0(a[0] == b[0]);
  245. bool a1b1(a[1] == b[1]);
  246. return ( a0b0 & a1b1);
  247. }
  248. /**************************************************************************
  249. * Vector Inequality Operator -- Detemine if two vectors are identical *
  250. * *
  251. * INPUT: *
  252. * *
  253. * OUTPUT: *
  254. * *
  255. * WARNINGS: *
  256. * *
  257. * HISTORY: *
  258. *========================================================================*/
  259. WWINLINE bool operator != (const Vector2 &a,const Vector2 &b)
  260. {
  261. bool a0b0(a[0] != b[0]);
  262. bool a1b1(a[1] != b[1]);
  263. return ( a0b0 | a1b1);
  264. }
  265. /**************************************************************************
  266. * Equal_Within_Epsilon -- Determine if two vectors are identical within e*
  267. * *
  268. * INPUT: *
  269. * *
  270. * OUTPUT: *
  271. * *
  272. * WARNINGS: *
  273. * *
  274. * HISTORY: *
  275. *========================================================================*/
  276. WWINLINE bool Equal_Within_Epsilon(const Vector2 &a,const Vector2 &b,float epsilon)
  277. {
  278. return( (WWMath::Fabs(a.X - b.X) < epsilon) && (WWMath::Fabs(a.Y - b.Y) < epsilon) );
  279. }
  280. /**************************************************************************
  281. * Vector2::Normalize -- Normalizes the vector. *
  282. * *
  283. * INPUT: *
  284. * *
  285. * OUTPUT: *
  286. * *
  287. * WARNINGS: *
  288. * *
  289. * HISTORY: *
  290. *========================================================================*/
  291. WWINLINE void Vector2::Normalize()
  292. {
  293. float len2 = Length2();
  294. if (len2 != 0.0f) {
  295. float oolen = WWMath::Inv_Sqrt(len2);
  296. X *= oolen;
  297. Y *= oolen;
  298. }
  299. }
  300. WWINLINE Vector2 Normalize(const Vector2 & vec)
  301. {
  302. float len2 = vec.Length2();
  303. if (len2 != 0.0f) {
  304. float oolen = WWMath::Inv_Sqrt(len2);
  305. return vec / oolen;
  306. }
  307. return Vector2(0.0f,0.0f);
  308. }
  309. /**************************************************************************
  310. * Vector2::Length -- Returns the length of the vector *
  311. * *
  312. * INPUT: *
  313. * *
  314. * OUTPUT: *
  315. * *
  316. * WARNINGS: *
  317. * *
  318. * HISTORY: *
  319. *========================================================================*/
  320. WWINLINE float Vector2::Length() const
  321. {
  322. return (float)WWMath::Sqrt(Length2());
  323. }
  324. /**************************************************************************
  325. * Vector2::Length -- Returns the square of the length of the vector *
  326. * *
  327. * INPUT: *
  328. * *
  329. * OUTPUT: *
  330. * *
  331. * WARNINGS: *
  332. * *
  333. * HISTORY: *
  334. *========================================================================*/
  335. WWINLINE float Vector2::Length2() const
  336. {
  337. return (X*X + Y*Y);
  338. }
  339. /**************************************************************************
  340. * Vector2::Rotate -- Rotate vector *
  341. * *
  342. * INPUT: *
  343. * float theta - angle to rotate *
  344. * *
  345. * OUTPUT: *
  346. * *
  347. * WARNINGS: *
  348. * *
  349. * HISTORY: *
  350. *========================================================================*/
  351. WWINLINE void Vector2::Rotate(float theta)
  352. {
  353. Rotate(WWMath::Sin(theta), WWMath::Cos(theta));
  354. }
  355. /**************************************************************************
  356. * Vector2::Rotate -- Rotate vector *
  357. * *
  358. * INPUT: *
  359. * s - sine of the angle *
  360. * c - cosine of the angle *
  361. * *
  362. * OUTPUT: *
  363. * *
  364. * WARNINGS: *
  365. * *
  366. * HISTORY: *
  367. *========================================================================*/
  368. WWINLINE void Vector2::Rotate(float s, float c)
  369. {
  370. float new_x = X * c + Y * -s;
  371. float new_y = X * s + Y * c;
  372. X = new_x;
  373. Y = new_y;
  374. }
  375. /**************************************************************************
  376. * Vector2::Rotate -- Rotate towards given vector (stop on reaching it) *
  377. * *
  378. * INPUT: *
  379. * float theta - angle to rotate *
  380. * *
  381. * OUTPUT: *
  382. * bool - true if we have reached the desired vector *
  383. * *
  384. * WARNINGS: *
  385. * This function assumes both vectors are normalized! *
  386. * *
  387. * HISTORY: *
  388. *========================================================================*/
  389. WWINLINE bool Vector2::Rotate_Towards_Vector(Vector2 &target, float max_theta, bool & positive_turn)
  390. {
  391. return Rotate_Towards_Vector(target, WWMath::Sin(max_theta), WWMath::Cos(max_theta), positive_turn);
  392. }
  393. /**************************************************************************
  394. * Vector2::Rotate -- Rotate towards given vector (stop on reaching it) *
  395. * *
  396. * INPUT: *
  397. * s - sine of the angle *
  398. * c - cosine of the angle *
  399. * *
  400. * OUTPUT: *
  401. * bool - true if we have reached the desired vector *
  402. * *
  403. * WARNINGS: *
  404. * This function assumes both vectors are normalized! *
  405. * *
  406. * HISTORY: *
  407. *========================================================================*/
  408. WWINLINE bool Vector2::Rotate_Towards_Vector(Vector2 &target, float max_s, float max_c, bool & positive_turn)
  409. {
  410. bool return_value = false;
  411. positive_turn = Vector2::Perp_Dot_Product(target, *this) > 0.0f;
  412. if (Vector2::Dot_Product(*this, target) >= max_c) {
  413. Set(target);
  414. return_value = true;
  415. } else {
  416. // Determine turn direction and rotate accordingly.
  417. if (positive_turn) {
  418. Rotate(max_s, max_c);
  419. } else {
  420. Rotate(-max_s, max_c);
  421. }
  422. }
  423. return return_value;
  424. }
  425. /***********************************************************************************************
  426. * Swap -- swap two Vector2's *
  427. * *
  428. * INPUT: *
  429. * *
  430. * OUTPUT: *
  431. * *
  432. * WARNINGS: *
  433. * *
  434. * HISTORY: *
  435. * 08/11/1997 GH : Created. *
  436. *=============================================================================================*/
  437. WWINLINE void Swap(Vector2 & a,Vector2 & b)
  438. {
  439. Vector2 tmp(a);
  440. a = b;
  441. b = tmp;
  442. }
  443. /***********************************************************************************************
  444. * Vector2::Is_Valid -- Verifies that all components are valid floats *
  445. * *
  446. * INPUT: *
  447. * *
  448. * OUTPUT: *
  449. * *
  450. * WARNINGS: *
  451. * *
  452. * HISTORY: *
  453. * 10/18/99 gth : Created. *
  454. *=============================================================================================*/
  455. WWINLINE bool Vector2::Is_Valid(void) const
  456. {
  457. return (WWMath::Is_Valid_Float(X) && WWMath::Is_Valid_Float(Y));
  458. }
  459. /***********************************************************************************************
  460. * Vector2::Update_Min -- Set each component of the vector to the min of this and a. *
  461. * *
  462. * INPUT: *
  463. * *
  464. * OUTPUT: *
  465. * *
  466. * WARNINGS: *
  467. * *
  468. * HISTORY: *
  469. * 06/12/00 IML : Created. *
  470. *=============================================================================================*/
  471. WWINLINE void Vector2::Update_Min (const Vector2 & a)
  472. {
  473. if (a.X < X) X = a.X;
  474. if (a.Y < Y) Y = a.Y;
  475. }
  476. /***********************************************************************************************
  477. * Vector2::Update_Max -- Set each component of the vector to the max of this and a. *
  478. * *
  479. * INPUT: *
  480. * *
  481. * OUTPUT: *
  482. * *
  483. * WARNINGS: *
  484. * *
  485. * HISTORY: *
  486. * 06/12/00 IML : Created. *
  487. *=============================================================================================*/
  488. WWINLINE void Vector2::Update_Max (const Vector2 & a)
  489. {
  490. if (a.X > X) X = a.X;
  491. if (a.Y > Y) Y = a.Y;
  492. }
  493. /***********************************************************************************************
  494. * Vector2::Scale -- multiply components of a vector by independant scaling factors. *
  495. * *
  496. * INPUT: *
  497. * *
  498. * OUTPUT: *
  499. * *
  500. * WARNINGS: *
  501. * *
  502. * HISTORY: *
  503. * 06/19/2000 IML : Created. *
  504. *=============================================================================================*/
  505. WWINLINE void Vector2::Scale (float a, float b)
  506. {
  507. X *= a;
  508. Y *= b;
  509. }
  510. /***********************************************************************************************
  511. * Vector2::Scale -- multiply components of a vector by independant scaling factors. *
  512. * *
  513. * INPUT: *
  514. * *
  515. * OUTPUT: *
  516. * *
  517. * WARNINGS: *
  518. * *
  519. * HISTORY: *
  520. * 06/19/2000 IML : Created. *
  521. *=============================================================================================*/
  522. WWINLINE void Vector2::Scale (const Vector2 & a)
  523. {
  524. X *= a.X;
  525. Y *= a.Y;
  526. }
  527. /***********************************************************************************************
  528. * Quick_Distance -- Fast but inaccurate 2D distance calculation. *
  529. * *
  530. * *
  531. * *
  532. * *
  533. * HISTORY: *
  534. * 11/29/1999MLL: Created. *
  535. *=============================================================================================*/
  536. WWINLINE float Quick_Distance(float x1, float y1, float x2, float y2)
  537. {
  538. // From Graphic Gems I.
  539. float x_diff = x1 - x2;
  540. float y_diff = y1 - y2;
  541. WWMath::Fabs(x_diff);
  542. WWMath::Fabs(y_diff);
  543. if (x_diff > y_diff)
  544. {
  545. return ((y_diff / 2) + x_diff);
  546. }
  547. else
  548. {
  549. return ((x_diff / 2) + y_diff);
  550. }
  551. }
  552. WWINLINE float Vector2::Quick_Distance(const Vector2 &a, const Vector2 &b)
  553. {
  554. return ::Quick_Distance(a.X, a.Y, b.X, b.Y);
  555. }
  556. /***********************************************************************************************
  557. * Distance -- Accurate distance 2D calculation. *
  558. * *
  559. * *
  560. * *
  561. * *
  562. * HISTORY: *
  563. * 11/29/1999MLL: Created. *
  564. *=============================================================================================*/
  565. WWINLINE float Vector2::Distance(const Vector2 &a, const Vector2 &b)
  566. {
  567. Vector2 temp;
  568. temp = a - b;
  569. return (temp.Length());
  570. }
  571. WWINLINE float Distance(float x1, float y1, float x2, float y2)
  572. {
  573. float x_diff = x1 - x2;
  574. float y_diff = y1 - y2;
  575. return (WWMath::Sqrt((x_diff * x_diff) + (y_diff * y_diff)));
  576. }
  577. /***********************************************************************************************
  578. * Vector2::Lerp -- linearly interpolates two Vector2's *
  579. * *
  580. * INPUT: *
  581. * *
  582. * OUTPUT: *
  583. * *
  584. * WARNINGS: *
  585. * *
  586. * HISTORY: *
  587. * 4/14/2000 gth : Created. *
  588. *=============================================================================================*/
  589. WWINLINE void Vector2::Lerp(const Vector2 & a,const Vector2 & b,float t,Vector2 * set_result)
  590. {
  591. assert(set_result != NULL);
  592. set_result->X = (a.X + (b.X - a.X)*t);
  593. set_result->Y = (a.Y + (b.Y - a.Y)*t);
  594. }
  595. #endif /* VECTOR2_H */