CCActionCamera.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga 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. ctor:function(){
  38. cc.ActionInterval.prototype.ctor.call(this);
  39. this._centerXOrig=0;
  40. this._centerYOrig=0;
  41. this._centerZOrig=0;
  42. this._eyeXOrig=0;
  43. this._eyeYOrig=0;
  44. this._eyeZOrig=0;
  45. this._upXOrig=0;
  46. this._upYOrig=0;
  47. this._upZOrig=0;
  48. },
  49. startWithTarget:function (target) {
  50. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  51. var camera = target.getCamera();
  52. var centerXYZ = camera.getCenter();
  53. this._centerXOrig = centerXYZ.x;
  54. this._centerYOrig = centerXYZ.y;
  55. this._centerZOrig = centerXYZ.z;
  56. var eyeXYZ = camera.getEye();
  57. this._eyeXOrig = eyeXYZ.x;
  58. this._eyeYOrig = eyeXYZ.y;
  59. this._eyeZOrig = eyeXYZ.z;
  60. var upXYZ = camera.getUp();
  61. this._upXOrig = upXYZ.x;
  62. this._upYOrig = upXYZ.y;
  63. this._upZOrig = upXYZ.z;
  64. },
  65. /**
  66. * returns a new clone of the action
  67. * @returns {cc.ActionCamera}
  68. */
  69. clone:function(){
  70. return new cc.ActionCamera();
  71. },
  72. reverse:function () {
  73. return cc.ReverseTime.create(this);
  74. }
  75. });
  76. /**
  77. * Orbits the camera around the center of the screen using spherical coordinates
  78. * @class
  79. * @extends cc.ActionCamera
  80. */
  81. cc.OrbitCamera = cc.ActionCamera.extend(/** @lends cc.OrbitCamera# */{
  82. _radius:0.0,
  83. _deltaRadius:0.0,
  84. _angleZ:0.0,
  85. _deltaAngleZ:0.0,
  86. _angleX:0.0,
  87. _deltaAngleX:0.0,
  88. _radZ:0.0,
  89. _radDeltaZ:0.0,
  90. _radX:0.0,
  91. _radDeltaX:0.0,
  92. ctor:function(){
  93. cc.ActionCamera.prototype.ctor.call(this);
  94. this._radius=0.0;
  95. this._deltaRadius=0.0;
  96. this._angleZ=0.0;
  97. this._deltaAngleZ=0.0;
  98. this._angleX=0.0;
  99. this._deltaAngleX=0.0;
  100. this._radZ=0.0;
  101. this._radDeltaZ=0.0;
  102. this._radX=0.0;
  103. this._radDeltaX=0.0;
  104. },
  105. /**
  106. * initializes a cc.OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX
  107. * @param {Number} t time
  108. * @param {Number} radius
  109. * @param {Number} deltaRadius
  110. * @param {Number} angleZ
  111. * @param {Number} deltaAngleZ
  112. * @param {Number} angleX
  113. * @param {Number} deltaAngleX
  114. * @return {Boolean}
  115. */
  116. initWithDuration:function (t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX) {
  117. if (cc.ActionInterval.prototype.initWithDuration.call(this, t)) {
  118. this._radius = radius;
  119. this._deltaRadius = deltaRadius;
  120. this._angleZ = angleZ;
  121. this._deltaAngleZ = deltaAngleZ;
  122. this._angleX = angleX;
  123. this._deltaAngleX = deltaAngleX;
  124. this._radDeltaZ = cc.DEGREES_TO_RADIANS(deltaAngleZ);
  125. this._radDeltaX = cc.DEGREES_TO_RADIANS(deltaAngleX);
  126. return true;
  127. }
  128. return false;
  129. },
  130. /**
  131. * positions the camera according to spherical coordinates
  132. * @return {Object}
  133. */
  134. sphericalRadius:function () {
  135. var newRadius, zenith, azimuth;
  136. var camera = this._target.getCamera();
  137. var eyeXYZ = camera.getEye();
  138. var centerXYZ = camera.getCenter();
  139. var x = eyeXYZ.x - centerXYZ.x;
  140. var y = eyeXYZ.y - centerXYZ.y;
  141. var z = eyeXYZ.z - centerXYZ.z;
  142. var r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
  143. var s = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  144. if (s === 0.0)
  145. s = cc.FLT_EPSILON;
  146. if (r === 0.0)
  147. r = cc.FLT_EPSILON;
  148. zenith = Math.acos(z / r);
  149. if (x < 0)
  150. azimuth = Math.PI - Math.asin(y / s);
  151. else
  152. azimuth = Math.asin(y / s);
  153. newRadius = r / cc.Camera.getZEye();
  154. return {newRadius:newRadius, zenith:zenith, azimuth:azimuth};
  155. },
  156. startWithTarget:function (target) {
  157. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  158. var retValue = this.sphericalRadius();
  159. if (isNaN(this._radius))
  160. this._radius = retValue.newRadius;
  161. if (isNaN(this._angleZ))
  162. this._angleZ = cc.RADIANS_TO_DEGREES(retValue.zenith);
  163. if (isNaN(this._angleX))
  164. this._angleX = cc.RADIANS_TO_DEGREES(retValue.azimuth);
  165. this._radZ = cc.DEGREES_TO_RADIANS(this._angleZ);
  166. this._radX = cc.DEGREES_TO_RADIANS(this._angleX);
  167. },
  168. clone:function(){
  169. var a = new cc.OrbitCamera();
  170. a.initWithDuration(this._duration, this._radius, this._deltaRadius, this._angleZ, this._deltaAngleZ, this._angleX, this._deltaAngleX);
  171. return a;
  172. },
  173. update:function (dt) {
  174. var r = (this._radius + this._deltaRadius * dt) * cc.Camera.getZEye();
  175. var za = this._radZ + this._radDeltaZ * dt;
  176. var xa = this._radX + this._radDeltaX * dt;
  177. var i = Math.sin(za) * Math.cos(xa) * r + this._centerXOrig;
  178. var j = Math.sin(za) * Math.sin(xa) * r + this._centerYOrig;
  179. var k = Math.cos(za) * r + this._centerZOrig;
  180. this._target.getCamera().setEye(i, j, k);
  181. }
  182. });
  183. /**
  184. * creates a cc.OrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX
  185. * @param {Number} t time
  186. * @param {Number} radius
  187. * @param {Number} deltaRadius
  188. * @param {Number} angleZ
  189. * @param {Number} deltaAngleZ
  190. * @param {Number} angleX
  191. * @param {Number} deltaAngleX
  192. * @return {cc.OrbitCamera}
  193. */
  194. cc.OrbitCamera.create = function (t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX) {
  195. var ret = new cc.OrbitCamera();
  196. if (ret.initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX))
  197. return ret;
  198. return null;
  199. };