collisionComponent.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef COLLISION_COMPONENT_H
  23. #define COLLISION_COMPONENT_H
  24. #ifndef __RESOURCE_H__
  25. #include "core/resource.h"
  26. #endif
  27. #ifndef _TSSHAPE_H_
  28. #include "ts/tsShape.h"
  29. #endif
  30. #ifndef _SCENERENDERSTATE_H_
  31. #include "scene/sceneRenderState.h"
  32. #endif
  33. #ifndef _MBOX_H_
  34. #include "math/mBox.h"
  35. #endif
  36. #ifndef ENTITY_H
  37. #include "T3D/entity.h"
  38. #endif
  39. #ifndef CORE_INTERFACES_H
  40. #include "T3D/components/coreInterfaces.h"
  41. #endif
  42. #ifndef COLLISION_INTERFACES_H
  43. #include "T3D/components/collision/collisionInterfaces.h"
  44. #endif
  45. #ifndef RENDER_COMPONENT_INTERFACE_H
  46. #include "T3D/components/render/renderComponentInterface.h"
  47. #endif
  48. #ifndef PHYSICS_COMPONENT_INTERFACE_H
  49. #include "T3D/components/physics/physicsComponentInterface.h"
  50. #endif
  51. #ifndef _T3D_PHYSICSCOMMON_H_
  52. #include "T3D/physics/physicsCommon.h"
  53. #endif
  54. #ifndef _T3D_PHYSICS_PHYSICSWORLD_H_
  55. #include "T3D/physics/physicsWorld.h"
  56. #endif
  57. class TSShapeInstance;
  58. class SceneRenderState;
  59. class CollisionComponent;
  60. class PhysicsBody;
  61. class PhysicsWorld;
  62. class CollisionComponent : public Component,
  63. public CollisionInterface,
  64. public CastRayInterface
  65. {
  66. typedef Component Parent;
  67. public:
  68. enum MeshType
  69. {
  70. None = 0, ///< No mesh
  71. Bounds = 1, ///< Bounding box of the shape
  72. CollisionMesh = 2, ///< Specifically designated collision meshes
  73. VisibleMesh = 3 ///< Rendered mesh polygons
  74. };
  75. PhysicsWorld* mPhysicsWorld;
  76. PhysicsBody* mPhysicsRep;
  77. protected:
  78. MeshType mCollisionType;
  79. MeshType mDecalType;
  80. MeshType mLOSType;
  81. Vector<S32> mCollisionDetails;
  82. Vector<S32> mLOSDetails;
  83. StringTableEntry colisionMeshPrefix;
  84. RenderComponentInterface* mOwnerRenderInterface;
  85. PhysicsComponentInterface* mOwnerPhysicsInterface;
  86. //only really relevent for the collision mesh type
  87. //if we note an animation component is added, we flag as being animated.
  88. //This way, if we're using collision meshes, we can set it up to update their transforms
  89. //as needed
  90. bool mAnimated;
  91. enum
  92. {
  93. ColliderMask = Parent::NextFreeMask,
  94. };
  95. public:
  96. CollisionComponent();
  97. virtual ~CollisionComponent();
  98. DECLARE_CONOBJECT(CollisionComponent);
  99. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  100. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  101. virtual void componentAddedToOwner(Component *comp);
  102. virtual void componentRemovedFromOwner(Component *comp);
  103. virtual void ownerTransformSet(MatrixF *mat);
  104. void targetShapeChanged(RenderComponentInterface* instanceInterface);
  105. virtual void onComponentRemove();
  106. virtual void onComponentAdd();
  107. virtual void checkDependencies();
  108. static void initPersistFields();
  109. void inspectPostApply();
  110. virtual void processTick();
  111. void prepCollision();
  112. PhysicsCollision* buildColShapes();
  113. void updatePhysics();
  114. virtual bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  115. virtual bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere){ return false; }
  116. virtual PhysicsCollision* getCollisionData();
  117. //Utility functions, mostly for script
  118. Point3F getContactNormal() { return mContactInfo.contactNormal; }
  119. bool hasContact()
  120. {
  121. if (mContactInfo.contactObject)
  122. return true;
  123. else
  124. return false;
  125. }
  126. S32 getCollisionCount()
  127. {
  128. return mCollisionList.getCount();
  129. }
  130. Point3F getCollisionNormal(S32 collisionIndex)
  131. {
  132. if (collisionIndex < 0 || mCollisionList.getCount() < collisionIndex)
  133. return Point3F::Zero;
  134. return mCollisionList[collisionIndex].normal;
  135. }
  136. F32 getCollisionAngle(S32 collisionIndex, Point3F upVector)
  137. {
  138. if (collisionIndex < 0 || mCollisionList.getCount() < collisionIndex)
  139. return 0.0f;
  140. return mRadToDeg(mAcos(mDot(mCollisionList[collisionIndex].normal, upVector)));
  141. }
  142. S32 getBestCollision(Point3F upVector)
  143. {
  144. S32 bestCollision = -1;
  145. F32 bestAngle = 360.f;
  146. S32 count = mCollisionList.getCount();
  147. for (U32 i = 0; i < count; ++i)
  148. {
  149. F32 angle = mRadToDeg(mAcos(mDot(mCollisionList[i].normal, upVector)));
  150. if (angle < bestAngle)
  151. {
  152. bestCollision = i;
  153. bestAngle = angle;
  154. }
  155. }
  156. return bestCollision;
  157. }
  158. F32 getBestCollisionAngle(VectorF upVector)
  159. {
  160. S32 bestCol = getBestCollision(upVector);
  161. if (bestCol == -1)
  162. return 0;
  163. return getCollisionAngle(bestCol, upVector);
  164. }
  165. };
  166. typedef CollisionComponent::MeshType CollisionMeshMeshType;
  167. DefineEnumType(CollisionMeshMeshType);
  168. #endif // COLLISION_COMPONENT_H