CCVRGenericHeadTracker.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /****************************************************************************
  2. Copyright (c) 2016 Google Inc.
  3. Copyright (c) 2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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. // IMPORTANT
  23. // For iOS/Mac, this file is treated as an "Objective-C++" file.
  24. // To change this behaviour, use the File Inspector from Xcode
  25. #include "vr/CCVRGenericHeadTracker.h"
  26. #include <cmath>
  27. #include "platform/CCPlatformMacros.h"
  28. #include "platform/CCDevice.h"
  29. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  30. #import <CoreMotion/CoreMotion.h>
  31. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  32. #include <jni.h>
  33. #include "platform/android/jni/JniHelper.h"
  34. #endif
  35. NS_CC_BEGIN
  36. //////
  37. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  38. static Mat4 matrixFromRotationMatrix(const CMRotationMatrix& rotationMatrix)
  39. {
  40. return Mat4(rotationMatrix.m11,
  41. rotationMatrix.m21,
  42. rotationMatrix.m31,
  43. 0.0f,
  44. rotationMatrix.m12,
  45. rotationMatrix.m22,
  46. rotationMatrix.m32,
  47. 0.0f,
  48. rotationMatrix.m13,
  49. rotationMatrix.m23,
  50. rotationMatrix.m33,
  51. 0.0f,
  52. 0.0f,
  53. 0.0f,
  54. 0.0f,
  55. 1.0f);
  56. }
  57. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  58. // getRotationMatrix taken from Android's SensorManager.java
  59. Mat4 getRotationMatrix(const Vec3& gravity, const Vec3& geomagnetic)
  60. {
  61. float Ax = gravity.x;
  62. float Ay = gravity.y;
  63. float Az = gravity.z;
  64. const float normsqA = (Ax*Ax + Ay*Ay + Az*Az);
  65. const float g = 9.81f;
  66. const float freeFallGravitySquared = 0.01f * g * g;
  67. if (normsqA < freeFallGravitySquared) {
  68. // gravity less than 10% of normal value
  69. return Mat4::IDENTITY;
  70. }
  71. const float Ex = geomagnetic.x;
  72. const float Ey = geomagnetic.y;
  73. const float Ez = geomagnetic.z;
  74. float Hx = Ey*Az - Ez*Ay;
  75. float Hy = Ez*Ax - Ex*Az;
  76. float Hz = Ex*Ay - Ey*Ax;
  77. const float normH = std::sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
  78. if (normH < 0.1f) {
  79. // device is close to free fall (or in space?), or close to
  80. // magnetic north pole. Typical values are > 100.
  81. return Mat4::IDENTITY;
  82. }
  83. const float invH = 1.0f / normH;
  84. Hx *= invH;
  85. Hy *= invH;
  86. Hz *= invH;
  87. const float invA = 1.0f / std::sqrt(Ax*Ax + Ay*Ay + Az*Az);
  88. Ax *= invA;
  89. Ay *= invA;
  90. Az *= invA;
  91. const float Mx = Ay*Hz - Az*Hy;
  92. const float My = Az*Hx - Ax*Hz;
  93. const float Mz = Ax*Hy - Ay*Hx;
  94. return Mat4( Hx, Mx, Ax, 0,
  95. Hy, My, Ay, 0,
  96. Hz, Mz, Az, 0,
  97. 0, 0, 0, 1);
  98. }
  99. Vec3 lowPass(const Vec3& input, const Vec3& prev)
  100. {
  101. // if ALPHA = 1 OR 0, no filter applies.
  102. static const float ALPHA = 0.12f;
  103. return Vec3(prev.x + ALPHA * (input.x - prev.x),
  104. prev.y + ALPHA * (input.y - prev.y),
  105. prev.z + ALPHA * (input.z - prev.z));
  106. }
  107. #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  108. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  109. static Mat4 getRotateEulerMatrix(float x, float y, float z)
  110. {
  111. x *= static_cast<float>(M_PI) / 180.0f;
  112. y *= static_cast<float>(M_PI) / 180.0f;
  113. z *= static_cast<float>(M_PI) / 180.0f;
  114. float cx = std::cos(x);
  115. float sx = std::sin(x);
  116. float cy = std::cos(y);
  117. float sy = std::sin(y);
  118. float cz = std::cos(z);
  119. float sz = std::sin(z);
  120. float cxsy = cx * sy;
  121. float sxsy = sx * sy;
  122. Mat4 matrix;
  123. matrix.m[0] = cy * cz;
  124. matrix.m[1] = -cy * sz;
  125. matrix.m[2] = sy;
  126. matrix.m[3] = 0.0f;
  127. matrix.m[4] = cxsy * cz + cx * sz;
  128. matrix.m[5] = -cxsy * sz + cx * cz;
  129. matrix.m[6] = -sx * cy;
  130. matrix.m[7] = 0.0f;
  131. matrix.m[8] = -sxsy * cz + sx * sz;
  132. matrix.m[9] = sxsy * sz + sx * cz;
  133. matrix.m[10] = cx * cy;
  134. matrix.m[11] = 0.0f;
  135. matrix.m[12] = 0.0f;
  136. matrix.m[13] = 0.0f;
  137. matrix.m[14] = 0.0f;
  138. matrix.m[15] = 1.0f;
  139. return matrix;
  140. }
  141. #endif
  142. VRGenericHeadTracker::VRGenericHeadTracker()
  143. : _localPosition(Vec3::ZERO)
  144. {
  145. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  146. _motionMgr = [[CMMotionManager alloc] init];
  147. #endif
  148. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  149. startTracking();
  150. #endif
  151. }
  152. VRGenericHeadTracker::~VRGenericHeadTracker()
  153. {
  154. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  155. stopTracking();
  156. #endif
  157. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  158. [(CMMotionManager*)_motionMgr release];
  159. #endif
  160. }
  161. void VRGenericHeadTracker::startTracking()
  162. {
  163. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  164. CMMotionManager* motionMgr = (CMMotionManager*)_motionMgr;
  165. if (motionMgr.isDeviceMotionAvailable && !motionMgr.isDeviceMotionActive)
  166. {
  167. [motionMgr startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];
  168. }
  169. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  170. if (orientation == UIInterfaceOrientationLandscapeLeft)
  171. {
  172. _deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, 90.f);
  173. }
  174. else if (orientation == UIInterfaceOrientationLandscapeRight)
  175. {
  176. _deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, -90.f);
  177. }
  178. // the inertial reference frame has z up and x forward, while the world has z out and x right
  179. _worldToInertialReferenceFrame = getRotateEulerMatrix(-90.f, 0.f, 90.f);
  180. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  181. _deviceToDisplay = getRotateEulerMatrix(0.f, 0.f, -90.f);
  182. _worldToInertialReferenceFrame = getRotateEulerMatrix(-90.f, 0.f, 90.f);
  183. JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxHelper", "enableAccelerometer");
  184. JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxHelper", "enableCompass");
  185. #endif
  186. }
  187. void VRGenericHeadTracker::stopTracking()
  188. {
  189. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  190. [(CMMotionManager*)_motionMgr stopDeviceMotionUpdates];
  191. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  192. Device::setAccelerometerEnabled(false);
  193. #endif
  194. }
  195. Vec3 VRGenericHeadTracker::getLocalPosition()
  196. {
  197. return _localPosition;
  198. }
  199. Mat4 VRGenericHeadTracker::getLocalRotation()
  200. {
  201. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
  202. CMMotionManager* motionMgr = (CMMotionManager*)_motionMgr;
  203. CMDeviceMotion* motion = motionMgr.deviceMotion;
  204. if (motion) {
  205. CMRotationMatrix rotationMatrix = motion.attitude.rotationMatrix;
  206. Mat4 inertialReferenceFrameToDevice0 = matrixFromRotationMatrix(rotationMatrix); // note the matrix inversion
  207. Mat4 inertialReferenceFrameToDevice = inertialReferenceFrameToDevice0.getTransposed();
  208. Mat4 worldToDevice = inertialReferenceFrameToDevice * _worldToInertialReferenceFrame;
  209. return _deviceToDisplay * worldToDevice;
  210. }
  211. // bug!
  212. return Mat4::IDENTITY;
  213. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  214. static Vec3 prevAccel = Vec3(0,0,0);
  215. static Vec3 prevCompass = Vec3(0,0,0);
  216. Vec3 accel = JniHelper::callStaticVec3Method("org/cocos2dx/lib/Cocos2dxHelper", "getAccelValue");
  217. Vec3 compass = JniHelper::callStaticVec3Method("org/cocos2dx/lib/Cocos2dxHelper", "getCompassValue");
  218. // CCLOG("accel: %f, %f, %f.... compass: %f, %f, %f", accel.x, accel.y, accel.z, compass.x, compass.y, compass.z);
  219. prevAccel = lowPass(accel, prevAccel);
  220. prevCompass = lowPass(compass, prevCompass);
  221. // CCLOG("low pass accel: %f, %f, %f.... compass: %f, %f, %f", prevAccel.x, prevAccel.y, prevAccel.z, prevCompass.x, prevCompass.y, prevCompass.z);
  222. Mat4 rotMatrix = getRotationMatrix(prevAccel, prevCompass);
  223. Mat4 inertialReferenceFrameToDevice(rotMatrix);
  224. Mat4 worldToDevice = inertialReferenceFrameToDevice * _worldToInertialReferenceFrame;
  225. return _deviceToDisplay * worldToDevice;
  226. #else
  227. return Mat4::IDENTITY;
  228. #endif
  229. }
  230. NS_CC_END