CCPUUtil.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUUtil.h"
  23. #include "base/ccMacros.h"
  24. NS_CC_BEGIN
  25. cocos2d::Vec3 PUUtil::randomDeviant( const Vec3 &src, float angle, const Vec3& up /*= Vec3::ZERO*/ )
  26. {
  27. Vec3 newUp;
  28. if (up == Vec3::ZERO)
  29. {
  30. // Generate an up vector
  31. newUp = perpendicular(src);
  32. }
  33. else
  34. {
  35. newUp = up;
  36. }
  37. // Rotate up vector by random amount around this
  38. //Quaternion q;
  39. //q.FromAngleAxis( Radian(Math::UnitRandom() * Math::TWO_PI), *this );
  40. //newUp = q * newUp;
  41. //// Finally rotate this by given angle around randomised up
  42. //q.FromAngleAxis( angle, newUp );
  43. //return q * (*this);
  44. Quaternion q;
  45. Mat4 mat;
  46. Quaternion::createFromAxisAngle(src, CCRANDOM_0_1() * M_PI * 2.0f, &q);
  47. Mat4::createRotation(q, &mat);
  48. //{
  49. // Vec3 uv, uuv;
  50. // Vec3 qvec(q.x, q.y, q.z);
  51. // Vec3::cross(qvec, newUp, &uv);
  52. // Vec3::cross(qvec, uv, &uuv);
  53. // uv *= (2.0f * q.w);
  54. // uuv *= 2.0f;
  55. // newUp = newUp + uv + uuv;
  56. //}
  57. newUp = mat * newUp;
  58. Quaternion::createFromAxisAngle(newUp, angle, &q);
  59. Mat4::createRotation(q, &mat);
  60. return mat * src;
  61. //{
  62. // Vec3 uv, uuv;
  63. // Vec3 qvec(q.x, q.y, q.z);
  64. // Vec3::cross(qvec, src, &uv);
  65. // Vec3::cross(qvec, uv, &uuv);
  66. // uv *= (2.0f * q.w);
  67. // uuv *= 2.0f;
  68. // return src + uv + uuv;
  69. //}
  70. }
  71. cocos2d::Vec3 PUUtil::perpendicular( const Vec3 &src )
  72. {
  73. //static const float fSquareZero = (float)(1e-06 * 1e-06);
  74. //Vector3 perp = this->crossProduct( Vector3::UNIT_X );
  75. //// Check length
  76. //if( perp.squaredLength() < fSquareZero )
  77. //{
  78. // /* This vector is the Y axis multiplied by a scalar, so we have
  79. // to use another axis.
  80. // */
  81. // perp = this->crossProduct( Vector3::UNIT_Y );
  82. //}
  83. //perp.normalise();
  84. //return perp;
  85. static const float fSquareZero = (float)(1e-06 * 1e-06);
  86. Vec3 perp;
  87. Vec3::cross(src, Vec3::UNIT_X, &perp);
  88. // Check length
  89. if( perp.lengthSquared() < fSquareZero )
  90. {
  91. /* This vector is the Y axis multiplied by a scalar, so we have
  92. to use another axis.
  93. */
  94. Vec3::cross(src, Vec3::UNIT_Y, &perp);
  95. }
  96. perp.normalize();
  97. return perp;
  98. }
  99. NS_CC_END