PolyCollisionScene.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyScene.h"
  22. #include "PolyVector3.h"
  23. #include "btBulletCollisionCommon.h"
  24. #include <vector>
  25. class btCollisionObject;
  26. class btCollisionWorld;
  27. namespace Polycode {
  28. class Entity;
  29. class CollisionEntity;
  30. /**
  31. * Result of a collision test.
  32. */
  33. struct CollisionResult {
  34. /**
  35. * True if collided.
  36. */
  37. bool collided;
  38. /**
  39. * Collision normal.
  40. */
  41. Vector3 colNormal;
  42. /**
  43. * Collision distance.
  44. */
  45. Number colDist;
  46. bool setOldPosition;
  47. Vector3 newPos;
  48. };
  49. /**
  50. * Result of a ray test.
  51. */
  52. struct RayTestResult {
  53. /**
  54. * Entity returned.
  55. */
  56. Entity *entity;
  57. /**
  58. * Ray normal
  59. */
  60. Vector3 normal;
  61. /**
  62. * Ray position.
  63. */
  64. Vector3 position;
  65. };
  66. /**
  67. * A scene that tracks collisions between entities. The collision scene acts like a regular scene, only it automatically tracks collisions between its child entities.
  68. */
  69. class _PolyExport CollisionScene : public Scene {
  70. public:
  71. /**
  72. * Creates a collision scene.
  73. */
  74. CollisionScene(Vector3 size = Vector3(2000), bool virtualScene = false, bool deferInitCollision = false);
  75. virtual ~CollisionScene();
  76. void initCollisionScene(Vector3 size);
  77. virtual void fixedUpdate();
  78. virtual void removeEntity(Entity *entity);
  79. CollisionEntity *getCollisionEntityByObject(btCollisionObject *collisionObject);
  80. /** @name Collision scene
  81. * Public methods
  82. */
  83. //@{
  84. RayTestResult getFirstEntityInRay(const Vector3 &origin, const Vector3 &dest);
  85. void enableCollision(Entity *entity, bool val);
  86. CollisionEntity *getCollisionByScreenEntity(Entity *ent);
  87. CollisionResult testCollision(Entity *ent1, Entity *ent2);
  88. CollisionResult testCollisionOnCollisionChild(CollisionEntity *cEnt1, CollisionEntity *cEnt2);
  89. CollisionResult testCollisionOnCollisionChild_Convex(CollisionEntity *cEnt1, CollisionEntity *cEnt2);
  90. bool isColliding(Entity *ent1);
  91. virtual CollisionEntity *addCollisionChild(Entity *newEntity, int type=0, int group=1);
  92. virtual CollisionEntity *trackCollision(Entity *newEntity, int type=0, int group=1);
  93. void removeCollision(Entity *entity);
  94. void adjustForCollision(CollisionEntity *collisionEntity);
  95. //@}
  96. // ----------------------------------------------------------------------------------------------------------------
  97. protected:
  98. std::vector<CollisionEntity*> collisionChildren;
  99. btCollisionWorld *world;
  100. btDefaultCollisionConfiguration *collisionConfiguration;
  101. btCollisionDispatcher *dispatcher;
  102. btAxisSweep3* axisSweep;
  103. };
  104. }