2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 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.
24 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.
26 ===========================================================================
29 #include "../../idlib/precompiled.h"
32 #include "../Game_local.h"
34 CLASS_DECLARATION( idForce, idForce_Spring )
39 idForce_Spring::idForce_Spring
42 idForce_Spring::idForce_Spring( void ) {
57 idForce_Spring::~idForce_Spring
60 idForce_Spring::~idForce_Spring( void ) {
65 idForce_Spring::InitSpring
68 void idForce_Spring::InitSpring( float Kstretch, float Kcompress, float damping, float restLength ) {
69 this->Kstretch = Kstretch;
70 this->Kcompress = Kcompress;
71 this->damping = damping;
72 this->restLength = restLength;
77 idForce_Spring::SetPosition
80 void idForce_Spring::SetPosition( idPhysics *physics1, int id1, const idVec3 &p1, idPhysics *physics2, int id2, const idVec3 &p2 ) {
81 this->physics1 = physics1;
84 this->physics2 = physics2;
91 idForce_Spring::Evaluate
94 void idForce_Spring::Evaluate( int time ) {
97 idVec3 pos1, pos2, velocity1, velocity2, force, dampingForce;
102 velocity1 = velocity2 = vec3_origin;
105 axis = physics1->GetAxis( id1 );
106 pos1 = physics1->GetOrigin( id1 );
108 if ( damping > 0.0f ) {
109 physics1->GetImpactInfo( id1, pos1, &info );
110 velocity1 = info.velocity;
115 axis = physics2->GetAxis( id2 );
116 pos2 = physics2->GetOrigin( id2 );
118 if ( damping > 0.0f ) {
119 physics2->GetImpactInfo( id2, pos2, &info );
120 velocity2 = info.velocity;
125 dampingForce = ( damping * ( ((velocity2 - velocity1) * force) / (force * force) ) ) * force;
126 length = force.Normalize();
128 // if the spring is stretched
129 if ( length > restLength ) {
130 if ( Kstretch > 0.0f ) {
131 force = ( Square( length - restLength ) * Kstretch ) * force - dampingForce;
133 physics1->AddForce( id1, pos1, force );
136 physics2->AddForce( id2, pos2, -force );
141 if ( Kcompress > 0.0f ) {
142 force = ( Square( length - restLength ) * Kcompress ) * force - dampingForce;
144 physics1->AddForce( id1, pos1, -force );
147 physics2->AddForce( id2, pos2, force );
155 idForce_Spring::RemovePhysics
158 void idForce_Spring::RemovePhysics( const idPhysics *phys ) {
159 if ( physics1 == phys ) {
162 if ( physics2 == phys ) {