PolyPhysicsScene.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. namespace Polycode {
  25. class SceneEntity;
  26. class PhysicsSceneEntity;
  27. class PhysicsCharacter;
  28. class PhysicsVehicle;
  29. class _PolyExport PhysicsSceneEvent : public Event {
  30. public:
  31. PhysicsSceneEvent() : Event () {
  32. eventType = "PhysicsSceneEvent";
  33. }
  34. ~PhysicsSceneEvent() {
  35. }
  36. static const int COLLISION_EVENT = 0;
  37. PhysicsSceneEntity *entityA;
  38. PhysicsSceneEntity *entityB;
  39. Number appliedImpulse;
  40. Vector3 positionOnA;
  41. Vector3 positionOnB;
  42. Vector3 worldNormalOnB;
  43. };
  44. /**
  45. * A scene subclass that simulates physics for its children.
  46. */
  47. class _PolyExport PhysicsScene : public CollisionScene {
  48. public:
  49. /**
  50. * Main constructor.
  51. */
  52. PhysicsScene(int maxSubSteps = 0);
  53. virtual ~PhysicsScene();
  54. void Update();
  55. void removeEntity(SceneEntity *entity);
  56. void processWorldCollisions();
  57. PhysicsSceneEntity *getPhysicsEntityByCollisionObject(btCollisionObject *object);
  58. /** @name Physics scene
  59. * Public methods
  60. */
  61. //@{
  62. void removePhysicsChild(SceneEntity *entity);
  63. PhysicsSceneEntity *getPhysicsEntityBySceneEntity(SceneEntity *entity);
  64. PhysicsSceneEntity *addPhysicsChild(SceneEntity *newEntity, int type=0, Number mass = 0.0f, Number friction=1, Number restitution=0, int group=1);
  65. PhysicsSceneEntity *trackPhysicsChild(SceneEntity *newEntity, int type=0, Number mass = 0.0f, Number friction=1, Number restitution=0, int group=1);
  66. PhysicsCharacter *addCharacterChild(SceneEntity *newEntity, Number mass, Number friction, Number stepSize, int group = 1);
  67. void removeCharacterChild(PhysicsCharacter *character);
  68. void setVelocity(SceneEntity *entity, Vector3 velocity);
  69. void warpEntity(SceneEntity *entity, Vector3 position, bool resetRotation = false);
  70. PhysicsVehicle *addVehicleChild(SceneEntity *newEntity, Number mass, Number friction, int group = 1);
  71. void setGravity(Vector3 gravity);
  72. //@}
  73. // ----------------------------------------------------------------------------------------------------------------
  74. protected:
  75. int maxSubSteps;
  76. void initPhysicsScene();
  77. btDiscreteDynamicsWorld* physicsWorld;
  78. std::vector<PhysicsSceneEntity*> physicsChildren;
  79. };
  80. }