TransformationHistory.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #ifndef __TRANFORMATION_HISTORY_H
  11. #define __TRANFORMATION_HISTORY_H
  12. #include "RakNetTypes.h"
  13. #include "OgreVector3.h"
  14. #include "OgreQuaternion.h"
  15. #include "DS_Queue.h"
  16. #include "RakMemoryOverride.h"
  17. struct TransformationHistoryCell
  18. {
  19. TransformationHistoryCell();
  20. TransformationHistoryCell(RakNet::TimeMS t, const Ogre::Vector3& pos, const Ogre::Vector3& vel, const Ogre::Quaternion& quat );
  21. RakNet::TimeMS time;
  22. Ogre::Vector3 position;
  23. Ogre::Quaternion orientation;
  24. Ogre::Vector3 velocity;
  25. };
  26. /// \brief This class stores a series of data points consisting of position, orientation, and velocity
  27. /// Data points are written using Write(). The number of points stored is determined by the values passed to Init()
  28. /// Points are read out using Read(). Read() will interpolate between two known points given a time between those points, or between the last known and current point.
  29. /// For smooth interpolation, render entities by reading their past positions by whatever amount of time your update interval is.
  30. class TransformationHistory
  31. {
  32. public:
  33. /// \brief Call to setup this class with required parameters
  34. /// maxWriteInterval should be equal to or less than the interval between updates for a given entity
  35. /// maxHistoryTime is the maximum amount of time you want to read in the past
  36. /// \param[in] maxWriteInterval Minimum amount of time that must elapse between new data points added with Write.
  37. /// \param[in] maxHistoryTime How long to store data points before they expire
  38. void Init(RakNet::TimeMS maxWriteInterval, RakNet::TimeMS maxHistoryTime);
  39. /// \brief Adds a new data point to the end of the queue
  40. /// If less than maxWriteInterval has elapsed since the last call, the Write() call is ignored.
  41. /// \param[in] position Position to write
  42. /// \param[in] velocity Velocity to write
  43. /// \param[in] orientation Orientation to write
  44. /// \param[in] curTimeMS Time when data point was generated, which should generally increment each call
  45. void Write(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Quaternion &orientation, RakNet::TimeMS curTimeMS);
  46. /// \brief Same as Write(), except that if the point is in the past, an older point updated
  47. void Overwrite(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Quaternion &orientation, RakNet::TimeMS when);
  48. enum ReadResult
  49. {
  50. // We are reading so far in the past there is no data yet
  51. READ_OLDEST,
  52. // We are not reading in the past, so the input parameters stay the same
  53. VALUES_UNCHANGED,
  54. // We are reading in the past
  55. INTERPOLATED
  56. };
  57. /// \brief Read an interpolated point in the psast
  58. /// Parameters are in/out, modified to reflect the history.
  59. /// \param[in/out] position As input, the current position of the object at \a curTime. As output, the position of the object at \when. Pass 0 to ignore.
  60. /// \param[in/out] velocity As input, the current velocity of the object at \a curTime. As output, the velocity of the object at \when. Pass 0 to ignore.
  61. /// \param[in/out] orientation As input, the current orientation of the object at \a curTime. As output, the orientation of the object at \when. Pass 0 to ignore.
  62. /// \param[in] when The time at which you want to read.
  63. /// \param[in] curTime Right now
  64. /// \return What method was used to calculate outputs
  65. ReadResult Read(Ogre::Vector3 *position, Ogre::Vector3 *velocity, Ogre::Quaternion *orientation,
  66. RakNet::TimeMS when, RakNet::TimeMS curTime);
  67. /// \brief Clear all values in the history
  68. void Clear(void);
  69. protected:
  70. DataStructures::Queue<TransformationHistoryCell> history;
  71. unsigned maxHistoryLength;
  72. RakNet::TimeMS writeInterval;
  73. };
  74. #endif
粤ICP备19079148号