DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Physics.h (9743B)


      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 #ifndef __PHYSICS_H__
     30 #define __PHYSICS_H__
     31 
     32 /*
     33 ===============================================================================
     34 
     35 	Physics abstract class
     36 
     37 	A physics object is a tool to manipulate the position and orientation of
     38 	an entity. The physics object is a container for idClipModels used for
     39 	collision detection. The physics deals with moving these collision models
     40 	through the world according to the laws of physics or other rules.
     41 
     42 	The mass of a clip model is the volume of the clip model times the density.
     43 	An arbitrary mass can however be set for specific clip models or the
     44 	whole physics object. The contents of a clip model is a set of bit flags
     45 	that define the contents. The clip mask defines the contents a clip model
     46 	collides with.
     47 
     48 	The linear velocity of a physics object is a vector that defines the
     49 	translation of the center of mass in units per second. The angular velocity
     50 	of a physics object is a vector that passes through the center of mass. The
     51 	direction of this vector defines the axis of rotation and the magnitude
     52 	defines the rate of rotation about the axis in radians per second.
     53 	The gravity is the change in velocity per second due to gravitational force.
     54 
     55 	Entities update their visual position and orientation from the physics
     56 	using GetOrigin() and GetAxis(). Direct origin and axis changes of
     57 	entities should go through the physics. In other words the physics origin
     58 	and axis are updated first and the entity updates it's visual position
     59 	from the physics.
     60 
     61 ===============================================================================
     62 */
     63 
     64 #define CONTACT_EPSILON			0.25f				// maximum contact seperation distance
     65 
     66 class idEntity;
     67 
     68 typedef struct impactInfo_s {
     69 	float						invMass;			// inverse mass
     70 	idMat3						invInertiaTensor;	// inverse inertia tensor
     71 	idVec3						position;			// impact position relative to center of mass
     72 	idVec3						velocity;			// velocity at the impact position
     73 } impactInfo_t;
     74 
     75 
     76 class idPhysics : public idClass {
     77 
     78 public:
     79 	ABSTRACT_PROTOTYPE( idPhysics );
     80 
     81 	virtual						~idPhysics();
     82 	static int					SnapTimeToPhysicsFrame( int t );
     83 
     84 	// Must not be virtual
     85 	void						Save( idSaveGame *savefile ) const;
     86 	void						Restore( idRestoreGame *savefile );
     87 
     88 public:	// common physics interface
     89 								// set pointer to entity using physics
     90 	virtual void				SetSelf( idEntity *e ) = 0;
     91 								// clip models
     92 	virtual void				SetClipModel( idClipModel *model, float density, int id = 0, bool freeOld = true ) = 0;
     93 	virtual void				SetClipBox( const idBounds &bounds, float density );
     94 	virtual idClipModel *		GetClipModel( int id = 0 ) const = 0;
     95 	virtual int					GetNumClipModels() const = 0;
     96 								// get/set the mass of a specific clip model or the whole physics object
     97 	virtual void				SetMass( float mass, int id = -1 ) = 0;
     98 	virtual float				GetMass( int id = -1 ) const = 0;
     99 								// get/set the contents of a specific clip model or the whole physics object
    100 	virtual void				SetContents( int contents, int id = -1 ) = 0;
    101 	virtual int					GetContents( int id = -1 ) const = 0;
    102 								// get/set the contents a specific clip model or the whole physics object collides with
    103 	virtual void				SetClipMask( int mask, int id = -1 ) = 0;
    104 	virtual int					GetClipMask( int id = -1 ) const = 0;
    105 								// get the bounds of a specific clip model or the whole physics object
    106 	virtual const idBounds &	GetBounds( int id = -1 ) const = 0;
    107 	virtual const idBounds &	GetAbsBounds( int id = -1 ) const = 0;
    108 								// evaluate the physics with the given time step, returns true if the object moved
    109 	virtual bool				Evaluate( int timeStepMSec, int endTimeMSec ) = 0;
    110 								// Interpolate between the two known snapshots with the given fraction, used for MP clients.
    111 								// returns true if the object moved.
    112 	virtual bool				Interpolate( const float fraction ) = 0;
    113 								// resets the prev and next states to the parameters.
    114 	virtual void				ResetInterpolationState( const idVec3 & origin, const idMat3 & axis ) = 0;
    115 								// update the time without moving
    116 	virtual void				UpdateTime( int endTimeMSec ) = 0;
    117 								// get the last physics update time
    118 	virtual int					GetTime() const = 0;
    119 								// collision interaction between different physics objects
    120 	virtual void				GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const = 0;
    121 	virtual void				ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse ) = 0;
    122 	virtual void				AddForce( const int id, const idVec3 &point, const idVec3 &force ) = 0;
    123 	virtual void				Activate() = 0;
    124 	virtual void				PutToRest() = 0;
    125 	virtual bool				IsAtRest() const = 0;
    126 	virtual int					GetRestStartTime() const = 0;
    127 	virtual bool				IsPushable() const = 0;
    128 								// save and restore the physics state
    129 	virtual void				SaveState() = 0;
    130 	virtual void				RestoreState() = 0;
    131 								// set the position and orientation in master space or world space if no master set
    132 	virtual void				SetOrigin( const idVec3 &newOrigin, int id = -1 ) = 0;
    133 	virtual void				SetAxis( const idMat3 &newAxis, int id = -1 ) = 0;
    134 								// translate or rotate the physics object in world space
    135 	virtual void				Translate( const idVec3 &translation, int id = -1 ) = 0;
    136 	virtual void				Rotate( const idRotation &rotation, int id = -1 ) = 0;
    137 								// get the position and orientation in world space
    138 	virtual const idVec3 &		GetOrigin( int id = 0 ) const = 0;
    139 	virtual const idMat3 &		GetAxis( int id = 0 ) const = 0;
    140 								// set linear and angular velocity
    141 	virtual void				SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 ) = 0;
    142 	virtual void				SetAngularVelocity( const idVec3 &newAngularVelocity, int id = 0 ) = 0;
    143 								// get linear and angular velocity
    144 	virtual const idVec3 &		GetLinearVelocity( int id = 0 ) const = 0;
    145 	virtual const idVec3 &		GetAngularVelocity( int id = 0 ) const = 0;
    146 								// gravity
    147 	virtual void				SetGravity( const idVec3 &newGravity ) = 0;
    148 	virtual const idVec3 &		GetGravity() const = 0;
    149 	virtual const idVec3 &		GetGravityNormal() const = 0;
    150 								// get first collision when translating or rotating this physics object
    151 	virtual void				ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const = 0;
    152 	virtual void				ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const = 0;
    153 	virtual int					ClipContents( const idClipModel *model ) const = 0;
    154 								// disable/enable the clip models contained by this physics object
    155 	virtual void				DisableClip() = 0;
    156 	virtual void				EnableClip() = 0;
    157 								// link/unlink the clip models contained by this physics object
    158 	virtual void				UnlinkClip() = 0;
    159 	virtual void				LinkClip() = 0;
    160 								// contacts
    161 	virtual bool				EvaluateContacts() = 0;
    162 	virtual int					GetNumContacts() const = 0;
    163 	virtual const contactInfo_t &GetContact( int num ) const = 0;
    164 	virtual void				ClearContacts() = 0;
    165 	virtual void				AddContactEntity( idEntity *e ) = 0;
    166 	virtual void 				RemoveContactEntity( idEntity *e ) = 0;
    167 								// ground contacts
    168 	virtual bool				HasGroundContacts() const = 0;
    169 	virtual bool				IsGroundEntity( int entityNum ) const = 0;
    170 	virtual bool				IsGroundClipModel( int entityNum, int id ) const = 0;
    171 								// set the master entity for objects bound to a master
    172 	virtual void				SetMaster( idEntity *master, const bool orientated = true ) = 0;
    173 								// set pushed state	
    174 	virtual void				SetPushed( int deltaTime ) = 0;
    175 	virtual const idVec3 &		GetPushedLinearVelocity( const int id = 0 ) const = 0;
    176 	virtual const idVec3 &		GetPushedAngularVelocity( const int id = 0 ) const = 0;
    177 								// get blocking info, returns NULL if the object is not blocked
    178 	virtual const trace_t *		GetBlockingInfo() const = 0;
    179 	virtual idEntity *			GetBlockingEntity() const = 0;
    180 								// movement end times in msec for reached events at the end of predefined motion
    181 	virtual int					GetLinearEndTime() const = 0;
    182 	virtual int					GetAngularEndTime() const = 0;
    183 								// networking
    184 	virtual void				WriteToSnapshot( idBitMsg &msg ) const = 0;
    185 	virtual void				ReadFromSnapshot( const idBitMsg &msg ) = 0;
    186 };
    187 
    188 #endif /* !__PHYSICS_H__ */