physics_world.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "id_array.h"
  25. #include "pool_allocator.h"
  26. #include "physics_types.h"
  27. #include "physics_callback.h"
  28. #include "event_stream.h"
  29. #include "physics_types.h"
  30. #include "math_types.h"
  31. #include "world_types.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. //-----------------------------------------------------------------------------
  50. struct SceneGraph;
  51. class World;
  52. struct PhysicsResource;
  53. struct PhysicsConfigResource;
  54. struct Unit;
  55. struct DebugLine;
  56. /// Manages physics objects in a World.
  57. ///
  58. /// @ingroup Physics
  59. class PhysicsWorld
  60. {
  61. public:
  62. PhysicsWorld(World& world);
  63. ~PhysicsWorld();
  64. ActorId create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node, UnitId unit_id);
  65. void destroy_actor(ActorId id);
  66. ControllerId create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node);
  67. void destroy_controller(ControllerId id);
  68. JointId create_joint(const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1);
  69. void destroy_joint(JointId id);
  70. RaycastId create_raycast(CollisionMode::Enum mode, CollisionType::Enum filter);
  71. void destroy_raycast(RaycastId id);
  72. /// Returns the gravity.
  73. Vector3 gravity() const;
  74. /// Sets the gravity.
  75. void set_gravity(const Vector3& g);
  76. /// Finds all actors in the physics world that are in a particular shape (supported: spheres, capsules and boxes)
  77. void overlap_test(CollisionType::Enum filter, ShapeType::Enum type,
  78. const Vector3& pos, const Quaternion& rot, const Vector3& size, Array<Actor*>& actors);
  79. /// Updates the physics simulation.
  80. void update(float dt);
  81. /// Draws debug lines.
  82. void draw_debug();
  83. Actor* get_actor(ActorId id);
  84. Controller* get_controller(ControllerId id);
  85. Raycast* get_raycast(RaycastId id);
  86. World& world() { return m_world; }
  87. EventStream& events() { return m_events; }
  88. public:
  89. PxPhysics* physx_physics();
  90. PxCooking* physx_cooking();
  91. PxScene* physx_scene();
  92. const PhysicsConfigResource* resource() { return m_resource; }
  93. private:
  94. World& m_world;
  95. PxControllerManager* m_controller_manager;
  96. PxScene* m_scene;
  97. PxDefaultCpuDispatcher* m_cpu_dispatcher;
  98. PxOverlapHit m_hits[64]; // hardcoded
  99. PxOverlapBuffer m_buffer;
  100. PoolAllocator m_actors_pool;
  101. PoolAllocator m_controllers_pool;
  102. PoolAllocator m_joints_pool;
  103. PoolAllocator m_raycasts_pool;
  104. IdArray<CE_MAX_ACTORS, Actor*> m_actors;
  105. IdArray<CE_MAX_CONTROLLERS, Controller*> m_controllers;
  106. IdArray<CE_MAX_JOINTS, Joint*> m_joints;
  107. IdArray<CE_MAX_RAYCASTS, Raycast*> m_raycasts;
  108. // Events management
  109. EventStream m_events;
  110. PhysicsSimulationCallback m_callback;
  111. const PhysicsConfigResource* m_resource;
  112. #if defined(CROWN_DEBUG)
  113. DebugLine* m_debug_line;
  114. #endif
  115. };
  116. } // namespace crown