collision.h

Engine/source/collision/collision.h

More...

Classes:

class
class

Extension of the collision structure to allow use with raycasting.

Public Typedefs

BSPTree 

Detailed Description

Public Typedefs

typedef Chunker< BSPNode > BSPTree 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _COLLISION_H_
 25#define _COLLISION_H_
 26
 27#ifndef _DATACHUNKER_H_
 28#include "core/dataChunker.h"
 29#endif
 30#ifndef _MPLANE_H_
 31#include "math/mPlane.h"
 32#endif
 33#ifndef _MPOINT2_H_
 34#include "math/mPoint2.h"
 35#endif
 36
 37class SceneObject;
 38class BaseMatInstance;
 39
 40//----------------------------------------------------------------------------
 41
 42struct Collision
 43{
 44   SceneObject* object;
 45   Point3F point;
 46   VectorF normal;
 47   BaseMatInstance* material;
 48
 49   // generate UV coordinate across (TSStatic) mesh based on 
 50   // matching normals, this isn't done by default and is 
 51   // primarily of interest in matching a collision point to 
 52   // either a GUI control coordinate or finding a hit pixel in texture space
 53   bool generateTexCoord;
 54
 55   // If generateTexCoord is set this will either be invalid (-1, -1)
 56   // or a value in texture space (assuming 0..1 UV mapping)
 57   Point2F texCoord;
 58
 59   // Face and Face dot are currently only set by the extrudedPolyList
 60   // clipper.  Values are otherwise undefined.
 61   U32 face;                  // Which face was hit
 62   F32 faceDot;               // -Dot of face with poly normal
 63   F32 distance;
 64   
 65   Collision() :
 66      object( NULL ),
 67      material( NULL ),
 68      generateTexCoord(false),
 69      texCoord(-1.0f, -1.0f)
 70   {
 71   }
 72};
 73
 74class CollisionList
 75{
 76public:
 77   enum
 78   {
 79      MaxCollisions = 64
 80   };
 81
 82protected:
 83   dsize_t mCount;
 84   Collision mCollision[MaxCollisions];
 85   F32 mT;
 86   // MaxHeight is currently only set by the extrudedPolyList
 87   // clipper.  It represents the maximum vertex z value of
 88   // the returned collision surfaces.
 89   F32 mMaxHeight;
 90
 91public:
 92   // Constructor
 93   CollisionList( /* const dsize_t reserveSize = MaxCollisions */ ) :
 94      mCount( 0 ), mT( 0.0f ), mMaxHeight( 0.0f )
 95   {
 96
 97   }
 98
 99   // Accessors
100   S32 getCount() const { return mCount; }
101   F32 getTime() const { return mT; }
102   F32 getMaxHeight() const { return mMaxHeight; }
103
104   const Collision &operator[]( const dsize_t idx ) const
105   {
106      AssertFatal( idx < mCount, "Out of bounds index." );
107      return mCollision[idx];
108   }
109
110   Collision &operator[]( const dsize_t idx )
111   {
112      AssertFatal( idx < mCount, "Out of bounds index." );
113      return mCollision[idx];
114   }
115
116   // Increment does NOT reset the collision which it returns. It is the job of
117   // the caller to make sure that the entry has data properly assigned to it.
118   Collision &increment()
119   {
120      return mCollision[mCount++];
121   }
122
123   void clear()
124   {
125      mCount = 0;
126   }
127
128   void setTime( const F32 t )
129   {
130      mT = t;
131   }
132
133   void setMaxHeight( const F32 height )
134   {
135      mMaxHeight = height;
136   }
137};
138
139
140//----------------------------------------------------------------------------
141// BSP Collision tree
142// Solid nodes are represented by structures with NULL frontNode and
143// backNode pointers. The material field is only valid on a solid node.
144// There is no structure for empty nodes, frontNode or backNode
145// should be set to NULL to represent empty half-spaces.
146
147struct BSPNode
148{
149   U32 material;
150   PlaneF plane;
151   BSPNode *frontNode, *backNode;
152};
153
154typedef Chunker<BSPNode> BSPTree;
155
156/// Extension of the collision structure to allow use with raycasting.
157/// @see Collision
158struct RayInfo : public Collision 
159{
160   RayInfo() : userData( NULL ) {}
161
162   // The collision struct has object, point, normal & material.
163
164   /// Distance along ray to contact point.
165   F32 t; 
166
167   /// Set the point of intersection according to t and the given ray.
168   ///
169   /// Several pieces of code will not use ray information but rather rely
170   /// on contact points directly, so it is a good thing to always set
171   /// this in castRay functions.
172   void setContactPoint( const Point3F& start, const Point3F& end )
173   {
174      Point3F startToEnd = end - start;
175      startToEnd *= t;
176      point = startToEnd + start;
177   }
178
179   /// A generic data void pointer.
180   /// Passing a void* around to random objects of unknown class types that may 
181   /// interpret it differently would be very dangerous.  Only use userData when 
182   /// you call castRay/etc on an individual object of a known type.
183   void *userData;
184};
185
186
187#endif // _COLLISION_H_
188