mathUtils.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mMath.h"
  23. #include "math/mathUtils.h"
  24. #include "math/mRandom.h"
  25. #include "2d/core/Vector2.h"
  26. RandomLCG sgRandom(0xdeadbeef); ///< Our random number generator.
  27. //------------------------------------------------------------------------------
  28. // Creates orientation matrix from a direction vector. Assumes ( 0 0 1 ) is up.
  29. //------------------------------------------------------------------------------
  30. MatrixF createOrientFromDir( Point3F &direction )
  31. {
  32. Point3F j = direction;
  33. Point3F k(0.0, 0.0, 1.0);
  34. Point3F i;
  35. mCross( j, k, &i );
  36. if( i.magnitudeSafe() == 0.0 )
  37. {
  38. i = Point3F( 0.0, -1.0, 0.0 );
  39. }
  40. i.normalizeSafe();
  41. mCross( i, j, &k );
  42. MatrixF mat( true );
  43. mat.setColumn( 0, i );
  44. mat.setColumn( 1, j );
  45. mat.setColumn( 2, k );
  46. return mat;
  47. }
  48. //------------------------------------------------------------------------------
  49. // Creates random direction given angle parameters similar to the particle system.
  50. // The angles are relative to the specified axis.
  51. //------------------------------------------------------------------------------
  52. Point3F randomDir( Point3F &axis, F32 thetaAngleMin, F32 thetaAngleMax,
  53. F32 phiAngleMin, F32 phiAngleMax )
  54. {
  55. MatrixF orient = createOrientFromDir( axis );
  56. Point3F axisx;
  57. orient.getColumn( 0, &axisx );
  58. F32 theta = (thetaAngleMax - thetaAngleMin) * sgRandom.randF() + thetaAngleMin;
  59. F32 phi = (phiAngleMax - phiAngleMin) * sgRandom.randF() + phiAngleMin;
  60. // Both phi and theta are in degs. Create axis angles out of them, and create the
  61. // appropriate rotation matrix...
  62. AngAxisF thetaRot(axisx, theta * ((F32)M_PI / 180.0f));
  63. AngAxisF phiRot(axis, phi * ((F32)M_PI / 180.0f));
  64. Point3F ejectionAxis = axis;
  65. MatrixF temp(true);
  66. thetaRot.setMatrix(&temp);
  67. temp.mulP(ejectionAxis);
  68. phiRot.setMatrix(&temp);
  69. temp.mulP(ejectionAxis);
  70. return ejectionAxis;
  71. }
  72. //------------------------------------------------------------------------------
  73. // Returns yaw and pitch angles from a given vector. Angles are in RADIANS.
  74. // Assumes north is (0.0, 1.0, 0.0), the degrees move upwards clockwise.
  75. // The range of yaw is 0 - 2PI. The range of pitch is -PI/2 - PI/2.
  76. // ASSUMES Z AXIS IS UP
  77. //------------------------------------------------------------------------------
  78. void getAnglesFromVector( VectorF &vec, F32 &yawAng, F32 &pitchAng )
  79. {
  80. yawAng = mAtan( vec.x, vec.y );
  81. if( yawAng < 0.0 )
  82. {
  83. yawAng += (F32)M_2PI;
  84. }
  85. if( fabs(vec.x) > fabs(vec.y) )
  86. {
  87. pitchAng = mAtan( fabs(vec.z), fabs(vec.x) );
  88. }
  89. else
  90. {
  91. pitchAng = mAtan( fabs(vec.z), fabs(vec.y) );
  92. }
  93. if( vec.z < 0.0 )
  94. {
  95. pitchAng = -pitchAng;
  96. }
  97. }
  98. //------------------------------------------------------------------------------
  99. // Returns vector from given yaw and pitch angles. Angles are in RADIANS.
  100. // Assumes north is (0.0, 1.0, 0.0), the degrees move upwards clockwise.
  101. // The range of yaw is 0 - 2PI. The range of pitch is -PI/2 - PI/2.
  102. // ASSUMES Z AXIS IS UP
  103. //------------------------------------------------------------------------------
  104. void getVectorFromAngles( VectorF &vec, F32 &yawAng, F32 &pitchAng )
  105. {
  106. VectorF pnt( 0.0, 1.0, 0.0 );
  107. EulerF rot( -pitchAng, 0.0, 0.0 );
  108. MatrixF mat( rot );
  109. rot.set( 0.0, 0.0, yawAng );
  110. MatrixF mat2( rot );
  111. mat.mulV( pnt );
  112. mat2.mulV( pnt );
  113. vec = pnt;
  114. }