IMKYZABITCHES

Vector3 - Bitwise

May 22nd, 2014
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.74 KB | None | 0 0
  1. //vec.h
  2. #include <stdio.h>
  3. /*################################################################################
  4.  #                  _          _           _                _                    #
  5.  #                 /\ \    _ / /\         /\ \            /\ \                   #
  6.  #                 \ \ \  /_/ / /        /  \ \          /  \ \                  #
  7.  #                  \ \ \ \___\/        / /\ \ \        / /\ \ \                 #
  8.  #                  / / /  \ \ \       / / /\ \_\      / / /\ \ \                #
  9.  #                  \ \ \   \_\ \     / /_/_ \/_/     / / /  \ \_\               #
  10.  #                   \ \ \  / / /    / /____/\       / / /    \/_/               #
  11.  #                    \ \ \/ / /    / /\____\/      / / /                        #
  12.  #                     \ \ \/ /    / / /______     / / /________                 #
  13.  #                      \ \  /    / / /_______\   / / /_________\                #
  14.  #                       \_\/     \/__________/   \/____________/                #
  15.  #                                                                               #
  16.  #                                                                               #
  17.  #          @@@@@@@\  @@\   @@\                   @@\                            # 
  18.  #          @@  __@@\ \__|  @@ |                  \__|                           #
  19.  #          @@ |  @@ |@@\ @@@@@@\   @@\  @@\  @@\ @@\  @@@@@@@\  @@@@@@\         #
  20.  #  @@@@@@\ @@@@@@@\ |@@ |\_@@  _|  @@ | @@ | @@ |@@ |@@  _____|@@  __@@\        #
  21.  #  \______|@@  __@@\ @@ |  @@ |    @@ | @@ | @@ |@@ |\@@@@@@\  @@@@@@@@ |       #
  22.  #          @@ |  @@ |@@ |  @@ |@@\ @@ | @@ | @@ |@@ | \____@@\ @@   ____|       #
  23.  #          @@@@@@@  |@@ |  \@@@@  |\@@@@@\@@@@  |@@ |@@@@@@@  |\@@@@@@@\        #
  24.  #          \_______/ \__|   \____/  \_____\____/ \__|\_______/  \_______|       #
  25.  #                                                                               #
  26.  #################################################################################*/
  27. typedef enum { X, Y, Z }vect0r3;
  28.  
  29. class Vector3
  30. {
  31.     public:
  32.         float x, y, z;
  33.         ~Vector3( );
  34.         void Add ( vect0r3 xyz, float value, Vector3&a ),
  35.             Sub ( vect0r3 xyz, float value, Vector3&a ),
  36.             Sub ( Vector3&s ),
  37.             Copy ( vect0r3 xyz, float out ),
  38.             Copy ( Vector3&out ),
  39.             Combine ( Vector3 &c ),
  40.             Combine ( vect0r3 xyz, float value ),
  41.             OutputXYZ ( const char*title, const char*colorHex ),
  42.             Insert ( vect0r3 xyz, float value ),
  43.             Insert ( Vector3&value ),
  44.             Insert ( float xx, float yy, float zz ),
  45.             Null ( ),
  46.             //Memory
  47.             SetVector3 ( int address );
  48.         Vector3 AnglesToForward ( ),
  49.             VectorToAngles ( ),
  50.             Scale ( float scale ),
  51.             Inverse ( ),
  52.             Snap ( ),
  53.             Normalize ( ),
  54.             Polar ( float radius,  float theta, float phi);
  55.         float Distance ( Vector3&vec ),
  56.             Length ( ),
  57.             Dot ( Vector3&vec ),
  58.             Dot ( ),
  59.             Normalize ( Vector3&n );
  60.         bool Equal ( Vector3&e ),
  61.             Equal ( vect0r3 xyz, float value ),
  62.             Compare ( Vector3&vec );
  63. };
  64. //vec.cpp
  65. #include "vec.h"
  66. #include <math.h>
  67. #include <stdio.h>
  68.  
  69. inline float Vector3::Normalize ( Vector3&n )
  70. {
  71.     if(!sqrt(x*x+y*y+z*z)){n.Null(); return 0.00;}
  72.     n.x = (x*(1.0/sqrt(x*x+y*y+z*z)));
  73.     n.y = (y*(1.0/sqrt(x*x+y*y+z*z)));
  74.     n.z = (z*(1.0/sqrt(x*x+y*y+z*z)));
  75.     return sqrt(x*x+y*y+z*z);
  76. }
  77.  
  78. class BulletTrace
  79. {
  80. public:
  81.     float fration;
  82.     Vector3 position, normal, unknown;
  83.     int gentity;
  84.     char*surfaceName;
  85. };
  86.  
  87. BulletTrace bulletTrace(Vector3&start, Vector3&end, bool hitCharacters, int entity)
  88. {
  89.     trace_t*trace; BulletTrace tracee; Vector3 vec; vec.Insert((end.x-start.x), (end.y-start.y), (end.z-start.z));
  90.    float*startX[3] = { &start.x, &start.y, &start.z }, *endX[3] = { &end.x, &end.y, &end.z };
  91.    G_LocationalTrace(trace, *startX, *endX, *(int*)entity, (hitCharacters ? 0x280E831 : 0x80A831), 0);
  92.    tracee.fration = trace->fraction;
  93.    for(int i = 0; i <= 2; i++)
  94.      (&tracee.position.x)[i] = ((endX[i]-startX[i]) * trace->fration) + *startX[i];
  95.    tracee.gentity = getEntity(trace->hitId);
  96.    for(int i = 0; i < 3; i++) (&tracee.normal.x)[i] = trace->normal[i];
  97.    tracee.unknown = vec.Normalize();
  98.    int math = (0xF0000000 + trace->surfaceFlags >> 0x14);
  99.    tracee.surfaceName = Com_SurfaceTypeToName(math & 0x1F);
  100.    return tracee;
  101. }
  102. inline Vector3 Vector3::Normalize ( )
  103. {
  104.     Vector3 vec;
  105.     if(!sqrt(x*x+y*y+z*z)){vec.Null(); return vec;}
  106.     vec.x = (x*(1.0/sqrt(x*x+y*y+z*z)));
  107.     vec.y = (y*(1.0/sqrt(x*x+y*y+z*z)));
  108.     vec.z = (z*(1.0/sqrt(x*x+y*y+z*z)));
  109.     return vec;
  110. }
  111.  
  112. inline Vector3 GetVector3 ( int address )
  113. {
  114.     Vector3 vec;
  115.     vec.Insert(*(float*)(address), *(float*)(address+4), *(float*)(address+8));
  116.     return vec;
  117. }
  118.  
  119. inline Vector3 Vector3::Snap ( )
  120. {
  121.     Vector3 vec;
  122.     vec.Insert(floor(x+0.5),floor(y+0.5),floor(z+0.5));
  123.     return vec;
  124. }
  125.  
  126. inline Vector3 Vector3::Polar(float radius, float theta, float phi)
  127. {
  128.     Vector3 vec;
  129.     vec.Insert(radius*cos(theta)*cos(phi), radius*sin(theta)*cos(phi), radius*sin(phi));
  130.     return vec;
  131. }
  132. inline void Vector3::SetVector3 ( int address )
  133. {
  134.     for(int i = 0; i < 3; i++) for(int f = 0; f <= 0xC; f += 0x4) *(float*)(address+f)=(&x)[i];
  135. }
  136.  
  137. inline void Vector3::Null ( )
  138. {
  139.     x,y,z=0;
  140. }
  141.  
  142. inline Vector3 Vector3::Inverse ( )
  143. {
  144.     Vector3 vec;vec.Insert(-x,-y,-z);return vec;
  145. }
  146.  
  147. inline void Vector3::Insert ( float xx, float yy, float zz )
  148. {
  149.     x=xx,y=yy,z=zz;
  150. }
  151.  
  152. inline Vector3 Vector3::Scale ( float scale )
  153. {
  154.     Vector3 vec;
  155.     for(int i = 0; i <= 2; i++) (&vec.x)[i] = ((&x)[i] * scale);
  156.     return vec;
  157. }
  158.  
  159. inline float Vector3::Dot ( Vector3&vec )
  160. {
  161.     return (x*vec.x + y*vec.y + z*vec.z);
  162. }
  163.  
  164. inline float Vector3::Dot ( )
  165. {
  166.     return (x*x+y*y+z*z);
  167. }
  168.  
  169. inline float Vector3::Distance ( Vector3&vec )
  170. {
  171.     Sub(vec);
  172.     return Length();
  173. }
  174.  
  175. inline void Vector3::Insert( vect0r3 xyz, float value )
  176. {
  177.     (&x)[xyz] = value;
  178. }
  179.  
  180. inline void Vector3::Insert( Vector3&value )
  181. {
  182.     for(int i = 0; i <= 2; i++)
  183.         (&x)[i] = (&value.x)[i];
  184. }
  185.  
  186. inline float Vector3::Length ( )
  187. {
  188.     return (float)sqrt((x*x+y*y+z*z));
  189. }
  190.  
  191. inline Vector3::~Vector3()
  192. {
  193.     x = 0, y = 0, z = 0;
  194. }
  195.  
  196. inline void Vector3::Add ( vect0r3 xyz, float value, Vector3&a=*(Vector3*)0 )
  197. {
  198.     (&x)[xyz] += (!value ? (&a.x)[xyz] : value);
  199. }
  200.  
  201. inline void Vector3::Sub ( vect0r3 xyz, float value, Vector3&s=*(Vector3*)0 )
  202. {
  203.     (&x)[xyz] -= (!value ? (&s.x)[xyz] : value);
  204. }
  205.  
  206. inline void Vector3::Combine ( Vector3 &c )
  207. {
  208.     for(int i = 0; i <= 2; i++)
  209.         (&x)[i] += (&c.x)[i];
  210. }
  211.  
  212. inline void Vector3::Combine ( vect0r3 xyz, float value )
  213. {
  214.     (&x)[xyz] += value;
  215. }
  216.  
  217. inline void Vector3::OutputXYZ ( const char*title="Vec3 Origins", const char*colorHex="a" )//Note this should only be used with CLI apps, unless you wanna comment out line 0-2
  218. {
  219.     //char buffer[0x20];
  220.     //sprintf(buffer, "color %s", colorHex);
  221.     //system(buffer);
  222.     printf("======= %s ========\n\tX: %f\n\tY: %f\n\tZ: %f\n\n", title, x, y, z);
  223. }
  224.  
  225. inline Vector3 Vector3::AnglesToForward ( )
  226. {
  227.     Vector3 vec;
  228.     vec.x = (cos(x*(3.14159265358979323846*2 / 360))*cos(y*(3.14159265358979323846*2 / 360)));
  229.     vec.y = (cos(x*(3.14159265358979323846*2 / 360))*sin(y*(3.14159265358979323846*2 / 360)));
  230.     vec.z = -sin(x*(3.14159265358979323846*2 / 360));
  231.     return vec;
  232. }
  233.  
  234. inline Vector3 Vector3::AnglesToRight ( )
  235. {
  236.     Vector3 vec;
  237.     vec.x = (-1*sin(z*(3.14159265358979323846*2 / 360))*sin(x*(3.14159265358979323846*2 / 360))*cos(y*(3.14159265358979323846*2 / 360))+-1*cos(z*(3.14159265358979323846*2 / 360))*-sin(x*(3.14159265358979323846*2 / 360)));
  238.     vec.y = (-1*sin(z*(3.14159265358979323846*2 / 360))*sin(x*(3.14159265358979323846*2 / 360))*sin(y)+-1*cos(z*(3.14159265358979323846*2 / 360))*cos(x*(3.14159265358979323846*2 / 360)));
  239.     vec.z = (-1*sin(z*(3.14159265358979323846*2 / 360))*cos(x*(3.14159265358979323846*2 / 360));
  240.     return vec;
  241. }
  242.  
  243. inline Vector3 Vector3::AnglesToUp ( )
  244. {
  245.     Vector3 vec;
  246.     vec.x = (cos(z*(3.14159265358979323846*2 / 360))*sin(x*(3.14159265358979323846*2 / 360))*cos(y*(3.14159265358979323846*2 / 360))+-sin(z*(3.14159265358979323846*2 / 360))*-sin(y*(3.14159265358979323846*2 / 360)));
  247.     vec.y = (cos(z**(3.14159265358979323846*2 / 360))*sin(x*(3.14159265358979323846*2 / 360))*sin(y*(3.14159265358979323846*2 / 360))+-sin(z*(3.14159265358979323846*2 / 360))*cos(y*(3.14159265358979323846*2 / 360)));
  248.     vec.z = (cos(z*(3.14159265358979323846*2 / 360))*cos(x*(3.14159265358979323846*2 / 360)));
  249.     return vec;
  250. }
  251.  
  252. inline Vector3 Vector3::VectorToAngles ( )
  253. {
  254.     Vector3 vec3;
  255.     float yaw, pitch;
  256.     if(!x && !y)
  257.     {
  258.         yaw = 0;
  259.         if(z > 0) pitch = 90; else pitch = 270;
  260.     }
  261.     else
  262.     {
  263.         yaw = atan2(y,z)*180/3.14159265358979323846;
  264.         if(yaw<0)yaw+=360;
  265.         pitch= atan2(z,sqrt(x*x+y*y)) * 180/3.14159265358979323846;
  266.         if(pitch<0)pitch+=360;
  267.     }
  268.     vec3.x = pitch;
  269.     vec3.y = yaw;
  270.     vec3.z = 0;
  271.     return vec3;
  272. }
  273.  
  274. inline bool Vector3::Equal ( Vector3&e )
  275. {
  276.     return (x==e.x&&y==e.y&&z==e.z);
  277. }
  278.  
  279. inline bool Vector3::Equal ( vect0r3 xyz, float value )
  280. {
  281.     return ((&x)[xyz]==value);
  282. }
  283.  
  284. inline bool Vector3::Compare ( Vector3&vec )
  285. {
  286.     for(int i = 0; i < 3; i++)
  287.         return (fabs((&x)[i]-(&vec.x)[i]) > 0.001);
  288. }
Add Comment
Please, Sign In to add comment