2
0

PolyPhysicsScene.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "PolyPhysicsConstraint.h"
  23. #include <vector>
  24. class btDiscreteDynamicsWorld;
  25. struct btDbvtBroadphase;
  26. class btSequentialImpulseConstraintSolver;
  27. class btGhostPairCallback;
  28. class btTypedConstraint;
  29. class btHingeConstraint;
  30. class btPoint2PointConstraint;
  31. class btGeneric6DofConstraint;
  32. namespace Polycode {
  33. class Entity;
  34. class PhysicsEntity;
  35. class PhysicsCharacter;
  36. class PhysicsVehicle;
  37. class _PolyExport PhysicsSceneEvent : public Event {
  38. public:
  39. PhysicsSceneEvent();
  40. ~PhysicsSceneEvent();
  41. static const int EVENTBASE_PHYSICSSCENEEVENT = 0x900;
  42. static const int COLLISION_EVENT = EVENTBASE_PHYSICSSCENEEVENT+0;
  43. PhysicsEntity *entityA;
  44. PhysicsEntity *entityB;
  45. CollisionEntity *collisionEntityA;
  46. CollisionEntity *collisionEntityB;
  47. Number appliedImpulse;
  48. Vector3 positionOnA;
  49. Vector3 positionOnB;
  50. Vector3 worldNormalOnB;
  51. };
  52. class _PolyExport PhysicsGenericConstraint : public PhysicsConstraint {
  53. public:
  54. void setLinearLowerLimit(Vector3 limit);
  55. void setLinearUpperLimit(Vector3 limit);
  56. void setAngularLowerLimit(Vector3 limit);
  57. void setAngularUpperLimit(Vector3 limit);
  58. btGeneric6DofConstraint *btGenericConstraint;
  59. };
  60. class _PolyExport PhysicsHingeConstraint : public PhysicsConstraint {
  61. public:
  62. void setLimits(Number minLimit, Number maxLimit);
  63. Number getAngle();
  64. btHingeConstraint *_btHingeConstraint;
  65. };
  66. class _PolyExport PhysicsPointToPointConstraint : public PhysicsConstraint {
  67. public:
  68. btPoint2PointConstraint *btPointToPointConstraint;
  69. };
  70. /**
  71. * A scene subclass that simulates physics for its children.
  72. */
  73. class _PolyExport PhysicsScene : public CollisionScene {
  74. public:
  75. /**
  76. * Main constructor.
  77. */
  78. PhysicsScene(int maxSubSteps = 0, Vector3 size = Vector3(200), bool virtualScene = false);
  79. virtual ~PhysicsScene();
  80. void fixedUpdate();
  81. void removeEntity(Entity *entity);
  82. void processWorldCollisions();
  83. PhysicsEntity *getPhysicsEntityByCollisionObject(btCollisionObject *object);
  84. /** @name Physics scene
  85. * Public methods
  86. */
  87. //@{
  88. void removePhysicsChild(Entity *entity);
  89. PhysicsEntity *getPhysicsEntityByEntity(Entity *entity);
  90. PhysicsEntity *addPhysicsChild(Entity *newEntity, int type=0, Number mass = 0.0f, Number friction=1, Number restitution=0, int group=1, bool compoundChildren = false);
  91. PhysicsEntity *trackPhysicsChild(Entity *newEntity, int type=0, Number mass = 0.0f, Number friction=1, Number restitution=0, int group=1, bool compoundChildren = false);
  92. PhysicsCharacter *addCharacterChild(Entity *newEntity, Number mass, Number friction, Number stepSize, int group = 1);
  93. PhysicsCharacter *trackCharacterChild(Entity *newEntity, Number mass, Number friction, Number stepSize, int group = 1);
  94. void removeCharacterChild(PhysicsCharacter *character);
  95. PhysicsPointToPointConstraint *createPointToPointConstraint(Entity *entity1, Entity *entity2, const Vector3 &pivot1, const Vector3 &pivot2);
  96. PhysicsHingeConstraint *createHingeConstraint(Entity *entity, Vector3 pivot, Vector3 axis, Number minLimit, Number maxLimit);
  97. PhysicsHingeConstraint *createHingeJoint(Entity *entity1, Entity *entity2, Vector3 pivot1, Vector3 axis1, Vector3 pivot2, Vector3 axis2, Number minLimit, Number maxLimit);
  98. PhysicsGenericConstraint *createGenericConstraint(Entity *entity);
  99. void setVelocity(Entity *entity, Vector3 velocity);
  100. void setSpin(Entity *entity, Vector3 spin);
  101. void warpEntity(Entity *entity, Vector3 position, bool resetRotation = false);
  102. void removeConstraint(PhysicsConstraint *constraint);
  103. void applyImpulse(Entity *entity, Vector3 force, Vector3 point);
  104. PhysicsVehicle *addVehicleChild(Entity *newEntity, Number mass, Number friction, int group = 1);
  105. PhysicsVehicle *trackVehicleChild(Entity *newEntity, Number mass, Number friction, int group = 1);
  106. void setGravity(Vector3 gravity);
  107. void wakeUp(Entity *entity);
  108. //@}
  109. // ----------------------------------------------------------------------------------------------------------------
  110. bool pausePhysics;
  111. protected:
  112. bool paused;
  113. int maxSubSteps;
  114. void initPhysicsScene(Vector3 size);
  115. btDiscreteDynamicsWorld* physicsWorld;
  116. btSequentialImpulseConstraintSolver *solver;
  117. btDbvtBroadphase *broadphase;
  118. btGhostPairCallback *ghostPairCallback;
  119. std::vector<PhysicsEntity*> physicsChildren;
  120. };
  121. }