TransformationHistory.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "RakAssert.h"
  11. #include "RakMemoryOverride.h"
  12. #include "TransformationHistory.h"
  13. TransformationHistoryCell::TransformationHistoryCell()
  14. {
  15. }
  16. TransformationHistoryCell::TransformationHistoryCell(RakNet::TimeMS t, const Ogre::Vector3& pos, const Ogre::Vector3& vel, const Ogre::Quaternion& quat ) :
  17. time(t),
  18. velocity(vel),
  19. position(pos),
  20. orientation(quat)
  21. {
  22. }
  23. void TransformationHistory::Init(RakNet::TimeMS maxWriteInterval, RakNet::TimeMS maxHistoryTime)
  24. {
  25. writeInterval=maxWriteInterval;
  26. maxHistoryLength = maxHistoryTime/maxWriteInterval+1;
  27. history.ClearAndForceAllocation(maxHistoryLength+1, _FILE_AND_LINE_ );
  28. RakAssert(writeInterval>0);
  29. }
  30. void TransformationHistory::Write(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Quaternion &orientation, RakNet::TimeMS curTimeMS)
  31. {
  32. if (history.Size()==0)
  33. {
  34. history.Push(TransformationHistoryCell(curTimeMS,position,velocity,orientation), _FILE_AND_LINE_ );
  35. }
  36. else
  37. {
  38. const TransformationHistoryCell &lastCell = history.PeekTail();
  39. if (curTimeMS-lastCell.time>=writeInterval)
  40. {
  41. history.Push(TransformationHistoryCell(curTimeMS,position,velocity,orientation), _FILE_AND_LINE_ );
  42. if (history.Size()>maxHistoryLength)
  43. history.Pop();
  44. }
  45. }
  46. }
  47. void TransformationHistory::Overwrite(const Ogre::Vector3 &position, const Ogre::Vector3 &velocity, const Ogre::Quaternion &orientation, RakNet::TimeMS when)
  48. {
  49. int historySize = history.Size();
  50. if (historySize==0)
  51. {
  52. history.Push(TransformationHistoryCell(when,position,velocity,orientation), _FILE_AND_LINE_ );
  53. }
  54. else
  55. {
  56. // Find the history matching this time, and change the values.
  57. int i;
  58. for (i=historySize-1; i>=0; i--)
  59. {
  60. TransformationHistoryCell &cell = history[i];
  61. if (when >= cell.time)
  62. {
  63. if (i==historySize-1 && when-cell.time>=writeInterval)
  64. {
  65. // Not an overwrite at all, but a new cell
  66. history.Push(TransformationHistoryCell(when,position,velocity,orientation), _FILE_AND_LINE_ );
  67. if (history.Size()>maxHistoryLength)
  68. history.Pop();
  69. return;
  70. }
  71. cell.time=when;
  72. cell.position=position;
  73. cell.velocity=velocity;
  74. cell.orientation=orientation;
  75. return;
  76. }
  77. }
  78. }
  79. }
  80. TransformationHistory::ReadResult TransformationHistory::Read(Ogre::Vector3 *position, Ogre::Vector3 *velocity, Ogre::Quaternion *orientation,
  81. RakNet::TimeMS when, RakNet::TimeMS curTime)
  82. {
  83. int historySize = history.Size();
  84. if (historySize==0)
  85. {
  86. return VALUES_UNCHANGED;
  87. }
  88. int i;
  89. for (i=historySize-1; i>=0; i--)
  90. {
  91. const TransformationHistoryCell &cell = history[i];
  92. if (when >= cell.time)
  93. {
  94. if (i==historySize-1)
  95. {
  96. if (curTime<=cell.time)
  97. return VALUES_UNCHANGED;
  98. float divisor = (float)(curTime-cell.time);
  99. RakAssert(divisor!=0.0f);
  100. float lerp = (float)(when - cell.time) / divisor;
  101. if (position)
  102. *position=cell.position + (*position-cell.position) * lerp;
  103. if (velocity)
  104. *velocity=cell.velocity + (*velocity-cell.velocity) * lerp;
  105. if (orientation)
  106. *orientation = Ogre::Quaternion::Slerp(lerp, cell.orientation, *orientation,true);
  107. }
  108. else
  109. {
  110. const TransformationHistoryCell &nextCell = history[i+1];
  111. float divisor = (float)(nextCell.time-cell.time);
  112. RakAssert(divisor!=0.0f);
  113. float lerp = (float)(when - cell.time) / divisor;
  114. if (position)
  115. *position=cell.position + (nextCell.position-cell.position) * lerp;
  116. if (velocity)
  117. *velocity=cell.velocity + (nextCell.velocity-cell.velocity) * lerp;
  118. if (orientation)
  119. *orientation = Ogre::Quaternion::Slerp(lerp, cell.orientation, nextCell.orientation,true);
  120. }
  121. return INTERPOLATED;
  122. }
  123. }
  124. // Return the oldest one
  125. const TransformationHistoryCell &cell = history.Peek();
  126. if (position)
  127. *position=cell.position;
  128. if (orientation)
  129. *orientation=cell.orientation;
  130. if (velocity)
  131. *velocity=cell.velocity;
  132. return READ_OLDEST;
  133. }
  134. void TransformationHistory::Clear(void)
  135. {
  136. history.Clear(_FILE_AND_LINE_);
  137. }
粤ICP备19079148号