CCVRGenericHeadTracker.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /****************************************************************************
  2. Copyright (c) 2016 Google Inc.
  3. Copyright (c) 2016-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. // IMPORTANT
  22. // For iOS/Mac, this file is treated as an "Objective-C++" file.
  23. // To change this behaviour, use the File Inspector from Xcode
  24. #include "vr/CCVRGenericHeadTracker.h"
  25. #include <cmath>
  26. #include "platform/CCPlatformMacros.h"
  27. #include "platform/CCDevice.h"
  28. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  29. #import <CoreMotion/CoreMotion.h>
  30. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  31. #include <jni.h>
  32. #include "platform/android/jni/JniHelper.h"
  33. #endif
  34. NS_CC_BEGIN
  35. //////
  36. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  37. static Mat4 matrixFromRotationMatrix(const CMRotationMatrix& rotationMatrix)
  38. {
  39. return Mat4(rotationMatrix.m11,
  40. rotationMatrix.m21,
  41. rotationMatrix.m31,
  42. 0.0f,
  43. rotationMatrix.m12,
  44. rotationMatrix.m22,
  45. rotationMatrix.m32,
  46. 0.0f,
  47. rotationMatrix.m13,
  48. rotationMatrix.m23,
  49. rotationMatrix.m33,
  50. 0.0f,
  51. 0.0f,
  52. 0.0f,
  53. 0.0f,
  54. 1.0f);
  55. }
  56. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  57. // getRotationMatrix taken from Android's SensorManager.java
  58. Mat4 getRotationMatrix(const Vec3& gravity, const Vec3& geomagnetic)
  59. {
  60. float Ax = gravity.x;
  61. float Ay = gravity.y;
  62. float Az = gravity.z;
  63. const float normsqA = (Ax*Ax + Ay*Ay + Az*Az);
  64. const float g = 9.81f;
  65. const float freeFallGravitySquared = 0.01f * g * g;
  66. if (normsqA < freeFallGravitySquared) {
  67. // gravity less than 10% of normal value
  68. return Mat4::IDENTITY;
  69. }
  70. const float Ex = geomagnetic.x;
  71. const float Ey = geomagnetic.y;
  72. const float Ez = geomagnetic.z;
  73. float Hx = Ey*Az - Ez*Ay;
  74. float Hy = Ez*Ax - Ex*Az;
  75. float Hz = Ex*Ay - Ey*Ax;
  76. const float normH = std::sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
  77. if (normH < 0.1f) {
  78. // device is close to free fall (or in space?), or close to
  79. // magnetic north pole. Typical values are > 100.
  80. return Mat4::IDENTITY;
  81. }
  82. const float invH = 1.0f / normH;
  83. Hx *= invH;
  84. Hy *= invH;
  85. Hz *= invH;
  86. const float invA = 1.0f / std::sqrt(Ax*Ax + Ay*Ay + Az*Az);
  87. Ax *= invA;
  88. Ay *= invA;
  89. Az *= invA;
  90. const float Mx = Ay*Hz - Az*Hy;
  91. const float My = Az*Hx - Ax*Hz;
  92. const float Mz = Ax*Hy - Ay*Hx;
  93. return Mat4( Hx, Mx, Ax, 0,
  94. Hy, My, Ay, 0,
  95. Hz, Mz, Az, 0,
  96. 0, 0, 0, 1);
  97. }
  98. Vec3 lowPass(const Vec3& input, const Vec3& prev)
  99. {
  100. // if ALPHA = 1 OR 0, no filter applies.
  101. static const float ALPHA = 0.12f;
  102. return Vec3(prev.x + ALPHA * (input.x - prev.x),
  103. prev.y + ALPHA * (input.y - prev.y),
  104. prev.z + ALPHA * (input.z - prev.z));
  105. }
  106. #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  107. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  108. static Mat4 getRotateEulerMatrix(float x, float y, float z)
  109. {
  110. x *= static_cast<float>(M_PI) / 180.0f;
  111. y *= static_cast<float>(M_PI) / 180.0f;
  112. z *= static_cast<float>(M_PI) / 180.0f;
  113. float cx = std::cos(x);
  114. float sx = std::sin(x);
  115. float cy = std::cos(y);
  116. float sy = std::sin(y);
  117. float cz = std::cos(z);
  118. float sz = std::sin(z);
  119. float cxsy = cx * sy;
  120. float sxsy = sx * sy;
  121. Mat4 matrix;
  122. matrix.m[0] = cy * cz;
  123. matrix.m[1] = -cy * sz;
  124. matrix.m[2] = sy;
  125. matrix.m[3] = 0.0f;
  126. matrix.m[4] = cxsy * cz + cx * sz;
  127. matrix.m[5] = -cxsy * sz + cx * cz;
  128. matrix.m[6] = -sx * cy;
  129. matrix.m[7] = 0.0f;
  130. matrix.m[8] = -sxsy * cz + sx * sz;
  131. matrix.m[9] = sxsy * sz + sx * cz;
  132. matrix.m[10] = cx * cy;
  133. matrix.m[11] = 0.0f;
  134. matrix.m[12] = 0.0f;
  135. matrix.m[13] = 0.0f;
  136. matrix.m[14] = 0.0f;
  137. matrix.m[15] = 1.0f;
  138. return matrix;
  139. }
  140. #endif
  141. VRGenericHeadTracker::VRGenericHeadTracker()
  142. : _localPosition(Vec3::ZERO)
  143. {
  144. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  145. _motionMgr = [[CMMotionManager alloc] init];
  146. #endif
  147. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  148. startTracking();
  149. #endif
  150. }
  151. VRGenericHeadTracker::~VRGenericHeadTracker()
  152. {
  153. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  154. stopTracking();
  155. #endif
  156. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  157. [(CMMotionManager*)_motionMgr release];
  158. #endif
  159. }
  160. void VRGenericHeadTracker::startTracking()
  161. {
  162. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  163. CMMotionManager* motionMgr = (CMMotionManager*)_motionMgr;
  164. if (motionMgr.isDeviceMotionAvailable && !motionMgr.isDeviceMotionActive)
  165. {
  166. [motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];
  167. }
  168. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  169. if (orientation == UIInterfaceOrientationLandscapeLeft)
  170. {
  171. _deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, 90.f);
  172. }
  173. else if (orientation == UIInterfaceOrientationLandscapeRight)
  174. {
  175. _deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, -90.f);
  176. }
  177. // the inertial reference frame has z up and x forward, while the world has z out and x right
  178. _worldToInertialReferenceFrame = getRotateEulerMatrix(-90.f, 0.f, 90.f);
  179. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  180. _deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, -90.f);
  181. _worldToInertialReferenceFrame = getRotateEulerMatrix(-90.f, 0.f, 90.f);
  182. JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxHelper", "enableAccelerometer");
  183. JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxHelper", "enableCompass");
  184. #endif
  185. }
  186. void VRGenericHeadTracker::stopTracking()
  187. {
  188. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  189. [(CMMotionManager*)_motionMgr stopDeviceMotionUpdates];
  190. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  191. Device::setAccelerometerEnabled(false);
  192. #endif
  193. }
  194. Vec3 VRGenericHeadTracker::getLocalPosition()
  195. {
  196. return _localPosition;
  197. }
  198. Mat4 VRGenericHeadTracker::getLocalRotation()
  199. {
  200. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  201. CMMotionManager* motionMgr = (CMMotionManager*)_motionMgr;
  202. CMDeviceMotion* motion = motionMgr.deviceMotion;
  203. if (motion) {
  204. CMRotationMatrix rotationMatrix = motion.attitude.rotationMatrix;
  205. Mat4 inertialReferenceFrameToDevice0 = matrixFromRotationMatrix(rotationMatrix); // note the matrix inversion
  206. Mat4 inertialReferenceFrameToDevice = inertialReferenceFrameToDevice0.getTransposed();
  207. Mat4 worldToDevice = inertialReferenceFrameToDevice * _worldToInertialReferenceFrame;
  208. return _deviceToDisplay * worldToDevice;
  209. }
  210. // bug!
  211. return Mat4::IDENTITY;
  212. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  213. static Vec3 prevAccel = Vec3(0,0,0);
  214. static Vec3 prevCompass = Vec3(0,0,0);
  215. Vec3 accel = JniHelper::callStaticVec3Method("org/cocos2dx/lib/Cocos2dxHelper", "getAccelValue");
  216. Vec3 compass = JniHelper::callStaticVec3Method("org/cocos2dx/lib/Cocos2dxHelper", "getCompassValue");
  217. // CCLOG("accel: %f, %f, %f.... compass: %f, %f, %f", accel.x, accel.y, accel.z, compass.x, compass.y, compass.z);
  218. prevAccel = lowPass(accel, prevAccel);
  219. prevCompass = lowPass(compass, prevCompass);
  220. // CCLOG("low pass accel: %f, %f, %f.... compass: %f, %f, %f", prevAccel.x, prevAccel.y, prevAccel.z, prevCompass.x, prevCompass.y, prevCompass.z);
  221. Mat4 rotMatrix = getRotationMatrix(prevAccel, prevCompass);
  222. Mat4 inertialReferenceFrameToDevice(rotMatrix);
  223. Mat4 worldToDevice = inertialReferenceFrameToDevice * _worldToInertialReferenceFrame;
  224. return _deviceToDisplay * worldToDevice;
  225. #else
  226. return Mat4::IDENTITY;
  227. #endif
  228. }
  229. NS_CC_END