Advertisement
Guest User

[External Base] CMath.h (by aVitamin)

a guest
Jan 9th, 2013
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <windows.h>
  4. #include <math.h>
  5.  
  6. #ifndef M_PI
  7. #define M_PI        3.14159265358979323846f // matches value in gcc v2 math.h
  8. #endif
  9.  
  10. #define M_RADPI 57.295779513082f
  11.  
  12. #define DotProduct(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1]+(x)[2]*(y)[2])
  13.  
  14. #define VectorSubtract(a,b,c) {(c)[0]=(a)[0]-(b)[0];(c)[1]=(a)[1]-(b)[1];(c)[2]=(a)[2]-(b)[2];}
  15.  
  16. extern bool g_bIsInFullScreen;
  17.  
  18. class CMath
  19. {
  20. public:
  21.     float VectorLength(float *v)
  22.     {
  23.         return (float)sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
  24.     }
  25.     float VectorAngle(float *a, float *b)
  26.     {
  27.         float length_a = VectorLength(a);
  28.         float length_b = VectorLength(b);
  29.         float length_ab = length_a*length_b;
  30.         if( length_ab==0.0 ){ return 0.0; }
  31.         else
  32.             return (float) (acos(DotProduct(a,b)/length_ab) * (180.f/M_PI));
  33.     }
  34.     void AngleVectors( float *angles, float *forward, float *right, float *up )
  35.     {
  36.         float angle;
  37.         static float sp, sy, cp, cy;
  38.  
  39.         angle = angles[0] * ( M_PI / 180 );
  40.         sp = sin( angle );
  41.         cp = cos( angle );
  42.  
  43.         angle = angles[1] * ( M_PI / 180 );
  44.         sy = sin( angle );
  45.         cy = cos( angle );
  46.  
  47.         if( forward ) {
  48.             forward[0] = cp*cy;
  49.             forward[1] = cp*sy;
  50.             forward[2] = -sp;
  51.         }
  52.         if( right || up ) {
  53.             static float sr, cr;
  54.  
  55.             angle = angles[2] * ( M_PI / 180 );
  56.             sr = sin( angle );
  57.             cr = cos( angle );
  58.  
  59.             if( right ) {
  60.                 right[0] = -1*sr*sp*cy+-1*cr*-sy;
  61.                 right[1] = -1*sr*sp*sy+-1*cr*cy;
  62.                 right[2] = -1*sr*cp;
  63.             }
  64.             if( up ) {
  65.                 up[0] = cr*sp*cy+-sr*-sy;
  66.                 up[1] = cr*sp*sy+-sr*cy;
  67.                 up[2] = cr*cp;
  68.             }
  69.         }
  70.     }
  71.     float GetDistance( float *vecA, float *vecB )
  72.     {
  73.         float diff[3] = { vecB[0] - vecA[0], vecB[1] - vecA[1], vecB[2] - vecA[2] };
  74.         return (float)( sqrt( ( diff[0] * diff[0] ) + ( diff[1] * diff[1] ) + ( diff[2] * diff[2] ) ) );
  75.     }
  76.     void MakeVector(float *pfIn, float *pfOut)
  77.     {
  78.         float pitch;
  79.         float yaw;
  80.         float tmp;     
  81.    
  82.         pitch = (float) (pfIn[0] * M_PI/180);
  83.         yaw = (float) (pfIn[1] * M_PI/180);
  84.         tmp = (float) cos(pitch);
  85.    
  86.         pfOut[0] = (float) (-tmp * -cos(yaw));
  87.         pfOut[1] = (float) (sin(yaw)*tmp);
  88.         pfOut[2] = (float) -sin(pitch);
  89.     }
  90.     void VectorRotateX(float *pfIn, float fAngle, float *pfOut)
  91.     {
  92.         float a,c,s;
  93.  
  94.         a = (float) (fAngle * M_PI/180);
  95.         c = (float) cos(a);
  96.         s = (float) sin(a);
  97.         pfOut[0] = pfIn[0];
  98.         pfOut[1] = c*pfIn[1] - s*pfIn[2];
  99.         pfOut[2] = s*pfIn[1] + c*pfIn[2];  
  100.     }
  101.     void VectorRotateY(float *pfIn, float fAngle, float *pfOut)
  102.     {
  103.         float a,c,s;
  104.  
  105.         a = (float) (fAngle * M_PI/180);
  106.         c = (float) cos(a);
  107.         s = (float) sin(a);
  108.         pfOut[0] = c*pfIn[0] + s*pfIn[2];
  109.         pfOut[1] = pfIn[1];
  110.         pfOut[2] = -s*pfIn[0] + c*pfIn[2];
  111.     }
  112.     void VectorRotateZ(float *pfIn, float fAngle, float *pfOut)
  113.     {
  114.         float a,c,s;
  115.  
  116.         a = (float) (fAngle * M_PI/180);
  117.         c = (float) cos(a);
  118.         s = (float) sin(a);
  119.         pfOut[0] = c*pfIn[0] - s*pfIn[1];
  120.         pfOut[1] = s*pfIn[0] + c*pfIn[1];
  121.         pfOut[2] = pfIn[2];
  122.     }
  123.     void CalcAngle( float *src, float *dst, float *angles )
  124.     {
  125.         double delta[3] = { (src[0]-dst[0]), (src[1]-dst[1]), (src[2]-dst[2]) };
  126.         double hyp = sqrt(delta[0]*delta[0] + delta[1]*delta[1]);
  127.  
  128.         angles[0] = (float) (atan(delta[2]/hyp) * M_RADPI);
  129.         angles[1] = (float) (atan(delta[1]/delta[0]) * M_RADPI);
  130.         angles[2] = 0.0f;
  131.  
  132.         if(delta[0] >= 0.0) { angles[1] += 180.0f; }
  133.     }
  134.     float GetFOV( float *angle, float *src, float *dst)
  135.     {
  136.         float ang[3],aim[3];
  137.         float fov;
  138.  
  139.         CalcAngle(src, dst, ang);
  140.         MakeVector(angle, aim);
  141.         MakeVector(ang, ang);      
  142.  
  143.         float mag_s = sqrt((aim[0]*aim[0]) + (aim[1]*aim[1]) + (aim[2]*aim[2]));
  144.         float mag_d = sqrt((aim[0]*aim[0]) + (aim[1]*aim[1]) + (aim[2]*aim[2]));
  145.  
  146.         float u_dot_v = aim[0]*ang[0] + aim[1]*ang[1] + aim[2]*ang[2];
  147.  
  148.         fov = acos(u_dot_v / (mag_s*mag_d)) * (180.0f / M_PI);
  149.  
  150.         return fov;
  151.     }
  152.     bool WorldToScreen(float *pfIn, float *pfViewOrigin, float *pfViewAngle, int FOV, int *piScreenSize, int *piOut)
  153.     {
  154.         float fAim[3];
  155.         float fNewAim[3];
  156.         float fView[3];
  157.         float fTmp[3];
  158.         float num;
  159.  
  160.         if(!pfIn||!piOut){ return false; }
  161.  
  162.         VectorSubtract(pfIn,pfViewOrigin,fAim);
  163.         MakeVector(pfViewAngle,fView); 
  164.  
  165.         //not in fov#!@#!@$#@!$
  166.         if (VectorAngle(fView,fAim) > (FOV/1.8))
  167.         {
  168.             return false;
  169.         }      
  170.    
  171.         VectorRotateZ(fAim,-pfViewAngle[1],fNewAim);// yaw
  172.         VectorRotateY(fNewAim,-pfViewAngle[0],fTmp);// pitch
  173.         VectorRotateX(fTmp,-pfViewAngle[2],fNewAim);// roll
  174.    
  175.         //they are behind us!@~!#@!$@!$
  176.         if(fNewAim[0] <= 0)
  177.             return false;
  178.    
  179.         if(FOV == 0.0f)
  180.         {
  181.             return false;
  182.         }
  183.         num = (float)(((piScreenSize[0] / 2) / fNewAim[0]) * (120.0 / FOV - 1.0 / 3.0));
  184.  
  185.         piOut[0] = (int)((piScreenSize[0] / 2) - num*fNewAim[1]);
  186.         piOut[1] = (int)((piScreenSize[1] / 2) - num * fNewAim[2]);
  187.  
  188.         //if( !g_bIsInFullScreen )
  189.         //{
  190.             piOut[0] = piOut[0] + 3;
  191.             piOut[1] = piOut[1] + 29;
  192.         //}
  193.  
  194.         return true;
  195.     }
  196. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement