doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Force_Grab.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
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.
13 
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.
18 
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/>.
21 
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.
23 
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.
25 
26 ===========================================================================
27 */
28 
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 #ifdef _D3XP
33 
34 #include "../Game_local.h"
35 
36 CLASS_DECLARATION( idForce, idForce_Grab )
38 
39 
40 /*
41 ================
42 idForce_Grab::Save
43 ================
44 */
45 void idForce_Grab::Save( idSaveGame *savefile ) const {
46 
47  savefile->WriteFloat( damping );
48  savefile->WriteVec3( goalPosition );
49  savefile->WriteFloat( distanceToGoal );
50  savefile->WriteInt( id );
51 }
52 
53 /*
54 ================
55 idForce_Grab::Restore
56 ================
57 */
58 void idForce_Grab::Restore( idRestoreGame *savefile ) {
59 
60  //Note: Owner needs to call set physics
61  savefile->ReadFloat( damping );
62  savefile->ReadVec3( goalPosition );
63  savefile->ReadFloat( distanceToGoal );
64  savefile->ReadInt( id );
65 }
66 
67 /*
68 ================
69 idForce_Grab::idForce_Grab
70 ================
71 */
72 idForce_Grab::idForce_Grab( void ) {
73  damping = 0.5f;
74  physics = NULL;
75  id = 0;
76 }
77 
78 /*
79 ================
80 idForce_Grab::~idForce_Grab
81 ================
82 */
83 idForce_Grab::~idForce_Grab( void ) {
84 }
85 
86 /*
87 ================
88 idForce_Grab::Init
89 ================
90 */
91 void idForce_Grab::Init( float damping ) {
92  if ( damping >= 0.0f && damping < 1.0f ) {
93  this->damping = damping;
94  }
95 }
96 
97 /*
98 ================
99 idForce_Grab::SetPhysics
100 ================
101 */
102 void idForce_Grab::SetPhysics( idPhysics *phys, int id, const idVec3 &goal ) {
103  this->physics = phys;
104  this->id = id;
105  this->goalPosition = goal;
106 }
107 
108 /*
109 ================
110 idForce_Grab::SetGoalPosition
111 ================
112 */
113 void idForce_Grab::SetGoalPosition( const idVec3 &goal ) {
114  this->goalPosition = goal;
115 }
116 
117 /*
118 =================
119 idForce_Grab::GetDistanceToGoal
120 =================
121 */
122 float idForce_Grab::GetDistanceToGoal( void ) {
123  return distanceToGoal;
124 }
125 
126 /*
127 ================
128 idForce_Grab::Evaluate
129 ================
130 */
131 void idForce_Grab::Evaluate( int time ) {
132  if ( !physics ) {
133  return;
134  }
135  idVec3 forceDir, v, objectCenter;
136  float forceAmt;
137  float mass = physics->GetMass(id);
138 
139  objectCenter = physics->GetAbsBounds(id).GetCenter();
140 
141  if ( g_grabberRandomMotion.GetBool() && !gameLocal.isMultiplayer ) {
142  // Jitter the objectCenter around so it doesn't remain stationary
143  float SinOffset = idMath::Sin( (float)(gameLocal.time)/66.f );
144  float randScale1 = gameLocal.random.RandomFloat();
145  float randScale2 = gameLocal.random.CRandomFloat();
146  objectCenter.x += ( SinOffset * 3.5f * randScale1 ) + ( randScale2 * 1.2f );
147  objectCenter.y += ( SinOffset * -3.5f * randScale1 ) + ( randScale2 * 1.4f );
148  objectCenter.z += ( SinOffset * 2.4f * randScale1 ) + ( randScale2 * 1.6f );
149  }
150 
151  forceDir = goalPosition - objectCenter;
152  distanceToGoal = forceDir.Normalize();
153 
154  float temp = distanceToGoal;
155  if ( temp > 12.f && temp < 32.f ) {
156  temp = 32.f;
157  }
158  forceAmt = (1000.f * mass) + (500.f * temp * mass);
159 
160  if ( forceAmt/mass > 120000.f ) {
161  forceAmt = 120000.f * mass;
162  }
163  physics->AddForce( id, objectCenter, forceDir * forceAmt );
164 
165  if ( distanceToGoal < 196.f ) {
166  v = physics->GetLinearVelocity( id );
167  physics->SetLinearVelocity( v * damping, id );
168  }
169  if ( distanceToGoal < 16.f ) {
170  v = physics->GetAngularVelocity(id);
171  if ( v.LengthSqr() > Square(8) ) {
172  physics->SetAngularVelocity( v * 0.99999f, id );
173  }
174  }
175 }
176 
177 /*
178 ================
179 idForce_Grab::RemovePhysics
180 ================
181 */
182 void idForce_Grab::RemovePhysics( const idPhysics *phys ) {
183  if ( physics == phys ) {
184  physics = NULL;
185  }
186 }
187 
188 #endif // _D3XP
float Normalize(void)
Definition: Vector.h:646
const GLdouble * v
Definition: glext.h:2936
Definition: Force.h:45
idRandom random
Definition: Game_local.h:291
bool isMultiplayer
Definition: Game_local.h:325
float z
Definition: Vector.h:320
Definition: Vector.h:316
ID_INLINE T Square(T x)
Definition: Math.h:104
float x
Definition: Vector.h:318
void ReadFloat(float &value)
Definition: SaveGame.cpp:967
float RandomFloat(void)
Definition: Random.h:82
static float Sin(float a)
Definition: Math.h:310
#define NULL
Definition: Lib.h:88
float y
Definition: Vector.h:319
idGameLocal gameLocal
Definition: Game_local.cpp:64
float LengthSqr(void) const
Definition: Vector.h:635
#define END_CLASS
Definition: Class.h:54
GLuint id
Definition: glext.h:3103
tuple f
Definition: idal.py:89
#define CLASS_DECLARATION(nameofsuperclass, nameofclass)
Definition: Class.h:110
void ReadVec3(idVec3 &vec)
Definition: SaveGame.cpp:1011
void ReadInt(int &value)
Definition: SaveGame.cpp:922
float CRandomFloat(void)
Definition: Random.h:86