CCActionCamera.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCActionCamera.h"
  25. #include "2d/CCNode.h"
  26. #include "platform/CCStdC.h"
  27. NS_CC_BEGIN
  28. //
  29. // CameraAction
  30. //
  31. ActionCamera::ActionCamera()
  32. : _center(0, 0, 0)
  33. , _eye(0, 0, FLT_EPSILON)
  34. , _up(0, 1, 0)
  35. {
  36. }
  37. void ActionCamera::startWithTarget(Node *target)
  38. {
  39. ActionInterval::startWithTarget(target);
  40. }
  41. ActionCamera* ActionCamera::clone() const
  42. {
  43. auto action = new (std::nothrow) ActionCamera();
  44. if (action)
  45. {
  46. action->autorelease();
  47. return action;
  48. }
  49. delete action;
  50. return nullptr;
  51. }
  52. ActionCamera * ActionCamera::reverse() const
  53. {
  54. // FIXME: This conversion isn't safe.
  55. return (ActionCamera*)ReverseTime::create(const_cast<ActionCamera*>(this));
  56. }
  57. void ActionCamera::restore()
  58. {
  59. _center.setZero();
  60. _eye.set(0.0f, 0.0f, FLT_EPSILON);
  61. _up.set(0.0f, 1.0f, 0.0f);
  62. }
  63. void ActionCamera::setEye(const Vec3& eye)
  64. {
  65. _eye = eye;
  66. updateTransform();
  67. }
  68. void ActionCamera::setEye(float x, float y, float z)
  69. {
  70. _eye.set(x, y, z);
  71. updateTransform();
  72. }
  73. void ActionCamera::setCenter(const Vec3& center)
  74. {
  75. _center = center;
  76. updateTransform();
  77. }
  78. void ActionCamera::setUp(const Vec3& up)
  79. {
  80. _up = up;
  81. updateTransform();
  82. }
  83. void ActionCamera::updateTransform()
  84. {
  85. Mat4 lookupMatrix;
  86. Mat4::createLookAt(_eye.x, _eye.y, _eye.z, _center.x, _center.y, _center.z, _up.x, _up.y, _up.z, &lookupMatrix);
  87. Vec2 anchorPoint = _target->getAnchorPointInPoints();
  88. bool needsTranslation = !anchorPoint.isZero();
  89. Mat4 mv = Mat4::IDENTITY;
  90. if(needsTranslation)
  91. {
  92. Mat4 t;
  93. Mat4::createTranslation(anchorPoint.x, anchorPoint.y, 0, &t);
  94. mv = mv * t;
  95. }
  96. mv = mv * lookupMatrix;
  97. if(needsTranslation)
  98. {
  99. Mat4 t;
  100. Mat4::createTranslation(-anchorPoint.x, -anchorPoint.y, 0, &t);
  101. mv = mv * t;
  102. }
  103. // FIXME: Using the AdditionalTransform is a complete hack.
  104. // This should be done by multiplying the lookup-Matrix with the Node's MV matrix
  105. // And then setting the result as the new MV matrix
  106. // But that operation needs to be done after all the 'updates'.
  107. // So the Director should emit an 'director_after_update' event.
  108. // And this object should listen to it
  109. _target->setAdditionalTransform(&mv);
  110. }
  111. //
  112. // OrbitCamera
  113. //
  114. OrbitCamera::OrbitCamera()
  115. : _radius(0.0)
  116. , _deltaRadius(0.0)
  117. , _angleZ(0.0)
  118. , _deltaAngleZ(0.0)
  119. , _angleX(0.0)
  120. , _deltaAngleX(0.0)
  121. , _radZ(0.0)
  122. , _radDeltaZ(0.0)
  123. , _radX(0.0)
  124. , _radDeltaX(0.0)
  125. {
  126. }
  127. OrbitCamera::~OrbitCamera()
  128. {
  129. }
  130. OrbitCamera * OrbitCamera::create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
  131. {
  132. OrbitCamera * obitCamera = new (std::nothrow) OrbitCamera();
  133. if(obitCamera && obitCamera->initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX))
  134. {
  135. obitCamera->autorelease();
  136. return obitCamera;
  137. }
  138. delete obitCamera;
  139. return nullptr;
  140. }
  141. OrbitCamera* OrbitCamera::clone() const
  142. {
  143. // no copy constructor
  144. return OrbitCamera::create(_duration, _radius, _deltaRadius, _angleZ, _deltaAngleZ, _angleX, _deltaAngleX);
  145. }
  146. bool OrbitCamera::initWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
  147. {
  148. if ( ActionInterval::initWithDuration(t) )
  149. {
  150. _radius = radius;
  151. _deltaRadius = deltaRadius;
  152. _angleZ = angleZ;
  153. _deltaAngleZ = deltaAngleZ;
  154. _angleX = angleX;
  155. _deltaAngleX = deltaAngleX;
  156. _radDeltaZ = (float)CC_DEGREES_TO_RADIANS(deltaAngleZ);
  157. _radDeltaX = (float)CC_DEGREES_TO_RADIANS(deltaAngleX);
  158. return true;
  159. }
  160. return false;
  161. }
  162. void OrbitCamera::startWithTarget(Node *target)
  163. {
  164. ActionCamera::startWithTarget(target);
  165. float r, zenith, azimuth;
  166. this->sphericalRadius(&r, &zenith, &azimuth);
  167. if( std::isnan(_radius) )
  168. _radius = r;
  169. if( std::isnan(_angleZ) )
  170. _angleZ = (float)CC_RADIANS_TO_DEGREES(zenith);
  171. if( std::isnan(_angleX) )
  172. _angleX = (float)CC_RADIANS_TO_DEGREES(azimuth);
  173. _radZ = (float)CC_DEGREES_TO_RADIANS(_angleZ);
  174. _radX = (float)CC_DEGREES_TO_RADIANS(_angleX);
  175. }
  176. void OrbitCamera::update(float dt)
  177. {
  178. float r = (_radius + _deltaRadius * dt) * FLT_EPSILON;
  179. float za = _radZ + _radDeltaZ * dt;
  180. float xa = _radX + _radDeltaX * dt;
  181. float i = sinf(za) * cosf(xa) * r + _center.x;
  182. float j = sinf(za) * sinf(xa) * r + _center.y;
  183. float k = cosf(za) * r + _center.z;
  184. setEye(i,j,k);
  185. }
  186. void OrbitCamera::sphericalRadius(float *newRadius, float *zenith, float *azimuth)
  187. {
  188. float r; // radius
  189. float s;
  190. float x = _eye.x - _center.x;
  191. float y = _eye.y - _center.y;
  192. float z = _eye.z - _center.z;
  193. r = sqrtf( powf(x,2) + powf(y,2) + powf(z,2));
  194. s = sqrtf( powf(x,2) + powf(y,2));
  195. if( s == 0.0f )
  196. s = FLT_EPSILON;
  197. if(r==0.0f)
  198. r = FLT_EPSILON;
  199. *zenith = acosf( z/r);
  200. if( x < 0 )
  201. *azimuth= (float)M_PI - asinf(y/s);
  202. else
  203. *azimuth = asinf(y/s);
  204. *newRadius = r / FLT_EPSILON;
  205. }
  206. NS_CC_END