Advertisement
Guest User

Math.cpp

a guest
Jul 5th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include "Math.h"
  2. #include "MyWindow.h"
  3. #include "Entity.h"
  4. ViewMatrix vMatrix;
  5. #ifndef M_PI
  6. #define M_PI 3.14159265358979323846
  7. #endif
  8. float Get3dDistance(Vector3D Pos1, Vector3D Pos2)
  9. {
  10. return sqrt(pow((Pos2.x - Pos1.x), 2.0) + pow((Pos2.y - Pos1.y), 2.0) + pow((Pos2.z - Pos1.z), 2.0));
  11. };
  12.  
  13.  
  14. bool WorldToScreen(Vector3D PositionIn, Vector3D &PositionOut)
  15. {
  16. float w = 0.0f;
  17.  
  18.  
  19. PositionOut.x = vMatrix.Matrix[0][0] * PositionIn.x + vMatrix.Matrix[0][1] * PositionIn.y + vMatrix.Matrix[0][2] * PositionIn.z + vMatrix.Matrix[0][3];
  20. PositionOut.y = vMatrix.Matrix[1][0] * PositionIn.x + vMatrix.Matrix[1][1] * PositionIn.y + vMatrix.Matrix[1][2] * PositionIn.z + vMatrix.Matrix[1][3];
  21. w = vMatrix.Matrix[3][0] * PositionIn.x + vMatrix.Matrix[3][1] * PositionIn.y + vMatrix.Matrix[3][2] * PositionIn.z + vMatrix.Matrix[3][3];
  22.  
  23.  
  24. if (w < 0.01f)
  25. return false;
  26.  
  27.  
  28. float InveseValue = 1.0f / w;
  29. PositionOut.x *= InveseValue;
  30. PositionOut.y *= InveseValue;
  31.  
  32.  
  33. float x = CMyWindow::Width / 2;
  34. float y = CMyWindow::Height / 2;
  35.  
  36.  
  37. x += 0.5 * PositionOut.x * CMyWindow::Width + 0.5;
  38. y -= 0.5 * PositionOut.y * CMyWindow::Height + 0.5;
  39.  
  40.  
  41. PositionOut.x = x;
  42. PositionOut.y = y;
  43.  
  44.  
  45. return true;
  46.  
  47.  
  48. };
  49. Vector3D VectorSubtract(Vector3D Vec1, Vector3D Vec2)
  50. {
  51. return Vector3D(Vec1.x - Vec2.x, Vec1.y - Vec2.y, Vec1.z - Vec2.z);
  52. };
  53. void CalcAngle(Vector3D Source, Vector3D Destination, Vector3D &Angles)
  54. {
  55. Vector3D forward = VectorSubtract(Destination, Source);
  56. float yaw, tmp, pitch;
  57. tmp = sqrt(forward.x*forward.x + forward.y*forward.y);
  58. yaw = (atan2(forward.y, forward.x) * 180 / M_PI);
  59. pitch = (atan2(-forward.z, tmp) * 180 / M_PI);
  60.  
  61.  
  62. Angles.x = pitch;
  63. Angles.y = yaw;
  64. Angles.z = 0;
  65.  
  66.  
  67. }
  68.  
  69.  
  70. /*
  71. void CalcAngle(Vector3D src, Vector3D dst, Vector3D &angles)
  72. {
  73.  
  74.  
  75. Vector3D delta = { (src.x - dst.x), (src.y - dst.y), (src.z - dst.z) };
  76. double hyp = sqrt(delta.x * delta.x + delta.y * delta.y);
  77. angles.x = (float)(asinf(delta.z / hyp) * 57.295779513082f);//asinf
  78. angles.y = (float)(atanf(delta.y / delta.x) * 57.295779513082f);
  79. angles.z = 0.0f;
  80.  
  81.  
  82. if (delta.x >= 0.0)
  83. {
  84. angles.y += 180.0f;
  85. }
  86.  
  87.  
  88. }
  89. */
  90.  
  91.  
  92. Vector3D AngleToDirection(Vector3D Angle)
  93. {
  94. Angle.x = (Angle.x) * 3.14159265 / 180;
  95. Angle.y = (Angle.y) * 3.14159265 / 180;
  96.  
  97.  
  98. float sinYaw = sin(Angle.y);
  99. float cosYaw = cos(Angle.y);
  100.  
  101.  
  102. float sinPitch = sin(Angle.x);
  103. float cosPitch = cos(Angle.x);
  104.  
  105.  
  106. Vector3D Direction;
  107. Direction.x = cosPitch * cosYaw;
  108. Direction.y = cosPitch * sinYaw;
  109. Direction.z = -sinPitch;
  110.  
  111.  
  112. return Direction;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement