Force_Grab.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #ifdef _D3XP
  23. #include "../Game_local.h"
  24. CLASS_DECLARATION( idForce, idForce_Grab )
  25. END_CLASS
  26. /*
  27. ================
  28. idForce_Grab::Save
  29. ================
  30. */
  31. void idForce_Grab::Save( idSaveGame *savefile ) const {
  32. savefile->WriteFloat( damping );
  33. savefile->WriteVec3( goalPosition );
  34. savefile->WriteFloat( distanceToGoal );
  35. savefile->WriteInt( id );
  36. }
  37. /*
  38. ================
  39. idForce_Grab::Restore
  40. ================
  41. */
  42. void idForce_Grab::Restore( idRestoreGame *savefile ) {
  43. //Note: Owner needs to call set physics
  44. savefile->ReadFloat( damping );
  45. savefile->ReadVec3( goalPosition );
  46. savefile->ReadFloat( distanceToGoal );
  47. savefile->ReadInt( id );
  48. }
  49. /*
  50. ================
  51. idForce_Grab::idForce_Grab
  52. ================
  53. */
  54. idForce_Grab::idForce_Grab( void ) {
  55. damping = 0.5f;
  56. physics = NULL;
  57. id = 0;
  58. }
  59. /*
  60. ================
  61. idForce_Grab::~idForce_Grab
  62. ================
  63. */
  64. idForce_Grab::~idForce_Grab( void ) {
  65. }
  66. /*
  67. ================
  68. idForce_Grab::Init
  69. ================
  70. */
  71. void idForce_Grab::Init( float damping ) {
  72. if ( damping >= 0.0f && damping < 1.0f ) {
  73. this->damping = damping;
  74. }
  75. }
  76. /*
  77. ================
  78. idForce_Grab::SetPhysics
  79. ================
  80. */
  81. void idForce_Grab::SetPhysics( idPhysics *phys, int id, const idVec3 &goal ) {
  82. this->physics = phys;
  83. this->id = id;
  84. this->goalPosition = goal;
  85. }
  86. /*
  87. ================
  88. idForce_Grab::SetGoalPosition
  89. ================
  90. */
  91. void idForce_Grab::SetGoalPosition( const idVec3 &goal ) {
  92. this->goalPosition = goal;
  93. }
  94. /*
  95. =================
  96. idForce_Grab::GetDistanceToGoal
  97. =================
  98. */
  99. float idForce_Grab::GetDistanceToGoal( void ) {
  100. return distanceToGoal;
  101. }
  102. /*
  103. ================
  104. idForce_Grab::Evaluate
  105. ================
  106. */
  107. void idForce_Grab::Evaluate( int time ) {
  108. if ( !physics ) {
  109. return;
  110. }
  111. idVec3 forceDir, v, objectCenter;
  112. float forceAmt;
  113. float mass = physics->GetMass(id);
  114. objectCenter = physics->GetAbsBounds(id).GetCenter();
  115. if ( g_grabberRandomMotion.GetBool() && !gameLocal.isMultiplayer ) {
  116. // Jitter the objectCenter around so it doesn't remain stationary
  117. float SinOffset = idMath::Sin( (float)(gameLocal.time)/66.f );
  118. float randScale1 = gameLocal.random.RandomFloat();
  119. float randScale2 = gameLocal.random.CRandomFloat();
  120. objectCenter.x += ( SinOffset * 3.5f * randScale1 ) + ( randScale2 * 1.2f );
  121. objectCenter.y += ( SinOffset * -3.5f * randScale1 ) + ( randScale2 * 1.4f );
  122. objectCenter.z += ( SinOffset * 2.4f * randScale1 ) + ( randScale2 * 1.6f );
  123. }
  124. forceDir = goalPosition - objectCenter;
  125. distanceToGoal = forceDir.Normalize();
  126. float temp = distanceToGoal;
  127. if ( temp > 12.f && temp < 32.f ) {
  128. temp = 32.f;
  129. }
  130. forceAmt = (1000.f * mass) + (500.f * temp * mass);
  131. if ( forceAmt/mass > 120000.f ) {
  132. forceAmt = 120000.f * mass;
  133. }
  134. physics->AddForce( id, objectCenter, forceDir * forceAmt );
  135. if ( distanceToGoal < 196.f ) {
  136. v = physics->GetLinearVelocity( id );
  137. physics->SetLinearVelocity( v * damping, id );
  138. }
  139. if ( distanceToGoal < 16.f ) {
  140. v = physics->GetAngularVelocity(id);
  141. if ( v.LengthSqr() > Square(8) ) {
  142. physics->SetAngularVelocity( v * 0.99999f, id );
  143. }
  144. }
  145. }
  146. /*
  147. ================
  148. idForce_Grab::RemovePhysics
  149. ================
  150. */
  151. void idForce_Grab::RemovePhysics( const idPhysics *phys ) {
  152. if ( physics == phys ) {
  153. physics = NULL;
  154. }
  155. }
  156. #endif // _D3XP