doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Force_Drag.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 #include "../Game_local.h"
33 
36 
37 /*
38 ================
39 idForce_Drag::idForce_Drag
40 ================
41 */
43  damping = 0.5f;
44  dragPosition = vec3_zero;
45  physics = NULL;
46  id = 0;
47  p = vec3_zero;
48  dragPosition = vec3_zero;
49 }
50 
51 /*
52 ================
53 idForce_Drag::~idForce_Drag
54 ================
55 */
57 }
58 
59 /*
60 ================
61 idForce_Drag::Init
62 ================
63 */
64 void idForce_Drag::Init( float damping ) {
65  if ( damping >= 0.0f && damping < 1.0f ) {
66  this->damping = damping;
67  }
68 }
69 
70 /*
71 ================
72 idForce_Drag::SetPhysics
73 ================
74 */
75 void idForce_Drag::SetPhysics( idPhysics *phys, int id, const idVec3 &p ) {
76  this->physics = phys;
77  this->id = id;
78  this->p = p;
79 }
80 
81 /*
82 ================
83 idForce_Drag::SetDragPosition
84 ================
85 */
87  this->dragPosition = pos;
88 }
89 
90 /*
91 ================
92 idForce_Drag::GetDragPosition
93 ================
94 */
95 const idVec3 &idForce_Drag::GetDragPosition( void ) const {
96  return this->dragPosition;
97 }
98 
99 /*
100 ================
101 idForce_Drag::GetDraggedPosition
102 ================
103 */
105  return ( physics->GetOrigin( id ) + p * physics->GetAxis( id ) );
106 }
107 
108 /*
109 ================
110 idForce_Drag::Evaluate
111 ================
112 */
113 void idForce_Drag::Evaluate( int time ) {
114  float l1, l2, mass;
115  idVec3 dragOrigin, dir1, dir2, velocity, centerOfMass;
116  idMat3 inertiaTensor;
117  idRotation rotation;
118  idClipModel *clipModel;
119 
120  if ( !physics ) {
121  return;
122  }
123 
124  clipModel = physics->GetClipModel( id );
125  if ( clipModel != NULL && clipModel->IsTraceModel() ) {
126  clipModel->GetMassProperties( 1.0f, mass, centerOfMass, inertiaTensor );
127  } else {
128  centerOfMass.Zero();
129  }
130 
131  centerOfMass = physics->GetOrigin( id ) + centerOfMass * physics->GetAxis( id );
132  dragOrigin = physics->GetOrigin( id ) + p * physics->GetAxis( id );
133 
134  dir1 = dragPosition - centerOfMass;
135  dir2 = dragOrigin - centerOfMass;
136  l1 = dir1.Normalize();
137  l2 = dir2.Normalize();
138 
139  rotation.Set( centerOfMass, dir2.Cross( dir1 ), RAD2DEG( idMath::ACos( dir1 * dir2 ) ) );
141 
142  velocity = physics->GetLinearVelocity( id ) * damping + dir1 * ( ( l1 - l2 ) * ( 1.0f - damping ) / MS2SEC( USERCMD_MSEC ) );
143  physics->SetLinearVelocity( velocity, id );
144 }
145 
146 /*
147 ================
148 idForce_Drag::RemovePhysics
149 ================
150 */
152  if ( physics == phys ) {
153  physics = NULL;
154  }
155 }
virtual const idVec3 & GetOrigin(int id=0) const =0
float damping
Definition: Force_Drag.h:65
float Normalize(void)
Definition: Vector.h:646
idPhysics * physics
Definition: Force_Drag.h:68
Definition: Force.h:45
idVec3 p
Definition: Force_Drag.h:70
idVec3 dragOrigin
Definition: XYWnd.cpp:2319
const idVec3 & GetDragPosition(void) const
Definition: Force_Drag.cpp:95
Definition: Vector.h:316
virtual const idVec3 & GetLinearVelocity(int id=0) const =0
virtual void SetLinearVelocity(const idVec3 &newLinearVelocity, int id=0)=0
idVec3 Cross(const idVec3 &a) const
Definition: Vector.h:619
virtual void RemovePhysics(const idPhysics *phys)
Definition: Force_Drag.cpp:151
void SetDragPosition(const idVec3 &pos)
Definition: Force_Drag.cpp:86
void SetPhysics(idPhysics *physics, int id, const idVec3 &p)
Definition: Force_Drag.cpp:75
const int USERCMD_MSEC
Definition: UsercmdGen.h:41
virtual void Evaluate(int time)
Definition: Force_Drag.cpp:113
void GetMassProperties(const float density, float &mass, idVec3 &centerOfMass, idMat3 &inertiaTensor) const
Definition: Clip.cpp:475
#define vec3_zero
Definition: Vector.h:390
#define NULL
Definition: Lib.h:88
bool IsTraceModel(void) const
Definition: Clip.h:218
idVec3 ToAngularVelocity(void) const
Definition: Rotation.cpp:125
virtual ~idForce_Drag(void)
Definition: Force_Drag.cpp:56
idVec3 dragPosition
Definition: Force_Drag.h:71
virtual const idMat3 & GetAxis(int id=0) const =0
#define END_CLASS
Definition: Class.h:54
void Set(const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle)
Definition: Rotation.h:108
virtual idClipModel * GetClipModel(int id=0) const =0
const idVec3 GetDraggedPosition(void) const
Definition: Force_Drag.cpp:104
static void Init(void)
Definition: Class.cpp:377
Definition: Matrix.h:333
tuple f
Definition: idal.py:89
#define RAD2DEG(a)
Definition: Math.h:57
#define CLASS_DECLARATION(nameofsuperclass, nameofclass)
Definition: Class.h:110
virtual void SetAngularVelocity(const idVec3 &newAngularVelocity, int id=0)=0
static float ACos(float a)
Definition: Math.h:544
GLfloat GLfloat p
Definition: glext.h:4674
void Zero(void)
Definition: Vector.h:415
#define MS2SEC(t)
Definition: Math.h:60