Force_Spring.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "../Game_local.h"
  23. CLASS_DECLARATION( idForce, idForce_Spring )
  24. END_CLASS
  25. /*
  26. ================
  27. idForce_Spring::idForce_Spring
  28. ================
  29. */
  30. idForce_Spring::idForce_Spring( void ) {
  31. Kstretch = 100.0f;
  32. Kcompress = 100.0f;
  33. damping = 0.0f;
  34. restLength = 0.0f;
  35. physics1 = NULL;
  36. id1 = 0;
  37. p1 = vec3_zero;
  38. physics2 = NULL;
  39. id2 = 0;
  40. p2 = vec3_zero;
  41. }
  42. /*
  43. ================
  44. idForce_Spring::~idForce_Spring
  45. ================
  46. */
  47. idForce_Spring::~idForce_Spring( void ) {
  48. }
  49. /*
  50. ================
  51. idForce_Spring::InitSpring
  52. ================
  53. */
  54. void idForce_Spring::InitSpring( float Kstretch, float Kcompress, float damping, float restLength ) {
  55. this->Kstretch = Kstretch;
  56. this->Kcompress = Kcompress;
  57. this->damping = damping;
  58. this->restLength = restLength;
  59. }
  60. /*
  61. ================
  62. idForce_Spring::SetPosition
  63. ================
  64. */
  65. void idForce_Spring::SetPosition( idPhysics *physics1, int id1, const idVec3 &p1, idPhysics *physics2, int id2, const idVec3 &p2 ) {
  66. this->physics1 = physics1;
  67. this->id1 = id1;
  68. this->p1 = p1;
  69. this->physics2 = physics2;
  70. this->id2 = id2;
  71. this->p2 = p2;
  72. }
  73. /*
  74. ================
  75. idForce_Spring::Evaluate
  76. ================
  77. */
  78. void idForce_Spring::Evaluate( int time ) {
  79. float length;
  80. idMat3 axis;
  81. idVec3 pos1, pos2, velocity1, velocity2, force, dampingForce;
  82. impactInfo_t info;
  83. pos1 = p1;
  84. pos2 = p2;
  85. velocity1 = velocity2 = vec3_origin;
  86. if ( physics1 ) {
  87. axis = physics1->GetAxis( id1 );
  88. pos1 = physics1->GetOrigin( id1 );
  89. pos1 += p1 * axis;
  90. if ( damping > 0.0f ) {
  91. physics1->GetImpactInfo( id1, pos1, &info );
  92. velocity1 = info.velocity;
  93. }
  94. }
  95. if ( physics2 ) {
  96. axis = physics2->GetAxis( id2 );
  97. pos2 = physics2->GetOrigin( id2 );
  98. pos2 += p2 * axis;
  99. if ( damping > 0.0f ) {
  100. physics2->GetImpactInfo( id2, pos2, &info );
  101. velocity2 = info.velocity;
  102. }
  103. }
  104. force = pos2 - pos1;
  105. dampingForce = ( damping * ( ((velocity2 - velocity1) * force) / (force * force) ) ) * force;
  106. length = force.Normalize();
  107. // if the spring is stretched
  108. if ( length > restLength ) {
  109. if ( Kstretch > 0.0f ) {
  110. force = ( Square( length - restLength ) * Kstretch ) * force - dampingForce;
  111. if ( physics1 ) {
  112. physics1->AddForce( id1, pos1, force );
  113. }
  114. if ( physics2 ) {
  115. physics2->AddForce( id2, pos2, -force );
  116. }
  117. }
  118. }
  119. else {
  120. if ( Kcompress > 0.0f ) {
  121. force = ( Square( length - restLength ) * Kcompress ) * force - dampingForce;
  122. if ( physics1 ) {
  123. physics1->AddForce( id1, pos1, -force );
  124. }
  125. if ( physics2 ) {
  126. physics2->AddForce( id2, pos2, force );
  127. }
  128. }
  129. }
  130. }
  131. /*
  132. ================
  133. idForce_Spring::RemovePhysics
  134. ================
  135. */
  136. void idForce_Spring::RemovePhysics( const idPhysics *phys ) {
  137. if ( physics1 == phys ) {
  138. physics1 = NULL;
  139. }
  140. if ( physics2 == phys ) {
  141. physics2 = NULL;
  142. }
  143. }