CCActionCamera.cpp 6.4 KB

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