home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 4 / CD_Magazyn_EXEC_nr_4.iso / Recent / dev / c / GSys.lha / gsys / g3d / G3DQuat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-01  |  2.3 KB  |  123 lines

  1.  
  2. /* A lot of ripped stuff */
  3.  
  4. #ifndef G3DQUAT_CPP
  5. #define G3DQUAT_CPP
  6.  
  7. #include "g3d/G3DQuat.h"
  8.  
  9. GQuat::GQuat(float rx, float ry, float rz)
  10. {
  11.     Set(rx, ry, rz);
  12. }
  13.  
  14. GQuat::GQuat()
  15. {
  16.     Set(0.0, 0.0, 0.0);
  17.  
  18. /*
  19.     x = 0.0;
  20.     y = 0.0;
  21.     z = 0.0;
  22.     w = 1.0;
  23. */
  24.  
  25. }
  26.  
  27. void G3DQuat::Set(float rx, float ry, float rz)
  28. {
  29.     float hyaw        = -rz*.5f;
  30.     float hpitch    = -ry*.5f;
  31.     float hroll        = -rx*.5f;
  32.  
  33.     float cosYaw = cos(hyaw);
  34.     float sinYaw = sin(hyaw);
  35.  
  36.     float cosPitch = cos(hpitch);
  37.     float sinPitch = sin(hpitch);
  38.  
  39.     float cosRoll = cos(hroll);
  40.     float sinRoll = sin(hroll);
  41.  
  42.     x = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw;
  43.     y = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw;
  44.     z = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw;
  45.     w = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw;
  46. }
  47.  
  48. void G3DQuat::Set()
  49. {
  50.     memset((void*)this, 0, sizeof (class G3DQuat) );
  51. }
  52.  
  53. /*
  54. GQuat G3DQuat::operator*(class G3DQuat &a, class Quat &b)
  55. {
  56.     float Ax = a.x;    // ---
  57.     float Ay = a.y;    // --|--
  58.     float Az = a.z;    // --|-|--
  59.     float Aw = a.w;    // - | | |
  60.             // | | | |
  61.     float Bx = b.x;    // - | | |
  62.     float By = b.y;    // --|-|-|
  63.     float Bz = b.z;    // --|--
  64.     float Bw = b.w;    // ---
  65.  
  66.     QQuat d;
  67.     d.x = Aw*Bx + Ax*Bw + Ay*Bz - Az*By;
  68.     d.y = Aw*By + Ay*Bw + Az*Bx - Ax*Bz;
  69.     d.z = Aw*Bz + Az*Bw + Ax*By - Ay*Bx;
  70.     d.w = Aw*Bw - Ax*Bx - Ay*By - Az*Bz;
  71.  
  72.     return(d);
  73. }
  74. */
  75.  
  76. /*
  77. void G3DQuat::operator*(class G3DQuat &b)
  78. {
  79.     float Ax = x;    // ---
  80.     float Ay = y;    // --|--
  81.     float Az = z;    // --|-|--
  82.     float Aw = w;    // - | | |
  83.             // | | | |
  84.     float Bx = b.x;    // - | | |
  85.     float By = b.y;    // --|-|-|
  86.     float Bz = b.z;    // --|--
  87.     float Bw = b.w;    // ---
  88.  
  89.     x = Aw*Bx + Ax*Bw + Ay*Bz - Az*By;
  90.     y = Aw*By + Ay*Bw + Az*Bx - Ax*Bz;
  91.     z = Aw*Bz + Az*Bw + Ax*By - Ay*Bx;
  92.     w = Aw*Bw - Ax*Bx - Ay*By - Az*Bz;
  93.  
  94. }
  95. */
  96.  
  97. GQuat operator*(class G3DQuat &a, class G3DQuat &b)
  98. {
  99.     float Ax = a.x;    // ---
  100.     float Ay = a.y;    // --|--
  101.     float Az = a.z;    // --|-|--
  102.     float Aw = a.w;    // - | | |
  103.             // | | | |
  104.     float Bx = b.x;    // - | | |
  105.     float By = b.y;    // --|-|-|
  106.     float Bz = b.z;    // --|--
  107.     float Bw = b.w;    // ---
  108.  
  109.     GQuat d;
  110.     d.x = Aw*Bx + Ax*Bw + Ay*Bz - Az*By;
  111.     d.y = Aw*By + Ay*Bw + Az*Bx - Ax*Bz;
  112.     d.z = Aw*Bz + Az*Bw + Ax*By - Ay*Bx;
  113.     d.w = Aw*Bw - Ax*Bx - Ay*By - Az*Bz;
  114.  
  115.     return(d);
  116. }
  117.  
  118. void G3DQuat::PrintfAll()
  119. {
  120.     printf("X: %f Y: %f Z: %f W: %f \n", x, y, z, w);
  121. }
  122.  
  123. #endif /* G3DQUAT_CPP */