collision.h
Engine/source/collision/collision.h
Classes:
class
class
class
class
Extension of the collision structure to allow use with raycasting.
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 face(0), 71 faceDot(0.0f), 72 distance(0) 73 74 { 75 } 76}; 77 78class CollisionList 79{ 80public: 81 enum 82 { 83 MaxCollisions = 64 84 }; 85 86protected: 87 dsize_t mCount; 88 Collision mCollision[MaxCollisions]; 89 F32 mT; 90 // MaxHeight is currently only set by the extrudedPolyList 91 // clipper. It represents the maximum vertex z value of 92 // the returned collision surfaces. 93 F32 mMaxHeight; 94 95public: 96 // Constructor 97 CollisionList( /* const dsize_t reserveSize = MaxCollisions */ ) : 98 mCount( 0 ), mT( 0.0f ), mMaxHeight( 0.0f ) 99 { 100 101 } 102 103 // Accessors 104 S32 getCount() const { return mCount; } 105 F32 getTime() const { return mT; } 106 F32 getMaxHeight() const { return mMaxHeight; } 107 108 const Collision &operator[]( const dsize_t idx ) const 109 { 110 AssertFatal( idx < mCount, "Out of bounds index." ); 111 return mCollision[idx]; 112 } 113 114 Collision &operator[]( const dsize_t idx ) 115 { 116 AssertFatal( idx < mCount, "Out of bounds index." ); 117 return mCollision[idx]; 118 } 119 120 // Increment does NOT reset the collision which it returns. It is the job of 121 // the caller to make sure that the entry has data properly assigned to it. 122 Collision &increment() 123 { 124 return mCollision[mCount++]; 125 } 126 127 void clear() 128 { 129 mCount = 0; 130 } 131 132 void setTime( const F32 t ) 133 { 134 mT = t; 135 } 136 137 void setMaxHeight( const F32 height ) 138 { 139 mMaxHeight = height; 140 } 141}; 142 143 144//---------------------------------------------------------------------------- 145// BSP Collision tree 146// Solid nodes are represented by structures with NULL frontNode and 147// backNode pointers. The material field is only valid on a solid node. 148// There is no structure for empty nodes, frontNode or backNode 149// should be set to NULL to represent empty half-spaces. 150 151struct BSPNode 152{ 153 U32 material; 154 PlaneF plane; 155 BSPNode *frontNode, *backNode; 156}; 157 158typedef Chunker<BSPNode> BSPTree; 159 160/// Extension of the collision structure to allow use with raycasting. 161/// @see Collision 162struct RayInfo : public Collision 163{ 164 RayInfo() : t(0), userData( NULL ) {} 165 166 // The collision struct has object, point, normal & material. 167 168 /// Distance along ray to contact point. 169 F32 t; 170 171 /// Set the point of intersection according to t and the given ray. 172 /// 173 /// Several pieces of code will not use ray information but rather rely 174 /// on contact points directly, so it is a good thing to always set 175 /// this in castRay functions. 176 void setContactPoint( const Point3F& start, const Point3F& end ) 177 { 178 Point3F startToEnd = end - start; 179 startToEnd *= t; 180 point = startToEnd + start; 181 } 182 183 /// A generic data void pointer. 184 /// Passing a void* around to random objects of unknown class types that may 185 /// interpret it differently would be very dangerous. Only use userData when 186 /// you call castRay/etc on an individual object of a known type. 187 void *userData; 188}; 189 190 191#endif // _COLLISION_H_ 192