]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/d3xp/physics/Physics.h
hello world
[icculus/iodoom3.git] / neo / d3xp / physics / Physics.h
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 #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( void );
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( void ) 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                                                                 // update the time without moving
111         virtual void                            UpdateTime( int endTimeMSec ) = 0;
112                                                                 // get the last physics update time
113         virtual int                                     GetTime( void ) const = 0;
114                                                                 // collision interaction between different physics objects
115         virtual void                            GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const = 0;
116         virtual void                            ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse ) = 0;
117         virtual void                            AddForce( const int id, const idVec3 &point, const idVec3 &force ) = 0;
118         virtual void                            Activate( void ) = 0;
119         virtual void                            PutToRest( void ) = 0;
120         virtual bool                            IsAtRest( void ) const = 0;
121         virtual int                                     GetRestStartTime( void ) const = 0;
122         virtual bool                            IsPushable( void ) const = 0;
123                                                                 // save and restore the physics state
124         virtual void                            SaveState( void ) = 0;
125         virtual void                            RestoreState( void ) = 0;
126                                                                 // set the position and orientation in master space or world space if no master set
127         virtual void                            SetOrigin( const idVec3 &newOrigin, int id = -1 ) = 0;
128         virtual void                            SetAxis( const idMat3 &newAxis, int id = -1 ) = 0;
129                                                                 // translate or rotate the physics object in world space
130         virtual void                            Translate( const idVec3 &translation, int id = -1 ) = 0;
131         virtual void                            Rotate( const idRotation &rotation, int id = -1 ) = 0;
132                                                                 // get the position and orientation in world space
133         virtual const idVec3 &          GetOrigin( int id = 0 ) const = 0;
134         virtual const idMat3 &          GetAxis( int id = 0 ) const = 0;
135                                                                 // set linear and angular velocity
136         virtual void                            SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 ) = 0;
137         virtual void                            SetAngularVelocity( const idVec3 &newAngularVelocity, int id = 0 ) = 0;
138                                                                 // get linear and angular velocity
139         virtual const idVec3 &          GetLinearVelocity( int id = 0 ) const = 0;
140         virtual const idVec3 &          GetAngularVelocity( int id = 0 ) const = 0;
141                                                                 // gravity
142         virtual void                            SetGravity( const idVec3 &newGravity ) = 0;
143         virtual const idVec3 &          GetGravity( void ) const = 0;
144         virtual const idVec3 &          GetGravityNormal( void ) const = 0;
145                                                                 // get first collision when translating or rotating this physics object
146         virtual void                            ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const = 0;
147         virtual void                            ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const = 0;
148         virtual int                                     ClipContents( const idClipModel *model ) const = 0;
149                                                                 // disable/enable the clip models contained by this physics object
150         virtual void                            DisableClip( void ) = 0;
151         virtual void                            EnableClip( void ) = 0;
152                                                                 // link/unlink the clip models contained by this physics object
153         virtual void                            UnlinkClip( void ) = 0;
154         virtual void                            LinkClip( void ) = 0;
155                                                                 // contacts
156         virtual bool                            EvaluateContacts( void ) = 0;
157         virtual int                                     GetNumContacts( void ) const = 0;
158         virtual const contactInfo_t &GetContact( int num ) const = 0;
159         virtual void                            ClearContacts( void ) = 0;
160         virtual void                            AddContactEntity( idEntity *e ) = 0;
161         virtual void                            RemoveContactEntity( idEntity *e ) = 0;
162                                                                 // ground contacts
163         virtual bool                            HasGroundContacts( void ) const = 0;
164         virtual bool                            IsGroundEntity( int entityNum ) const = 0;
165         virtual bool                            IsGroundClipModel( int entityNum, int id ) const = 0;
166                                                                 // set the master entity for objects bound to a master
167         virtual void                            SetMaster( idEntity *master, const bool orientated = true ) = 0;
168                                                                 // set pushed state
169         virtual void                            SetPushed( int deltaTime ) = 0;
170         virtual const idVec3 &          GetPushedLinearVelocity( const int id = 0 ) const = 0;
171         virtual const idVec3 &          GetPushedAngularVelocity( const int id = 0 ) const = 0;
172                                                                 // get blocking info, returns NULL if the object is not blocked
173         virtual const trace_t *         GetBlockingInfo( void ) const = 0;
174         virtual idEntity *                      GetBlockingEntity( void ) const = 0;
175                                                                 // movement end times in msec for reached events at the end of predefined motion
176         virtual int                                     GetLinearEndTime( void ) const = 0;
177         virtual int                                     GetAngularEndTime( void ) const = 0;
178                                                                 // networking
179         virtual void                            WriteToSnapshot( idBitMsgDelta &msg ) const = 0;
180         virtual void                            ReadFromSnapshot( const idBitMsgDelta &msg ) = 0;
181 };
182
183 #endif /* !__PHYSICS_H__ */