reality.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // Reality.H
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 02/23/97 JMI Started.
  23. //
  24. // 02/23/97 JMI Changed calling convention making acceleration functions
  25. // less tied to gravity and, therefore, more versatile.
  26. //
  27. // 02/23/97 JMI Accidentally mixed distance and velocity in Accelerate().
  28. //
  29. // 02/24/97 JMI For the gravity constant I had calculated the equivalent
  30. // of a meter in pixels but forgot to multiply by 9.8.
  31. // What a dumb ass?!
  32. //
  33. // 02/24/97 JMI Changed accel to dAccel (a double instead of a templated
  34. // parameter) so it could be defaulted.
  35. // Also, changed Accel() to AdjustVel().
  36. //
  37. // 02/24/97 JMI Now Map2DTo3D can take two different templated types.
  38. //
  39. // 06/26/97 JMI Moved Map2DTo3D from here to CRealm.
  40. //
  41. //////////////////////////////////////////////////////////////////////////////
  42. //
  43. // Reality constants and functions for postal.
  44. //
  45. //////////////////////////////////////////////////////////////////////////////
  46. #ifndef REALITY_H
  47. #define REALITY_H
  48. //////////////////////////////////////////////////////////////////////////////
  49. // C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
  50. //////////////////////////////////////////////////////////////////////////////
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // RSPiX Headers.
  53. ///////////////////////////////////////////////////////////////////////////////
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // Postal includes.
  56. ///////////////////////////////////////////////////////////////////////////////
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // Macros.
  59. ///////////////////////////////////////////////////////////////////////////////
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // Typedefs.
  62. ///////////////////////////////////////////////////////////////////////////////
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // Constants/Tunables.
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // I figure a meter is about half a man's height. Our man is about 50 pixels
  67. // tall.
  68. const double g_dAccelerationDueToGravity = -(25 * 9.8); // Pixels/(sec^2) upward.
  69. ///////////////////////////////////////////////////////////////////////////////
  70. // Protos.
  71. ///////////////////////////////////////////////////////////////////////////////
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // Inlines/Templates.
  74. ///////////////////////////////////////////////////////////////////////////////
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // Adjust provided velocity by drag for the given period.
  77. ///////////////////////////////////////////////////////////////////////////////
  78. template<class VelT>
  79. void AdjustVel( // Returns nothing.
  80. VelT* pvel, // In: Velocity to affect by specified acceleration.
  81. // Out: New velocity.
  82. double dDeltaTime, // In: Duration over which acceleration affected
  83. // *pvel in seconds.
  84. double dAccel = g_dAccelerationDueToGravity) // In: Acceleration value
  85. // to apply (in Pixels/Sec^2).
  86. {
  87. *pvel += (VelT)(dAccel * dDeltaTime);
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////
  90. // Adjust provided vertical position and velocity by gravity for the given
  91. // period.
  92. ///////////////////////////////////////////////////////////////////////////////
  93. template<class PosT, class VelT>
  94. void AdjustPosVel( // Returns nothing.
  95. PosT* ppos, // In: Current position.
  96. // Out: New position.
  97. VelT* pvel, // In: Velocity to affect by drag.
  98. // Out: New velocity.
  99. double dDeltaTime, // In: Duration over which drag affected
  100. // *ppos in seconds.
  101. double dAccel = g_dAccelerationDueToGravity) // In: Drag value to apply
  102. // (in Pixels/Sec^2).
  103. {
  104. VelT velDelta = 0;
  105. AdjustVel(&velDelta, dDeltaTime, dAccel);
  106. // On the average during this duration, the position was affected
  107. // by the average velocity change (hence velDelta / 2).
  108. *ppos += (*pvel + velDelta / 2) * dDeltaTime;
  109. // Adjust velocity.
  110. *pvel += velDelta;
  111. }
  112. #endif // REALITY_H
  113. ///////////////////////////////////////////////////////////////////////////////
  114. // EOF
  115. ///////////////////////////////////////////////////////////////////////////////