PolyPhysicsScene.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "PolyCollisionScene.h"
  22. #include <vector>
  23. class btDiscreteDynamicsWorld;
  24. class btDbvtBroadphase;
  25. class btSequentialImpulseConstraintSolver;
  26. class btGhostPairCallback;
  27. class btTypedConstraint;
  28. class btHingeConstraint;
  29. namespace Polycode {
  30. class SceneEntity;
  31. class PhysicsSceneEntity;
  32. class PhysicsCharacter;
  33. class PhysicsVehicle;
  34. class _PolyExport PhysicsSceneEvent : public Event {
  35. public:
  36. PhysicsSceneEvent();
  37. ~PhysicsSceneEvent();
  38. static const int COLLISION_EVENT = 0;
  39. PhysicsSceneEntity *entityA;
  40. PhysicsSceneEntity *entityB;
  41. Number appliedImpulse;
  42. Vector3 positionOnA;
  43. Vector3 positionOnB;
  44. Vector3 worldNormalOnB;
  45. };
  46. class _PolyExport PhysicsHingeConstraint {
  47. public:
  48. void setLimits(Number minLimit, Number maxLimit);
  49. Number getAngle();
  50. btHingeConstraint *btConstraint;
  51. };
  52. /**
  53. * A scene subclass that simulates physics for its children.
  54. */
  55. class _PolyExport PhysicsScene : public CollisionScene {
  56. public:
  57. /**
  58. * Main constructor.
  59. */
  60. PhysicsScene(int maxSubSteps = 0, Vector3 size = Vector3(200), bool virtualScene = false);
  61. virtual ~PhysicsScene();
  62. void Update();
  63. void removeEntity(SceneEntity *entity);
  64. void processWorldCollisions();
  65. PhysicsSceneEntity *getPhysicsEntityByCollisionObject(btCollisionObject *object);
  66. /** @name Physics scene
  67. * Public methods
  68. */
  69. //@{
  70. void removePhysicsChild(SceneEntity *entity);
  71. PhysicsSceneEntity *getPhysicsEntityBySceneEntity(SceneEntity *entity);
  72. PhysicsSceneEntity *addPhysicsChild(SceneEntity *newEntity, int type=0, Number mass = 0.0f, Number friction=1, Number restitution=0, int group=1, bool compoundChildren = false);
  73. PhysicsSceneEntity *trackPhysicsChild(SceneEntity *newEntity, int type=0, Number mass = 0.0f, Number friction=1, Number restitution=0, int group=1, bool compoundChildren = false);
  74. PhysicsCharacter *addCharacterChild(SceneEntity *newEntity, Number mass, Number friction, Number stepSize, int group = 1);
  75. void removeCharacterChild(PhysicsCharacter *character);
  76. PhysicsHingeConstraint *createHingeConstraint(SceneEntity *entity, Vector3 pivot, Vector3 axis, Number minLimit, Number maxLimit);
  77. PhysicsHingeConstraint *createHingeJoint(SceneEntity *entity1, SceneEntity *entity2, Vector3 pivot1, Vector3 axis1, Vector3 pivot2, Vector3 axis2, Number minLimit, Number maxLimit);
  78. void setVelocity(SceneEntity *entity, Vector3 velocity);
  79. void warpEntity(SceneEntity *entity, Vector3 position, bool resetRotation = false);
  80. PhysicsVehicle *addVehicleChild(SceneEntity *newEntity, Number mass, Number friction, int group = 1);
  81. void setGravity(Vector3 gravity);
  82. void wakeUp(SceneEntity *entity);
  83. //@}
  84. // ----------------------------------------------------------------------------------------------------------------
  85. protected:
  86. int maxSubSteps;
  87. void initPhysicsScene(Vector3 size);
  88. btDiscreteDynamicsWorld* physicsWorld;
  89. btSequentialImpulseConstraintSolver *solver;
  90. btDbvtBroadphase *broadphase;
  91. btGhostPairCallback *ghostPairCallback;
  92. std::vector<PhysicsSceneEntity*> physicsChildren;
  93. };
  94. }