CCActionCamera.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  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. /**
  23. * Base class for cc.Camera actions
  24. * @class
  25. * @extends cc.ActionInterval
  26. */
  27. cc.ActionCamera = cc.ActionInterval.extend(/** @lends cc.ActionCamera# */{
  28. _centerXOrig:0,
  29. _centerYOrig:0,
  30. _centerZOrig:0,
  31. _eyeXOrig:0,
  32. _eyeYOrig:0,
  33. _eyeZOrig:0,
  34. _upXOrig:0,
  35. _upYOrig:0,
  36. _upZOrig:0,
  37. /**
  38. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  39. */
  40. ctor:function(){
  41. var _t = this;
  42. cc.ActionInterval.prototype.ctor.call(_t);
  43. _t._centerXOrig=0;
  44. _t._centerYOrig=0;
  45. _t._centerZOrig=0;
  46. _t._eyeXOrig=0;
  47. _t._eyeYOrig=0;
  48. _t._eyeZOrig=0;
  49. _t._upXOrig=0;
  50. _t._upYOrig=0;
  51. _t._upZOrig=0;
  52. },
  53. /**
  54. * called before the action start. It will also set the target.
  55. *
  56. * @param {cc.Node} target
  57. */
  58. startWithTarget:function (target) {
  59. var _t = this;
  60. cc.ActionInterval.prototype.startWithTarget.call(_t, target);
  61. var camera = target.getCamera();
  62. var centerXYZ = camera.getCenter();
  63. _t._centerXOrig = centerXYZ.x;
  64. _t._centerYOrig = centerXYZ.y;
  65. _t._centerZOrig = centerXYZ.z;
  66. var eyeXYZ = camera.getEye();
  67. _t._eyeXOrig = eyeXYZ.x;
  68. _t._eyeYOrig = eyeXYZ.y;
  69. _t._eyeZOrig = eyeXYZ.z;
  70. var upXYZ = camera.getUp();
  71. _t._upXOrig = upXYZ.x;
  72. _t._upYOrig = upXYZ.y;
  73. _t._upZOrig = upXYZ.z;
  74. },
  75. /**
  76. * to copy object with deep copy.
  77. * returns a new clone of the action
  78. *
  79. * @returns {cc.ActionCamera}
  80. */
  81. clone:function(){
  82. return new cc.ActionCamera();
  83. },
  84. /**
  85. * returns a reversed action. <br />
  86. * For example: <br />
  87. * - The action will be x coordinates of 0 move to 100. <br />
  88. * - The reversed action will be x of 100 move to 0.
  89. * - Will be rewritten
  90. *
  91. */
  92. reverse:function () {
  93. return new cc.ReverseTime(this);
  94. }
  95. });
  96. /**
  97. * Orbits the camera around the center of the screen using spherical coordinates.
  98. *
  99. * @param {Number} t time
  100. * @param {Number} radius
  101. * @param {Number} deltaRadius
  102. * @param {Number} angleZ
  103. * @param {Number} deltaAngleZ
  104. * @param {Number} angleX
  105. * @param {Number} deltaAngleX
  106. *
  107. * @class
  108. * @extends cc.ActionCamera
  109. */
  110. cc.OrbitCamera = cc.ActionCamera.extend(/** @lends cc.OrbitCamera# */{
  111. _radius: 0.0,
  112. _deltaRadius: 0.0,
  113. _angleZ: 0.0,
  114. _deltaAngleZ: 0.0,
  115. _angleX: 0.0,
  116. _deltaAngleX: 0.0,
  117. _radZ: 0.0,
  118. _radDeltaZ: 0.0,
  119. _radX: 0.0,
  120. _radDeltaX: 0.0,
  121. /**
  122. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  123. * creates a cc.OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX.
  124. * @param {Number} t time
  125. * @param {Number} radius
  126. * @param {Number} deltaRadius
  127. * @param {Number} angleZ
  128. * @param {Number} deltaAngleZ
  129. * @param {Number} angleX
  130. * @param {Number} deltaAngleX
  131. */
  132. ctor:function(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX){
  133. cc.ActionCamera.prototype.ctor.call(this);
  134. deltaAngleX !== undefined && this.initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX);
  135. },
  136. /**
  137. * initializes a cc.OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX
  138. * @param {Number} t time
  139. * @param {Number} radius
  140. * @param {Number} deltaRadius
  141. * @param {Number} angleZ
  142. * @param {Number} deltaAngleZ
  143. * @param {Number} angleX
  144. * @param {Number} deltaAngleX
  145. * @return {Boolean}
  146. */
  147. initWithDuration:function (t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX) {
  148. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  149. var _t = this;
  150. _t._radius = radius;
  151. _t._deltaRadius = deltaRadius;
  152. _t._angleZ = angleZ;
  153. _t._deltaAngleZ = deltaAngleZ;
  154. _t._angleX = angleX;
  155. _t._deltaAngleX = deltaAngleX;
  156. _t._radDeltaZ = cc.degreesToRadians(deltaAngleZ);
  157. _t._radDeltaX = cc.degreesToRadians(deltaAngleX);
  158. return true;
  159. }
  160. return false;
  161. },
  162. /**
  163. * positions the camera according to spherical coordinates
  164. * @return {Object}
  165. */
  166. sphericalRadius:function () {
  167. var newRadius, zenith, azimuth;
  168. var camera = this.target.getCamera();
  169. var eyeXYZ = camera.getEye();
  170. var centerXYZ = camera.getCenter();
  171. var x = eyeXYZ.x - centerXYZ.x, y = eyeXYZ.y - centerXYZ.y, z = eyeXYZ.z - centerXYZ.z;
  172. var r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
  173. var s = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  174. if (s === 0.0)
  175. s = cc.FLT_EPSILON;
  176. if (r === 0.0)
  177. r = cc.FLT_EPSILON;
  178. zenith = Math.acos(z / r);
  179. if (x < 0)
  180. azimuth = Math.PI - Math.asin(y / s);
  181. else
  182. azimuth = Math.asin(y / s);
  183. newRadius = r / cc.Camera.getZEye();
  184. return {newRadius:newRadius, zenith:zenith, azimuth:azimuth};
  185. },
  186. /**
  187. * called before the action start. It will also set the target.
  188. *
  189. * @param {cc.Node} target
  190. */
  191. startWithTarget:function (target) {
  192. var _t = this;
  193. cc.ActionInterval.prototype.startWithTarget.call(_t, target);
  194. var retValue = _t.sphericalRadius();
  195. if (isNaN(_t._radius))
  196. _t._radius = retValue.newRadius;
  197. if (isNaN(_t._angleZ))
  198. _t._angleZ = cc.radiansToDegrees(retValue.zenith);
  199. if (isNaN(_t._angleX))
  200. _t._angleX = cc.radiansToDegrees(retValue.azimuth);
  201. _t._radZ = cc.degreesToRadians(_t._angleZ);
  202. _t._radX = cc.degreesToRadians(_t._angleX);
  203. },
  204. /**
  205. * to copy object with deep copy.
  206. * returns a new clone of the action
  207. *
  208. * @returns {cc.ActionCamera}
  209. */
  210. clone:function(){
  211. var a = new cc.OrbitCamera(), _t = this;
  212. a.initWithDuration(_t._duration, _t._radius, _t._deltaRadius, _t._angleZ, _t._deltaAngleZ, _t._angleX, _t._deltaAngleX);
  213. return a;
  214. },
  215. /**
  216. * Called once per frame. Time is the number of seconds of a frame interval.
  217. *
  218. * @param {Number} dt
  219. */
  220. update:function (dt) {
  221. dt = this._computeEaseTime(dt);
  222. var r = (this._radius + this._deltaRadius * dt) * cc.Camera.getZEye();
  223. var za = this._radZ + this._radDeltaZ * dt;
  224. var xa = this._radX + this._radDeltaX * dt;
  225. var i = Math.sin(za) * Math.cos(xa) * r + this._centerXOrig;
  226. var j = Math.sin(za) * Math.sin(xa) * r + this._centerYOrig;
  227. var k = Math.cos(za) * r + this._centerZOrig;
  228. this.target.getCamera().setEye(i, j, k);
  229. }
  230. });
  231. /**
  232. * creates a cc.OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX
  233. * @function
  234. * @param {Number} t time
  235. * @param {Number} radius
  236. * @param {Number} deltaRadius
  237. * @param {Number} angleZ
  238. * @param {Number} deltaAngleZ
  239. * @param {Number} angleX
  240. * @param {Number} deltaAngleX
  241. * @return {cc.OrbitCamera}
  242. */
  243. cc.orbitCamera = function (t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX) {
  244. return new cc.OrbitCamera(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX);
  245. };
  246. /**
  247. * Please use cc.orbitCamera instead
  248. * creates a cc.OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX
  249. * @param {Number} t time
  250. * @param {Number} radius
  251. * @param {Number} deltaRadius
  252. * @param {Number} angleZ
  253. * @param {Number} deltaAngleZ
  254. * @param {Number} angleX
  255. * @param {Number} deltaAngleX
  256. * @return {cc.OrbitCamera}
  257. * @static
  258. * @deprecated since v3.0 please use cc.orbitCamera() instead.
  259. */
  260. cc.OrbitCamera.create = cc.orbitCamera;