OldSchoolHack

Registrieren / Anmelden Deutsch

Vector Klasse


icon Vector Klasse #1

Anmeldungsdatum: Mai 2011

Beiträge: 419

Benutzer-Bewertung:

199 positiv
8 negativ
Kleines 0815 Release von mir, damit sich hier mal wieder ein klein wenig was in Richtung Programmierung tut

Achtung: Die Funktionen der Klasse arbeiten mit Grad und nicht mit Bogenmaß!

vector.h
CPP Code:
  1. #include <math.h>
  2. #pragma once
  3. #pragma warning (disable:4244)
  4. #define M_PI 3.1415926535897932384626433832795
  5.  
  6. class Vector;
  7. class Angle;
  8.  
  9. class Vector
  10. {
  11. public:
  12. float x, y, z;
  13.  
  14. Vector();
  15. Vector(float X, float Y, float Z);
  16.  
  17. Vector operator + (const Vector &add);
  18. Vector operator - (const Vector &sub);
  19. Vector operator * (const Vector &mul);
  20. Vector operator * (const float mul);
  21.  
  22. void operator += (const Vector &add);
  23. void operator -= (const Vector &sub);
  24. void operator *= (const Vector &mul);
  25. void operator *= (const float mul);
  26.  
  27. float operator[](int i);
  28.  
  29. float GetLength();
  30. void GetVectorAngles(Angle *angles);
  31. void CrossProduct(const Vector &b, Vector *result);
  32. };
  33.  
  34. class Angle : public Vector
  35. {
  36. public:
  37. Angle() : Vector(){};
  38. Angle(float pitch, float yaw, float roll) : Vector(pitch, yaw, roll){};
  39. Angle(const Vector &v) : Vector(v){};
  40.  
  41. Angle DegAng_to_RadAng();
  42. Angle RadAng_to_DegAng();
  43.  
  44. void Normalize();
  45. void GetAngleVectors(Vector *forward, Vector *right, Vector *up);
  46. };


