collisionTrigger.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 _H_CollisionTrigger
  23. #define _H_CollisionTrigger
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef _MBOX_H_
  28. #include "math/mBox.h"
  29. #endif
  30. #ifndef _EARLYOUTPOLYLIST_H_
  31. #include "collision/earlyOutPolyList.h"
  32. #endif
  33. #ifndef _MPOLYHEDRON_H_
  34. #include "math/mPolyhedron.h"
  35. #endif
  36. #ifndef _TRIGGER_H_
  37. #include "T3D/trigger.h"
  38. #endif
  39. class Convex;
  40. class PhysicsBody;
  41. class TriggerPolyhedronType;
  42. class CollisionTrigger : public GameBase
  43. {
  44. typedef GameBase Parent;
  45. /// CollisionTrigger polyhedron with *outward* facing normals and CCW ordered
  46. /// vertices.
  47. Polyhedron mCollisionTriggerPolyhedron;
  48. EarlyOutPolyList mClippedList;
  49. Vector<GameBase*> mObjects;
  50. PhysicsBody *mPhysicsRep;
  51. U32 mLastThink;
  52. U32 mCurrTick;
  53. Convex *mConvexList;
  54. String mEnterCommand;
  55. String mLeaveCommand;
  56. String mTickCommand;
  57. enum CollisionTriggerUpdateBits
  58. {
  59. TransformMask = Parent::NextFreeMask << 0,
  60. PolyMask = Parent::NextFreeMask << 1,
  61. EnterCmdMask = Parent::NextFreeMask << 2,
  62. LeaveCmdMask = Parent::NextFreeMask << 3,
  63. TickCmdMask = Parent::NextFreeMask << 4,
  64. NextFreeMask = Parent::NextFreeMask << 5,
  65. };
  66. static const U32 CMD_SIZE = 1024;
  67. protected:
  68. static bool smRenderCollisionTriggers;
  69. bool testObject(GameBase* enter);
  70. void processTick(const Move *move);
  71. void buildConvex(const Box3F& box, Convex* convex);
  72. static bool setEnterCmd(void *object, const char *index, const char *data);
  73. static bool setLeaveCmd(void *object, const char *index, const char *data);
  74. static bool setTickCmd(void *object, const char *index, const char *data);
  75. public:
  76. CollisionTrigger();
  77. ~CollisionTrigger();
  78. // SimObject
  79. DECLARE_CONOBJECT(CollisionTrigger);
  80. DECLARE_CALLBACK(void, onAdd, (U32 objectId));
  81. DECLARE_CALLBACK(void, onRemove, (U32 objectId));
  82. static void consoleInit();
  83. static void initPersistFields();
  84. bool onAdd();
  85. void onRemove();
  86. void onDeleteNotify(SimObject*);
  87. void inspectPostApply();
  88. // NetObject
  89. U32 packUpdate(NetConnection *conn, U32 mask, BitStream* stream);
  90. void unpackUpdate(NetConnection *conn, BitStream* stream);
  91. // SceneObject
  92. void setTransform(const MatrixF &mat);
  93. void prepRenderImage(SceneRenderState* state);
  94. // GameBase
  95. bool onNewDataBlock(GameBaseData *dptr, bool reload);
  96. // CollisionTrigger
  97. void setTriggerPolyhedron(const Polyhedron&);
  98. void potentialEnterObject(GameBase*);
  99. U32 getNumCollisionTriggeringObjects() const;
  100. GameBase* getObject(const U32);
  101. const Vector<GameBase*>& getObjects() const { return mObjects; }
  102. void renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  103. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  104. };
  105. inline U32 CollisionTrigger::getNumCollisionTriggeringObjects() const
  106. {
  107. return mObjects.size();
  108. }
  109. inline GameBase* CollisionTrigger::getObject(const U32 index)
  110. {
  111. AssertFatal(index < getNumCollisionTriggeringObjects(), "Error, out of range object index");
  112. return mObjects[index];
  113. }
  114. #endif // _H_CollisionTrigger