PhysicsWorld.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "IdArray.h"
  25. #include "PoolAllocator.h"
  26. #include "PhysicsTypes.h"
  27. #include "PhysicsCallback.h"
  28. #include "EventStream.h"
  29. #include "PxScene.h"
  30. #include "PxDefaultCpuDispatcher.h"
  31. #include "PxControllerManager.h"
  32. #include "Log.h"
  33. using physx::PxControllerManager;
  34. using physx::PxScene;
  35. using physx::PxDefaultCpuDispatcher;
  36. using physx::PxActor;
  37. #define MAX_ACTORS 1024
  38. #define MAX_CONTROLLERS 1024
  39. #define MAX_TRIGGERS 1024
  40. #define MAX_JOINTS 512
  41. namespace crown
  42. {
  43. /// Global physics-related functions
  44. namespace physics_system
  45. {
  46. /// Initializes the physics system.
  47. /// This is the place where to create and initialize per-application objects.
  48. void init();
  49. /// It should reverse the actions performed by audio_system::init().
  50. void shutdown();
  51. } // namespace physics_system
  52. //-----------------------------------------------------------------------------
  53. struct PhysicsResource;
  54. struct PhysicsActor;
  55. struct Controller;
  56. struct Vector3;
  57. struct Actor;
  58. struct Trigger;
  59. struct Joint;
  60. struct Quaternion;
  61. class SceneGraph;
  62. struct Actor;
  63. //-----------------------------------------------------------------------------
  64. class PhysicsWorld
  65. {
  66. public:
  67. PhysicsWorld();
  68. ~PhysicsWorld();
  69. ActorId create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node);
  70. void destroy_actor(ActorId id);
  71. ControllerId create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node);
  72. void destroy_controller(ControllerId id);
  73. TriggerId create_trigger(const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
  74. void destroy_trigger(TriggerId id);
  75. JointId create_joint(const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1);
  76. void destroy_joint(JointId id);
  77. Actor* lookup_actor(StringId32 name);
  78. Actor* lookup_actor(ActorId id);
  79. Controller* lookup_controller(ControllerId id);
  80. Trigger* lookup_trigger(TriggerId id);
  81. Vector3 gravity() const;
  82. void set_gravity(const Vector3& g);
  83. void set_kinematic(ActorId id);
  84. void clear_kinematic(ActorId id);
  85. void update(float dt);
  86. public:
  87. PxControllerManager* m_controller_manager;
  88. PxScene* m_scene;
  89. PxDefaultCpuDispatcher* m_cpu_dispatcher;
  90. PoolAllocator m_actors_pool;
  91. PoolAllocator m_controllers_pool;
  92. PoolAllocator m_triggers_pool;
  93. PoolAllocator m_joints_pool;
  94. IdArray<MAX_ACTORS, Actor*> m_actors;
  95. IdArray<MAX_CONTROLLERS, Controller*> m_controllers;
  96. IdArray<MAX_TRIGGERS, Trigger*> m_triggers;
  97. IdArray<MAX_JOINTS, Joint*> m_joints;
  98. // Events management
  99. EventStream m_events;
  100. PhysicsSimulationCallback m_callback;
  101. public:
  102. friend class PhysicsSimulationCallback;
  103. };
  104. } // namespace crown