PhysicsWorld.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "PhysicsWorld.h"
  24. #include "Vector3.h"
  25. #include "Actor.h"
  26. #include "Device.h"
  27. #include "Physics.h"
  28. #include "Quaternion.h"
  29. #include "PxPhysicsAPI.h"
  30. using physx::PxSceneDesc;
  31. using physx::PxVec3;
  32. using physx::PxTransform;
  33. using physx::PxQuat;
  34. using physx::PxHalfPi;
  35. using physx::PxPlaneGeometry;
  36. using physx::PxMaterial;
  37. using physx::PxShape;
  38. using physx::PxRigidStatic;
  39. namespace crown
  40. {
  41. static physx::PxSimulationFilterShader g_default_filter_shader = physx::PxDefaultSimulationFilterShader;
  42. //-----------------------------------------------------------------------------
  43. PhysicsWorld::PhysicsWorld()
  44. : m_scene(NULL)
  45. , m_actor_pool(default_allocator(), MAX_ACTORS, sizeof(Actor), CE_ALIGNOF(Actor))
  46. {
  47. PxSceneDesc scene_desc(device()->physx()->getTolerancesScale());
  48. scene_desc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
  49. if(!scene_desc.cpuDispatcher)
  50. {
  51. m_cpu_dispatcher = physx::PxDefaultCpuDispatcherCreate(1);
  52. CE_ASSERT(m_cpu_dispatcher != NULL, "Failed to create PhysX cpu dispatcher");
  53. scene_desc.cpuDispatcher = m_cpu_dispatcher;
  54. }
  55. if(!scene_desc.filterShader)
  56. scene_desc.filterShader = g_default_filter_shader;
  57. m_scene = device()->physx()->createScene(scene_desc);
  58. /* m_scene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0);
  59. m_scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f);*/
  60. PxTransform pose = PxTransform(PxVec3(0.0f, -3.75, 0.0f), PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)));
  61. PxMaterial* mat = device()->physx()->createMaterial(0.5f, 0.5f, 1.0f);
  62. PxRigidStatic* plane = device()->physx()->createRigidStatic(pose);
  63. PxShape* shape = plane->createShape(PxPlaneGeometry(), *mat);
  64. m_scene->addActor(*plane);
  65. }
  66. //-----------------------------------------------------------------------------
  67. PhysicsWorld::~PhysicsWorld()
  68. {
  69. m_scene->release();
  70. }
  71. //-----------------------------------------------------------------------------
  72. ActorId PhysicsWorld::create_actor(ActorType::Enum type)
  73. {
  74. Actor* actor = CE_NEW(m_actor_pool, Actor)(type, Vector3::ZERO, Quaternion::IDENTITY);
  75. m_scene->addActor(*actor->m_actor);
  76. return m_actor.create(actor);
  77. }
  78. //-----------------------------------------------------------------------------
  79. void PhysicsWorld::destroy_actor(ActorId id)
  80. {
  81. CE_ASSERT(m_actor.has(id), "Actor does not exist");
  82. Actor* actor = m_actor.lookup(id);
  83. CE_DELETE(m_actor_pool, actor);
  84. m_scene->removeActor(*actor->m_actor);
  85. m_actor.destroy(id);
  86. }
  87. //-----------------------------------------------------------------------------
  88. Actor* PhysicsWorld::lookup_actor(ActorId id)
  89. {
  90. CE_ASSERT(m_actor.has(id), "Actor does not exist");
  91. return m_actor.lookup(id);
  92. }
  93. //-----------------------------------------------------------------------------
  94. Vector3 PhysicsWorld::gravity() const
  95. {
  96. PxVec3 g = m_scene->getGravity();
  97. return Vector3(g.x, g.y, g.z);
  98. }
  99. //-----------------------------------------------------------------------------
  100. void PhysicsWorld::set_gravity(const Vector3& g)
  101. {
  102. m_scene->setGravity(PxVec3(g.x, g.y, g.z));
  103. }
  104. //-----------------------------------------------------------------------------
  105. void PhysicsWorld::update(float dt)
  106. {
  107. m_scene->simulate(1.0 / 60.0);
  108. while (!m_scene->fetchResults());
  109. m_graph_manager.update();
  110. }
  111. } // namespace crown