Clone of PhatAC @ https://github.com/floaterxk/PhatAC

Math.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #pragma once
  2. #include <math.h>
  3. #pragma pack(push, 1)
  4. typedef struct loc_s
  5. {
  6. loc_s() {
  7. landcell = 0;
  8. x = 0;
  9. y = 0;
  10. z = 0;
  11. }
  12. loc_s(DWORD _landcell, float _x, float _y, float _z) {
  13. landcell = _landcell;
  14. x = _x;
  15. y = _y;
  16. z = _z;
  17. }
  18. //inline Vector2D operator/(vec_t fl) const { return Vector2D(x/fl, y/fl); }
  19. DWORD landcell;
  20. float x;
  21. float y;
  22. float z;
  23. } loc_t;
  24. #pragma pack(pop)
  25. inline bool operator !(const loc_t &v) {
  26. return !v.landcell ? true : false;
  27. }
  28. #pragma pack(push, 1)
  29. typedef struct heading_s
  30. {
  31. heading_s() {
  32. w = 1;
  33. x = 0;
  34. y = 0;
  35. z = 0;
  36. }
  37. heading_s(float _w, float _x, float _y, float _z) {
  38. w = _w;
  39. x = _x;
  40. y = _y;
  41. z = _z;
  42. }
  43. float w;
  44. float x;
  45. float y;
  46. float z;
  47. } heading_t;
  48. #pragma pack(pop)
  49. typedef struct placement_s
  50. {
  51. loc_t origin;
  52. heading_t angles;
  53. } placement_t;
  54. extern DWORD RandomDWORD(DWORD min, DWORD max);
  55. extern long RandomLong(long min, long max);
  56. extern double RandomDouble(double min, double max);
  57. extern float RandomFloat(float min, float max);
  58. extern BOOL WillOF(unsigned long value_one, unsigned long value_two);
  59. //Credits to Valve for a lot of this.
  60. typedef double vec_t;
  61. enum {
  62. PITCH = 0,
  63. YAW,
  64. ROLL
  65. };
  66. class Vector2D;
  67. class Vector;
  68. #define PI 3.1415926535897f
  69. #define RAD2DEG( x ) ( (x) * (180.f / PI) )
  70. #define DEG2RAD( x ) ( (x) * (PI / 180.f) )
  71. class matrix {
  72. public:
  73. matrix();
  74. void define(float xa, float xb, float xc, float xd, float ya, float yb, float yc, float yd,
  75. float za, float zb, float zc, float zd);
  76. void defineByRotation(float roll, float pitch, float yaw);
  77. void defineByQuaternion(float qw, float qx, float qy, float qz);
  78. void applyRotation(float roll, float pitch, float yaw);
  79. void applyTranslation(float x, float y, float z);
  80. void applyToVector(Vector &vect);
  81. void multiply(matrix second);
  82. void copy(matrix &dest);
  83. float data[4][4];
  84. };
  85. class Vector2D
  86. {
  87. public:
  88. inline Vector2D(void) { }
  89. inline Vector2D(vec_t X, vec_t Y) { x = X; y = Y; }
  90. inline Vector2D operator+(const Vector2D& v) const { return Vector2D(x + v.x, y + v.y); }
  91. inline Vector2D operator-(const Vector2D& v) const { return Vector2D(x - v.x, y - v.y); }
  92. inline Vector2D operator*(vec_t fl) const { return Vector2D(x*fl, y*fl); }
  93. inline Vector2D operator/(vec_t fl) const { return Vector2D(x / fl, y / fl); }
  94. inline vec_t Length(void) const { return sqrt(x*x + y*y); }
  95. inline Vector2D Normalize(void) const
  96. {
  97. Vector2D vec2;
  98. vec_t flLen = Length();
  99. if (flLen == 0)
  100. {
  101. return Vector2D(0, 0);
  102. }
  103. else
  104. {
  105. flLen = 1 / flLen;
  106. return Vector2D(x * flLen, y * flLen);
  107. }
  108. }
  109. vec_t x, y;
  110. };
  111. inline vec_t DotProduct(const Vector2D& a, const Vector2D& b)
  112. {
  113. return(a.x*b.x + a.y*b.y);
  114. }
  115. inline Vector2D operator*(vec_t fl, const Vector2D& v)
  116. {
  117. return v * fl;
  118. }
  119. class Vector
  120. {
  121. public:
  122. // Construction/destruction
  123. inline Vector(void) { }
  124. inline Vector(vec_t X, vec_t Y, vec_t Z) { x = X; y = Y; z = Z; }
  125. //inline Vector(double X, double Y, double Z) { x = (float)X; y = (float)Y; z = (float)Z; }
  126. //inline Vector(int X, int Y, int Z) { x = (float)X; y = (float)Y; z = (float)Z; }
  127. inline Vector(const Vector& v) { x = v.x; y = v.y; z = v.z; }
  128. inline Vector(vec_t rgfl[3]) { x = rgfl[0]; y = rgfl[1]; z = rgfl[2]; }
  129. inline Vector(loc_t origin)
  130. {
  131. x = ((origin.landcell >> 24) * 192.0f) + origin.x;
  132. y = (((origin.landcell >> 16) & 0xFF) * 192.0f) + origin.y;
  133. z = origin.z;
  134. }
  135. inline Vector(float X, float Y, float Z, float W)
  136. {
  137. //matrix poop;
  138. //poop.defineByQuaternion( X, Y, Z, W );
  139. //poop.applyToVector(this);
  140. }
  141. // Operators
  142. inline Vector operator-(void) const { return Vector(-x, -y, -z); }
  143. inline int operator==(const Vector& v) const { return x == v.x && y == v.y && z == v.z; }
  144. inline int operator!=(const Vector& v) const { return !(*this == v); }
  145. inline Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
  146. inline Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
  147. inline Vector operator*(vec_t fl) const { return Vector(x*fl, y*fl, z*fl); }
  148. inline Vector operator/(vec_t fl) const { return Vector(x / fl, y / fl, z / fl); }
  149. // Methods
  150. inline void CopyToArray(vec_t* rgfl) const { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; }
  151. inline vec_t Length(void) const { return sqrt(x*x + y*y + z*z); }
  152. operator vec_t *() { return &x; } // Vectors will now automatically convert to float * when needed
  153. operator const vec_t *() const { return &x; } // Vectors will now automatically convert to float * when needed
  154. inline Vector Normalize(void) const
  155. {
  156. vec_t flLen = Length();
  157. if (flLen == 0) return Vector(0, 0, 1); // ????
  158. flLen = 1 / flLen;
  159. return Vector(x * flLen, y * flLen, z * flLen);
  160. }
  161. inline Vector2D Make2D(void) const
  162. {
  163. Vector2D Vec2;
  164. Vec2.x = x;
  165. Vec2.y = y;
  166. return Vec2;
  167. }
  168. inline vec_t Length2D(void) const { return sqrt(x*x + y*y); }
  169. // Members
  170. vec_t x, y, z;
  171. };
  172. inline Vector operator*(vec_t fl, const Vector& v)
  173. {
  174. return v * fl;
  175. }
  176. inline vec_t DotProduct(const Vector& a, const Vector& b)
  177. {
  178. return(a.x*b.x + a.y*b.y + a.z*b.z);
  179. }
  180. inline Vector CrossProduct(const Vector& a, const Vector& b)
  181. {
  182. return Vector(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
  183. }
  184. float FindVectorZ(const Vector& p1, const Vector& p2, const Vector& p3, float x, float y);
  185. void inline SinCos(float radians, float *sine, float *cosine)
  186. {
  187. #ifdef _WIN32
  188. _asm
  189. {
  190. fld DWORD PTR[radians]
  191. fsincos
  192. mov edx, DWORD PTR[cosine]
  193. mov eax, DWORD PTR[sine]
  194. fstp DWORD PTR[edx]
  195. fstp DWORD PTR[eax]
  196. }
  197. #elif _LINUX
  198. register double __cosr, __sinr;
  199. __asm __volatile__
  200. ("fsincos"
  201. : "=t" (__cosr), "=u" (__sinr) : "0" (radians));
  202. *sine = __sinr;
  203. *cosine = __cosr;
  204. #endif
  205. }
  206. inline void AngleVectors(const Vector &angles, Vector *forward)
  207. {
  208. float sp, sy, cp, cy;
  209. SinCos((float)DEG2RAD(angles[YAW]), &sy, &cy);
  210. SinCos((float)DEG2RAD(angles[PITCH]), &sp, &cp);
  211. forward->x = cp*cy;
  212. forward->y = cp*sy;
  213. forward->z = -sp;
  214. }