vector.cpp
CPP Code:
  1. #include "vector.h"
  2.  
  3. Vector::Vector()
  4. {
  5. x = 0.0f;
  6. y = 0.0f;
  7. z = 0.0f;
  8. }
  9.  
  10. Vector::Vector(float X, float Y, float Z)
  11. {
  12. x = X;
  13. y = Y;
  14. z = Z;
  15. }
  16.  
  17. Vector Vector::operator + (const Vector &add)
  18. {
  19. return Vector(x + add.x, y + add.y, z + add.z);
  20. }
  21.  
  22. Vector Vector::operator - (const Vector &sub)
  23. {
  24. return Vector(x - sub.x, y - sub.y, z - sub.z);
  25. }
  26.  
  27. Vector Vector::operator * (const Vector &mul)
  28. {
  29. return Vector(x*mul.x, y*mul.y, z*mul.z);
  30. }
  31.  
  32. Vector Vector::operator * (const float mul)
  33. {
  34. return Vector(x*mul, y*mul, z*mul);
  35. }
  36.  
  37. void Vector::operator += (const Vector &add)
  38. {
  39. x += add.x;
  40. y += add.y;
  41. z += add.z;
  42. }
  43.  
  44. void Vector::operator -= (const Vector &sub)
  45. {
  46. x -= sub.x;
  47. y -= sub.y;
  48. z -= sub.z;
  49. }
  50.  
  51. void Vector::operator *= (const Vector &mul)
  52. {
  53. x *= mul.x;
  54. y *= mul.y;
  55. z *= mul.z;
  56. }
  57.  
  58. void Vector::operator *= (const float mul)
  59. {
  60. x *= mul;
  61. y *= mul;
  62. z *= mul;
  63. }
  64.  
  65. float Vector::operator [] (const int i)
  66. {
  67. if (i == 0)
  68. return x;
  69.  
  70. if (i == 1)
  71. return y;
  72.  
  73. if (i == 2)
  74. return z;
  75.  
  76. return 0.f;
  77. }
  78.  
  79. float Vector::GetLength()
  80. {
  81. return sqrt(x*x + y*y + z*z);
  82. }
  83.  
  84. void Vector::GetVectorAngles(Angle *angles)
  85. {
  86. float tmp, yaw, pitch;
  87.  
  88. if (y == 0 && x == 0)
  89. {
  90. yaw = 0;
  91. if (z > 0)
  92. pitch = 270;
  93. else
  94. pitch = 90;
  95. }
  96. else
  97. {
  98. yaw = (atan2(y, x) * 180 / M_PI);
  99. if (yaw < 0)
  100. yaw += 360;
  101.  
  102. tmp = sqrt(x * x + y * y);
  103. pitch = (atan2(-z, tmp) * 180 / M_PI);
  104. if (pitch < 0)
  105. pitch += 360;
  106. }
  107.  
  108. angles->x = pitch;
  109. angles->y = yaw;
  110. angles->z = 0;
  111. }
  112.  
  113. void Vector::CrossProduct(const Vector &b, Vector *result)
  114. {
  115. result->x = y*b.z - z*b.y;
  116. result->y = z*b.x - x*b.z;
  117. result->z = x*b.y - y*b.x;
  118. }
  119.  
  120.  
  121. Angle Angle::DegAng_to_RadAng()
  122. {
  123. return Angle(x / 180 * M_PI, y / 180 * M_PI, z / 180 * M_PI);
  124. }
  125.  
  126. Angle Angle::RadAng_to_DegAng()
  127. {
  128. return Angle(x / M_PI * 180, y / M_PI * 180, z / M_PI * 180);
  129. }
  130.  
  131. void Angle::Normalize()
  132. {
  133. while (x > 180.f)
  134. {
  135. x -= 360.f;
  136. }
  137.  
  138. while (x < -180.f)
  139. {
  140. x += 360.f;
  141. }
  142.  
  143.  
  144. while (y > 180.f)
  145. {
  146. y -= 360.f;
  147. }
  148.  
  149. while (y < -180.f)
  150. {
  151. y += 360.f;
  152. }
  153.  
  154. while (z > 180.f)
  155. {
  156. z -= 360.f;
  157. }
  158.  
  159. while (z < -180.f)
  160. {
  161. z += 360.f;
  162. }
  163. }
  164.  
  165. void Angle::GetAngleVectors(Vector *forward, Vector *right, Vector *up)
  166. {
  167. float sr, sp, sy, cr, cp, cy;
  168.  
  169. sy = sin(y / 180 * M_PI);
  170. cy = cos(y / 180 * M_PI);
  171.  
  172. sp = sin(x / 180 * M_PI);
  173. cp = cos(x / 180 * M_PI);
  174.  
  175. sr = sin(z / 180 * M_PI);
  176. cr = cos(z / 180 * M_PI);
  177.  
  178. if (forward)
  179. {
  180. forward->x = cp*cy;
  181. forward->y = cp*sy;
  182. forward->z = -sp;
  183. }
  184.  
  185. if (right)
  186. {
  187. right->x = (-1 * sr*sp*cy + -1 * cr*-sy);
  188. right->y = (-1 * sr*sp*sy + -1 * cr*cy);
  189. right->z = -1 * sr*cp;
  190. }
  191.  
  192. if (up)
  193. {
  194. up->x = (cr*sp*cy + -sr*-sy);
  195. up->y = (cr*sp*sy + -sr*cy);
  196. up->z = cr*cp;
  197. }
  198. }

mfg Dr_Pepper

__________________

http://abload.de/img/signaturmitoshlogos2ufb.jpg
http://abload.de/img/pfeila4uzk.png Da unten ist ein Like-Button, benutze ihn doch
Zuletzt geändert von Dr_Pepper (Mo 9. Dez 2013, 18:08)

Grund: kein Grund angegeben