PhysicsWorld.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "PhysicsTypes.h"
  30. #include "MathTypes.h"
  31. #include "WorldTypes.h"
  32. #include "PxScene.h"
  33. #include "PxCooking.h"
  34. #include "PxDefaultCpuDispatcher.h"
  35. #include "PxControllerManager.h"
  36. #include "Log.h"
  37. using physx::PxControllerManager;
  38. using physx::PxScene;
  39. using physx::PxDefaultCpuDispatcher;
  40. using physx::PxActor;
  41. using physx::PxOverlapHit;
  42. using physx::PxOverlapBuffer;
  43. using physx::PxMaterial;
  44. using physx::PxShape;
  45. using physx::PxPhysics;
  46. using physx::PxCooking;
  47. namespace crown
  48. {
  49. /// @defgroup Physics Physics
  50. /// Global physics-related functions
  51. ///
  52. /// @ingroup Physics
  53. namespace physics_system
  54. {
  55. /// Initializes the physics system.
  56. /// This is the place where to create and initialize per-application objects.
  57. void init();
  58. /// It should reverse the actions performed by physics_system::init().
  59. void shutdown();
  60. } // namespace physics_system
  61. //-----------------------------------------------------------------------------
  62. class SceneGraph;
  63. class World;
  64. struct PhysicsResource;
  65. struct PhysicsConfigResource;
  66. struct Unit;
  67. struct DebugLine;
  68. /// Manages physics objects in a World.
  69. ///
  70. /// @ingroup Physics
  71. class PhysicsWorld
  72. {
  73. public:
  74. PhysicsWorld(World& world);
  75. ~PhysicsWorld();
  76. ActorId create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node, UnitId unit_id);
  77. void destroy_actor(ActorId id);
  78. ControllerId create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node);
  79. void destroy_controller(ControllerId id);
  80. JointId create_joint(const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1);
  81. void destroy_joint(JointId id);
  82. RaycastId create_raycast(CollisionMode::Enum mode, CollisionType::Enum filter);
  83. void destroy_raycast(RaycastId id);
  84. Vector3 gravity() const;
  85. void set_gravity(const Vector3& g);
  86. /// Finds all actors in the physics world that are in a particular shape (supported: spheres, capsules and boxes)
  87. void overlap_test(CollisionType::Enum filter, ShapeType::Enum type,
  88. const Vector3& pos, const Quaternion& rot, const Vector3& size, Array<Actor*>& actors);
  89. /// Updates the physics simulation.
  90. void update(float dt);
  91. /// Draws debug lines.
  92. void draw_debug();
  93. Actor* get_actor(ActorId id);
  94. Controller* get_controller(ControllerId id);
  95. Raycast* get_raycast(RaycastId id);
  96. World& world() { return m_world; }
  97. EventStream& events() { return m_events; }
  98. public:
  99. PxPhysics* physx_physics();
  100. PxCooking* physx_cooking();
  101. PxScene* physx_scene();
  102. const PhysicsConfigResource* resource() { return m_resource; }
  103. private:
  104. World& m_world;
  105. PxControllerManager* m_controller_manager;
  106. PxScene* m_scene;
  107. PxDefaultCpuDispatcher* m_cpu_dispatcher;
  108. PxOverlapHit m_hits[64]; // hardcoded
  109. PxOverlapBuffer m_buffer;
  110. PoolAllocator m_actors_pool;
  111. PoolAllocator m_controllers_pool;
  112. PoolAllocator m_joints_pool;
  113. PoolAllocator m_raycasts_pool;
  114. IdArray<CE_MAX_ACTORS, Actor*> m_actors;
  115. IdArray<CE_MAX_CONTROLLERS, Controller*> m_controllers;
  116. IdArray<CE_MAX_JOINTS, Joint*> m_joints;
  117. IdArray<CE_MAX_RAYCASTS, Raycast*> m_raycasts;
  118. // Events management
  119. EventStream m_events;
  120. PhysicsSimulationCallback m_callback;
  121. const PhysicsConfigResource* m_resource;
  122. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  123. DebugLine* m_debug_line;
  124. #endif
  125. };
  126. } // namespace crown