Torque3D Documentation / _generateds / collisionComponent.h

collisionComponent.h

Engine/source/T3D/components/collision/collisionComponent.h

More...

Classes:

Public Typedefs

CollisionMeshMeshType 

Detailed Description

Public Typedefs

typedef CollisionComponent::MeshType CollisionMeshMeshType 

Public Functions

DefineEnumType(CollisionMeshMeshType )

  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_COMPONENT_H
 25#define COLLISION_COMPONENT_H
 26
 27#ifndef __RESOURCE_H__
 28#include "core/resource.h"
 29#endif
 30#ifndef _TSSHAPE_H_
 31#include "ts/tsShape.h"
 32#endif
 33#ifndef _SCENERENDERSTATE_H_
 34#include "scene/sceneRenderState.h"
 35#endif
 36#ifndef _MBOX_H_
 37#include "math/mBox.h"
 38#endif
 39#ifndef ENTITY_H
 40#include "T3D/entity.h"
 41#endif
 42#ifndef CORE_INTERFACES_H
 43#include "T3D/components/coreInterfaces.h"
 44#endif
 45#ifndef COLLISION_INTERFACES_H
 46#include "T3D/components/collision/collisionInterfaces.h"
 47#endif
 48#ifndef RENDER_COMPONENT_INTERFACE_H
 49#include "T3D/components/render/renderComponentInterface.h"
 50#endif
 51#ifndef PHYSICS_COMPONENT_INTERFACE_H
 52#include "T3D/components/physics/physicsComponentInterface.h"
 53#endif
 54#ifndef _T3D_PHYSICSCOMMON_H_
 55#include "T3D/physics/physicsCommon.h"
 56#endif
 57#ifndef _T3D_PHYSICS_PHYSICSWORLD_H_
 58#include "T3D/physics/physicsWorld.h"
 59#endif
 60
 61class TSShapeInstance;
 62class SceneRenderState;
 63class CollisionComponent;
 64class PhysicsBody;
 65class PhysicsWorld;
 66
 67class CollisionComponent : public Component,
 68   public CollisionInterface,
 69   public CastRayInterface
 70{
 71   typedef Component Parent;
 72public:
 73   enum MeshType
 74   {
 75      None = 0,            ///< No mesh
 76      Bounds = 1,          ///< Bounding box of the shape
 77      CollisionMesh = 2,   ///< Specifically designated collision meshes
 78      VisibleMesh = 3      ///< Rendered mesh polygons
 79   };
 80
 81   PhysicsWorld* mPhysicsWorld;
 82   PhysicsBody* mPhysicsRep;
 83
 84protected:
 85   MeshType mCollisionType;
 86   MeshType mDecalType;
 87   MeshType mLOSType;
 88
 89   Vector<S32> mCollisionDetails;
 90   Vector<S32> mLOSDetails;
 91
 92   StringTableEntry colisionMeshPrefix;
 93
 94   RenderComponentInterface* mOwnerRenderInterface;
 95
 96   PhysicsComponentInterface* mOwnerPhysicsInterface;
 97
 98   //only really relevent for the collision mesh type
 99   //if we note an animation component is added, we flag as being animated.
100   //This way, if we're using collision meshes, we can set it up to update their transforms
101   //as needed
102   bool mAnimated;
103
104   enum
105   {
106      ColliderMask = Parent::NextFreeMask,
107   };
108
109public:
110   CollisionComponent();
111   virtual ~CollisionComponent();
112   DECLARE_CONOBJECT(CollisionComponent);
113
114   virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
115   virtual void unpackUpdate(NetConnection *con, BitStream *stream);
116
117   virtual void componentAddedToOwner(Component *comp);
118   virtual void componentRemovedFromOwner(Component *comp);
119   virtual void ownerTransformSet(MatrixF *mat);
120   void targetShapeChanged(RenderComponentInterface* instanceInterface);
121
122   virtual void onComponentRemove();
123   virtual void onComponentAdd();
124
125   virtual void checkDependencies();
126
127   static void initPersistFields();
128
129   void inspectPostApply();
130
131   virtual void processTick();
132
133   void prepCollision();
134
135   PhysicsCollision* buildColShapes();
136
137   void updatePhysics();
138
139   virtual bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
140
141   virtual bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere){ return false; }
142
143   virtual PhysicsCollision* getCollisionData();
144
145   //Utility functions, mostly for script
146   Point3F getContactNormal() { return mContactInfo.contactNormal; }
147   bool hasContact()
148   {
149      if (mContactInfo.contactObject)
150         return true;
151      else
152         return false;
153   }
154   S32 getCollisionCount()
155   {
156      return mCollisionList.getCount();
157   }
158
159   Point3F getCollisionNormal(S32 collisionIndex)
160   {
161      if (collisionIndex < 0 || mCollisionList.getCount() < collisionIndex)
162         return Point3F::Zero;
163
164      return mCollisionList[collisionIndex].normal;
165   }
166
167   F32 getCollisionAngle(S32 collisionIndex, Point3F upVector)
168   {
169      if (collisionIndex < 0 || mCollisionList.getCount() < collisionIndex)
170         return 0.0f;
171
172      return mRadToDeg(mAcos(mDot(mCollisionList[collisionIndex].normal, upVector)));
173   }
174
175   S32 getBestCollision(Point3F upVector)
176   {
177      S32 bestCollision = -1;
178
179      F32 bestAngle = 360.f;
180      S32 count = mCollisionList.getCount();
181      for (U32 i = 0; i < count; ++i)
182      {
183         F32 angle = mRadToDeg(mAcos(mDot(mCollisionList[i].normal, upVector)));
184
185         if (angle < bestAngle)
186         {
187            bestCollision = i;
188            bestAngle = angle;
189         }
190      }
191
192      return bestCollision;
193   }
194
195   F32 getBestCollisionAngle(VectorF upVector)
196   {
197      S32 bestCol = getBestCollision(upVector);
198
199      if (bestCol == -1)
200         return 0;
201
202      return getCollisionAngle(bestCol, upVector);
203   }
204};
205
206typedef CollisionComponent::MeshType CollisionMeshMeshType;
207DefineEnumType(CollisionMeshMeshType);
208
209#endif // COLLISION_COMPONENT_H
210