DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Force_Drag.cpp (4274B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition 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 BFG Edition 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 #pragma hdrstop
     30 #include "../../idlib/precompiled.h"
     31 
     32 
     33 #include "../Game_local.h"
     34 
     35 CLASS_DECLARATION( idForce, idForce_Drag )
     36 END_CLASS
     37 
     38 /*
     39 ================
     40 idForce_Drag::idForce_Drag
     41 ================
     42 */
     43 idForce_Drag::idForce_Drag() {
     44 	damping			= 0.5f;
     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 */
     56 idForce_Drag::~idForce_Drag() {
     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 */
     86 void idForce_Drag::SetDragPosition( const idVec3 &pos ) {
     87 	this->dragPosition = pos;
     88 }
     89 
     90 /*
     91 ================
     92 idForce_Drag::GetDragPosition
     93 ================
     94 */
     95 const idVec3 &idForce_Drag::GetDragPosition() const {
     96 	return this->dragPosition;
     97 }
     98 
     99 /*
    100 ================
    101 idForce_Drag::GetDraggedPosition
    102 ================
    103 */
    104 const idVec3 idForce_Drag::GetDraggedPosition() const {
    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 ) ) );
    140 	physics->SetAngularVelocity( rotation.ToAngularVelocity() / MS2SEC( gameLocal.time - gameLocal.previousTime ), id );
    141 
    142 	velocity = physics->GetLinearVelocity( id ) * damping + dir1 * ( ( l1 - l2 ) * ( 1.0f - damping ) / MS2SEC( gameLocal.time - gameLocal.previousTime ) );
    143 	physics->SetLinearVelocity( velocity, id );
    144 }
    145 
    146 /*
    147 ================
    148 idForce_Drag::RemovePhysics
    149 ================
    150 */
    151 void idForce_Drag::RemovePhysics( const idPhysics *phys ) {
    152 	if ( physics == phys ) {
    153 		physics = NULL;
    154 	}
    155 }