oculusVRSensorData.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/input/oculusVR/oculusVRDevice.h"
  23. #include "platform/input/oculusVR/oculusVRSensorData.h"
  24. #include "platform/input/oculusVR/oculusVRUtil.h"
  25. #include "console/console.h"
  26. OculusVRSensorData::OculusVRSensorData()
  27. {
  28. reset();
  29. }
  30. void OculusVRSensorData::reset()
  31. {
  32. mDataSet = false;
  33. mStatusFlags = 0;
  34. }
  35. void OculusVRSensorData::setData(ovrTrackingState& data, const F32& maxAxisRadius)
  36. {
  37. // Sensor rotation & position
  38. OVR::Posef pose = data.HeadPose.ThePose;
  39. OVR::Quatf orientation = pose.Rotation;
  40. OVR::Vector3f position = data.HeadPose.ThePose.Position;
  41. mPosition = Point3F(-position.z, position.x, position.y);
  42. mPosition *= OculusVRDevice::smPositionTrackingScale;
  43. OVR::Matrix4f orientMat(orientation);
  44. OculusVRUtil::convertRotation(orientMat.M, mRot);
  45. mRotQuat.set(mRot);
  46. // Sensor rotation in Euler format
  47. OculusVRUtil::convertRotation(orientation, mRotEuler); // mRotEuler == pitch, roll, yaw FROM yaw, pitch, roll
  48. //mRotEuler = EulerF(0,0,0);
  49. float hmdYaw, hmdPitch, hmdRoll;
  50. orientation.GetEulerAngles<OVR::Axis_Y, OVR::Axis_X, OVR::Axis_Z>(&hmdYaw, &hmdPitch, &hmdRoll);
  51. // Sensor rotation as axis
  52. OculusVRUtil::calculateAxisRotation(mRot, maxAxisRadius, mRotAxis);
  53. // Sensor raw values
  54. OVR::Vector3f accel = data.HeadPose.LinearAcceleration;
  55. OculusVRUtil::convertAcceleration(accel, mAcceleration);
  56. OVR::Vector3f angVel = data.HeadPose.AngularVelocity;
  57. OculusVRUtil::convertAngularVelocity(angVel, mAngVelocity);
  58. OVR::Vector3f mag = data.RawSensorData.Magnetometer;
  59. OculusVRUtil::convertMagnetometer(mag, mMagnetometer);
  60. mStatusFlags = data.StatusFlags;
  61. mDataSet = true;
  62. }
  63. U32 OculusVRSensorData::compare(OculusVRSensorData* other, bool doRawCompare)
  64. {
  65. S32 result = DIFF_NONE;
  66. // Check rotation
  67. if(mRotEuler.x != other->mRotEuler.x || mRotEuler.y != other->mRotEuler.y || mRotEuler.z != other->mRotEuler.z || !mDataSet)
  68. {
  69. result |= DIFF_ROT;
  70. }
  71. // Check rotation as axis
  72. if(mRotAxis.x != other->mRotAxis.x || !mDataSet)
  73. {
  74. result |= DIFF_ROTAXISX;
  75. }
  76. if(mRotAxis.y != other->mRotAxis.y || !mDataSet)
  77. {
  78. result |= DIFF_ROTAXISY;
  79. }
  80. // Check raw values
  81. if(doRawCompare)
  82. {
  83. if(mAcceleration.x != other->mAcceleration.x || mAcceleration.y != other->mAcceleration.y || mAcceleration.z != other->mAcceleration.z || !mDataSet)
  84. {
  85. result |= DIFF_ACCEL;
  86. }
  87. if(mAngVelocity.x != other->mAngVelocity.x || mAngVelocity.y != other->mAngVelocity.y || mAngVelocity.z != other->mAngVelocity.z || !mDataSet)
  88. {
  89. result |= DIFF_ANGVEL;
  90. }
  91. if(mMagnetometer.x != other->mMagnetometer.x || mMagnetometer.y != other->mMagnetometer.y || mMagnetometer.z != other->mMagnetometer.z || !mDataSet)
  92. {
  93. result |= DIFF_MAG;
  94. }
  95. }
  96. if (other->mStatusFlags != mStatusFlags)
  97. {
  98. result |= DIFF_STATUS;
  99. }
  100. return result;
  101. }