dayzplayerimplementheading.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. DayZPlayerImplement
  3. this file is implemenation of dayzPlayer in script
  4. - logic of movement
  5. - camera switching logic
  6. */
  7. class DayZPlayerImplementHeading
  8. {
  9. //-------------------------------------------------------------
  10. //!
  11. //! This HeadingModel - Clamps heading
  12. //!
  13. //!
  14. static bool ClampHeading (float pDt, SDayZPlayerHeadingModel pModel, out float pLastHeadingDiff)
  15. {
  16. float aDiff = pModel.m_fHeadingAngle - pModel.m_fOrientationAngle;
  17. if (aDiff < -Math.PI)
  18. {
  19. aDiff += Math.PI2;
  20. }
  21. else if (aDiff > Math.PI)
  22. {
  23. aDiff -= Math.PI2;
  24. }
  25. // Print("Heading model: or: " + pModel.m_fOrientationAngle.ToString() + " head:" + pModel.m_fHeadingAngle.ToString() + " dif:" + aDiff.ToString());
  26. if (pLastHeadingDiff < -Math.PI_HALF && aDiff > 0)
  27. {
  28. aDiff = -Math.PI + 0.01;
  29. pLastHeadingDiff = aDiff;
  30. pModel.m_fHeadingAngle = pModel.m_fOrientationAngle + aDiff;
  31. // Print("-APA- : or: " + pModel.m_fOrientationAngle.ToString() + " head:" + pModel.m_fHeadingAngle.ToString() + " dif:" + aDiff.ToString());
  32. return true; // modify heading
  33. }
  34. else if (pLastHeadingDiff > Math.PI_HALF && aDiff < 0)
  35. {
  36. aDiff = Math.PI - 0.01;
  37. pLastHeadingDiff = aDiff;
  38. pModel.m_fHeadingAngle = pModel.m_fOrientationAngle + aDiff;
  39. // Print("-APA- : or: " + pModel.m_fOrientationAngle.ToString() + " head:" + pModel.m_fHeadingAngle.ToString() + " dif:" + aDiff.ToString());
  40. return true; // modify heading
  41. }
  42. pLastHeadingDiff = aDiff;
  43. // Print("Heading model diff " + aDiff.ToString());
  44. return false;
  45. }
  46. //-------------------------------------------------------------
  47. //!
  48. //! This HeadingModel - Rotates orientations - so player slides
  49. //!
  50. static float CONST_ROTLIMIT = Math.PI * 0.95;
  51. //!
  52. static bool RotateOrient (float pDt, SDayZPlayerHeadingModel pModel, out float pLastHeadingDiff)
  53. {
  54. float aDiff = pModel.m_fHeadingAngle - pModel.m_fOrientationAngle;
  55. while (aDiff < -Math.PI)
  56. {
  57. aDiff += Math.PI2;
  58. }
  59. while (aDiff > Math.PI)
  60. {
  61. aDiff -= Math.PI2;
  62. }
  63. // Print("Heading model: or: " + pModel.m_fOrientationAngle.ToString() + " head:" + pModel.m_fHeadingAngle.ToString() + " dif:" + aDiff.ToString());
  64. if (pLastHeadingDiff < -Math.PI_HALF && aDiff > 0)
  65. {
  66. aDiff -= Math.PI2;
  67. }
  68. else if (pLastHeadingDiff > Math.PI_HALF && aDiff < 0)
  69. {
  70. aDiff += Math.PI2;
  71. }
  72. pLastHeadingDiff = aDiff;
  73. if (aDiff < -CONST_ROTLIMIT)
  74. {
  75. // character is somehow stucked (happens in prone stance)
  76. if (aDiff < -(Math.PI_HALF + CONST_ROTLIMIT))
  77. {
  78. pLastHeadingDiff = 0;
  79. return false;
  80. }
  81. pModel.m_fOrientationAngle += aDiff + CONST_ROTLIMIT;
  82. return true;
  83. }
  84. else if (aDiff > CONST_ROTLIMIT)
  85. {
  86. // character is somehow stucked (happens in prone stance)
  87. if (aDiff > (Math.PI_HALF + CONST_ROTLIMIT))
  88. {
  89. pLastHeadingDiff = 0;
  90. return false;
  91. }
  92. pModel.m_fOrientationAngle += aDiff - CONST_ROTLIMIT;
  93. return true;
  94. }
  95. // Print("Heading model diff " + aDiff.ToString());
  96. return false;
  97. }
  98. static bool RestrictHeading(float pDt, SDayZPlayerHeadingModel pModel, out float pLastHeadingDiff, HeadingRestrictData restrictData)
  99. {
  100. pModel.m_fHeadingAngle = ClampAngle(pModel.m_fHeadingAngle, restrictData);
  101. return true;
  102. }
  103. // Clamp angle between -PI to PI
  104. protected static float ClampAngle(float angle, HeadingRestrictData restrictData)
  105. {
  106. float testOtherDir;
  107. if (restrictData.m_RestrictedR > restrictData.m_RestrictedL)
  108. {
  109. if (angle > restrictData.m_RestrictedL && angle < restrictData.m_RestrictedR)
  110. {
  111. //Print("STANDARD NOCLAMP: " + angle*Math.RAD2DEG + " |MIN: " + restrictData.m_RestrictedL*Math.RAD2DEG + " |MAX: " + restrictData.m_RestrictedR*Math.RAD2DEG);
  112. return angle;
  113. }
  114. else
  115. {
  116. //Print("STANDARD CLAMP: " + angle*Math.RAD2DEG + " |MIN: " + restrictData.m_RestrictedL*Math.RAD2DEG + " |MAX: " + restrictData.m_RestrictedR*Math.RAD2DEG);
  117. if (angle > restrictData.m_RestrictedR + 90 * Math.DEG2RAD) // check if restrictData.m_RestrictedR/restrictData.m_RestrictedL is close to -PI and the new angle flips to +PI or vice versa
  118. return restrictData.m_RestrictedL;
  119. else if (angle < restrictData.m_RestrictedL - 90 * Math.DEG2RAD)
  120. return restrictData.m_RestrictedR;
  121. return Math.Clamp(angle, restrictData.m_RestrictedL, restrictData.m_RestrictedR);
  122. }
  123. }
  124. else if (restrictData.m_RestrictedR < restrictData.m_RestrictedL) // angle is restrited through 180 -> -180, clamping follows different rules
  125. {
  126. if ((angle >= -180 && angle < restrictData.m_RestrictedR) || (angle <= 180 && angle > restrictData.m_RestrictedL))
  127. {
  128. //Print("INVERSE NOCLAMP: " + angle*Math.RAD2DEG + " |MIN: " + restrictData.m_RestrictedL*Math.RAD2DEG + " |MAX: " + restrictData.m_RestrictedR*Math.RAD2DEG);
  129. return angle;
  130. }
  131. else
  132. {
  133. //Print("INVERSE CLAMP: " + angle*Math.RAD2DEG + " |MIN: " + restrictData.m_RestrictedL*Math.RAD2DEG + " |MAX: " + restrictData.m_RestrictedR*Math.RAD2DEG);
  134. if (angle < 0)
  135. {
  136. testOtherDir = Math.AbsFloat(restrictData.m_RestrictedR - angle);
  137. if (testOtherDir < restrictData.m_AngleRangeInverted - testOtherDir)
  138. return restrictData.m_RestrictedR;
  139. else
  140. return restrictData.m_RestrictedL;
  141. }
  142. else if (angle >= 0)
  143. {
  144. testOtherDir = Math.AbsFloat(restrictData.m_RestrictedL - angle);
  145. if (testOtherDir < restrictData.m_AngleRangeInverted - testOtherDir)
  146. return restrictData.m_RestrictedL;
  147. else
  148. return restrictData.m_RestrictedR;
  149. }
  150. return angle;
  151. }
  152. }
  153. return angle;
  154. }
  155. static bool NoHeading(float pDt, SDayZPlayerHeadingModel pModel, out float pLastHeadingDiff)
  156. {
  157. pLastHeadingDiff = 0;
  158. pModel.m_fHeadingAngle = pModel.m_fOrientationAngle;
  159. return true;
  160. }
  161. }
  162. class HeadingRestrictData
  163. {
  164. float m_RestrictedL; // restricted angle left
  165. float m_RestrictedR; // restricted angle right
  166. float m_AngleRangeInverted; // the size of the restricted angle
  167. void InitData(float currentHeading, Vector2 restrictedAngles)
  168. {
  169. m_RestrictedL = currentHeading + (Math.DEG2RAD * restrictedAngles.x);
  170. if (m_RestrictedL < -Math.PI)
  171. m_RestrictedL += Math.PI2;
  172. m_RestrictedR = currentHeading + (Math.DEG2RAD * restrictedAngles.y);
  173. if (m_RestrictedR > Math.PI)
  174. m_RestrictedR -= Math.PI2;
  175. m_AngleRangeInverted = Math.PI2 - (Math.AbsFloat(restrictedAngles.x * Math.DEG2RAD) + restrictedAngles.y * Math.DEG2RAD);
  176. }
  177. }