DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

CollisionModel.h (7447B)


      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 __COLLISIONMODELMANAGER_H__
     30 #define __COLLISIONMODELMANAGER_H__
     31 
     32 /*
     33 ===============================================================================
     34 
     35 	Trace model vs. polygonal model collision detection.
     36 
     37 	Short translations are the least expensive. Retrieving contact points is
     38 	about as cheap as a short translation. Position tests are more expensive
     39 	and rotations are most expensive.
     40 
     41 	There is no position test at the start of a translation or rotation. In other
     42 	words if a translation with start != end or a rotation with angle != 0 starts
     43 	in solid, this goes unnoticed and the collision result is undefined.
     44 
     45 	A translation with start == end or a rotation with angle == 0 performs
     46 	a position test and fills in the trace_t structure accordingly.
     47 
     48 ===============================================================================
     49 */
     50 
     51 // contact type
     52 typedef enum {
     53 	CONTACT_NONE,							// no contact
     54 	CONTACT_EDGE,							// trace model edge hits model edge
     55 	CONTACT_MODELVERTEX,					// model vertex hits trace model polygon
     56 	CONTACT_TRMVERTEX						// trace model vertex hits model polygon
     57 } contactType_t;
     58 
     59 // contact info
     60 typedef struct {
     61 	contactType_t			type;			// contact type
     62 	idVec3					point;			// point of contact
     63 	idVec3					normal;			// contact plane normal
     64 	float					dist;			// contact plane distance
     65 	int						contents;		// contents at other side of surface
     66 	const idMaterial *		material;		// surface material
     67 	int						modelFeature;	// contact feature on model
     68 	int						trmFeature;		// contact feature on trace model
     69 	int						entityNum;		// entity the contact surface is a part of
     70 	int						id;				// id of clip model the contact surface is part of
     71 } contactInfo_t;
     72 
     73 // trace result
     74 typedef struct trace_s {
     75 	float					fraction;		// fraction of movement completed, 1.0 = didn't hit anything
     76 	idVec3					endpos;			// final position of trace model
     77 	idMat3					endAxis;		// final axis of trace model
     78 	contactInfo_t			c;				// contact information, only valid if fraction < 1.0
     79 } trace_t;
     80 
     81 typedef int cmHandle_t;
     82 
     83 #define CM_CLIP_EPSILON		0.25f			// always stay this distance away from any model
     84 #define CM_BOX_EPSILON		1.0f			// should always be larger than clip epsilon
     85 #define CM_MAX_TRACE_DIST	4096.0f			// maximum distance a trace model may be traced, point traces are unlimited
     86 
     87 class idCollisionModelManager {
     88 public:
     89 	virtual					~idCollisionModelManager() {}
     90 
     91 	// Loads collision models from a map file.
     92 	virtual void			LoadMap( const idMapFile *mapFile ) = 0;
     93 	// Frees all the collision models.
     94 	virtual void			FreeMap() = 0;
     95 
     96 	virtual void			Preload( const char *mapName ) = 0;
     97 
     98 	// Gets the clip handle for a model.
     99 	virtual cmHandle_t		LoadModel( const char *modelName ) = 0;
    100 	// Sets up a trace model for collision with other trace models.
    101 	virtual cmHandle_t		SetupTrmModel( const idTraceModel &trm, const idMaterial *material ) = 0;
    102 	// Creates a trace model from a collision model, returns true if succesfull.
    103 	virtual bool			TrmFromModel( const char *modelName, idTraceModel &trm ) = 0;
    104 
    105 	// Gets the name of a model.
    106 	virtual const char *	GetModelName( cmHandle_t model ) const = 0;
    107 	// Gets the bounds of a model.
    108 	virtual bool			GetModelBounds( cmHandle_t model, idBounds &bounds ) const = 0;
    109 	// Gets all contents flags of brushes and polygons of a model ored together.
    110 	virtual bool			GetModelContents( cmHandle_t model, int &contents ) const = 0;
    111 	// Gets a vertex of a model.
    112 	virtual bool			GetModelVertex( cmHandle_t model, int vertexNum, idVec3 &vertex ) const = 0;
    113 	// Gets an edge of a model.
    114 	virtual bool			GetModelEdge( cmHandle_t model, int edgeNum, idVec3 &start, idVec3 &end ) const = 0;
    115 	// Gets a polygon of a model.
    116 	virtual bool			GetModelPolygon( cmHandle_t model, int polygonNum, idFixedWinding &winding ) const = 0;
    117 
    118 	// Translates a trace model and reports the first collision if any.
    119 	virtual void			Translation( trace_t *results, const idVec3 &start, const idVec3 &end,
    120 								const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
    121 								cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
    122 	// Rotates a trace model and reports the first collision if any.
    123 	virtual void			Rotation( trace_t *results, const idVec3 &start, const idRotation &rotation,
    124 								const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
    125 								cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
    126 	// Returns the contents touched by the trace model or 0 if the trace model is in free space.
    127 	virtual int				Contents( const idVec3 &start,
    128 								const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
    129 								cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
    130 	// Stores all contact points of the trace model with the model, returns the number of contacts.
    131 	virtual int				Contacts( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
    132 								const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
    133 								cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
    134 
    135 	// Tests collision detection.
    136 	virtual void			DebugOutput( const idVec3 &origin ) = 0;
    137 	// Draws a model.
    138 	virtual void			DrawModel( cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis,
    139 												const idVec3 &viewOrigin, const float radius ) = 0;
    140 	// Prints model information, use -1 handle for accumulated model info.
    141 	virtual void			ModelInfo( cmHandle_t model ) = 0;
    142 	// Lists all loaded models.
    143 	virtual void			ListModels() = 0;
    144 	// Writes a collision model file for the given map entity.
    145 	virtual bool			WriteCollisionModelForMapEntity( const idMapEntity *mapEnt, const char *filename, const bool testTraceModel = true ) = 0;
    146 };
    147 
    148 extern idCollisionModelManager *		collisionModelManager;
    149 
    150 #endif /* !__COLLISIONMODELMANAGER_H__ */