PhysicsWorld.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/physics/PhysicsWorld.h>
  6. #include <anki/physics/PhysicsPlayerController.h>
  7. #include <anki/physics/PhysicsCollisionShape.h>
  8. #include <anki/physics/PhysicsBody.h>
  9. namespace anki
  10. {
  11. // Ugly but there is no other way
  12. static HeapAllocator<U8>* gAlloc = nullptr;
  13. static void* newtonAlloc(int size)
  14. {
  15. return gAlloc->allocate(size + 16);
  16. }
  17. static void newtonFree(void* const ptr, int size)
  18. {
  19. gAlloc->deallocate(ptr, size + 16);
  20. }
  21. PhysicsWorld::PhysicsWorld()
  22. {
  23. }
  24. PhysicsWorld::~PhysicsWorld()
  25. {
  26. cleanupMarkedForDeletion();
  27. if(m_sceneBody)
  28. {
  29. NewtonDestroyBody(m_sceneBody);
  30. m_sceneBody = nullptr;
  31. }
  32. if(m_sceneCollision)
  33. {
  34. NewtonDestroyCollision(m_sceneCollision);
  35. m_sceneCollision = nullptr;
  36. }
  37. if(m_world)
  38. {
  39. NewtonDestroy(m_world);
  40. m_world = nullptr;
  41. }
  42. gAlloc = nullptr;
  43. }
  44. Error PhysicsWorld::create(AllocAlignedCallback allocCb, void* allocCbData)
  45. {
  46. Error err = Error::NONE;
  47. m_alloc = HeapAllocator<U8>(allocCb, allocCbData);
  48. // Set allocators
  49. gAlloc = &m_alloc;
  50. NewtonSetMemorySystem(newtonAlloc, newtonFree);
  51. // Initialize world
  52. m_world = NewtonCreate();
  53. if(!m_world)
  54. {
  55. ANKI_PHYS_LOGE("NewtonCreate() failed");
  56. return Error::FUNCTION_FAILED;
  57. }
  58. // Set the simplified solver mode (faster but less accurate)
  59. NewtonSetSolverModel(m_world, 1);
  60. // Create the character controller manager. Newton needs it's own allocators
  61. m_playerManager = new CharacterControllerManager(this);
  62. // Create scene collision
  63. m_sceneCollision = NewtonCreateSceneCollision(m_world, 0);
  64. Mat4 trf = Mat4::getIdentity();
  65. m_sceneBody = NewtonCreateDynamicBody(m_world, m_sceneCollision, &trf[0]);
  66. NewtonBodySetMaterialGroupID(m_sceneBody, NewtonMaterialGetDefaultGroupID(m_world));
  67. NewtonDestroyCollision(m_sceneCollision); // destroy old scene
  68. m_sceneCollision = NewtonBodyGetCollision(m_sceneBody);
  69. // Set callbacks
  70. NewtonMaterialSetCollisionCallback(m_world,
  71. NewtonMaterialGetDefaultGroupID(m_world),
  72. NewtonMaterialGetDefaultGroupID(m_world),
  73. onAabbOverlapCallback,
  74. onContactCallback);
  75. return err;
  76. }
  77. Error PhysicsWorld::updateAsync(Second dt)
  78. {
  79. m_dt = dt;
  80. // Do cleanup of marked for deletion
  81. cleanupMarkedForDeletion();
  82. // Update
  83. NewtonUpdateAsync(m_world, dt);
  84. return Error::NONE;
  85. }
  86. void PhysicsWorld::waitUpdate()
  87. {
  88. NewtonWaitForUpdateToFinish(m_world);
  89. }
  90. void PhysicsWorld::cleanupMarkedForDeletion()
  91. {
  92. LockGuard<Mutex> lock(m_mtx);
  93. while(!m_forDeletion.isEmpty())
  94. {
  95. auto it = m_forDeletion.getBegin();
  96. PhysicsObject* obj = *it;
  97. // Remove from objects marked for deletion
  98. m_forDeletion.erase(m_alloc, it);
  99. // Finaly, delete it
  100. m_alloc.deleteInstance(obj);
  101. }
  102. }
  103. void PhysicsWorld::registerObject(PhysicsObject* ptr)
  104. {
  105. // TODO Remove
  106. }
  107. void PhysicsWorld::onContactCallback(const NewtonJoint* contactJoint, F32 timestep, int threadIndex)
  108. {
  109. const NewtonBody* body0 = NewtonJointGetBody0(contactJoint);
  110. const NewtonBody* body1 = NewtonJointGetBody1(contactJoint);
  111. F32 friction0 = 0.01f;
  112. F32 elasticity0 = 0.001f;
  113. F32 friction1 = friction0;
  114. F32 elasticity1 = elasticity0;
  115. void* userData = NewtonBodyGetUserData(body0);
  116. if(userData)
  117. {
  118. friction0 = static_cast<PhysicsBody*>(userData)->getFriction();
  119. elasticity0 = static_cast<PhysicsBody*>(userData)->getElasticity();
  120. }
  121. userData = NewtonBodyGetUserData(body1);
  122. if(userData)
  123. {
  124. friction1 = static_cast<PhysicsBody*>(userData)->getFriction();
  125. elasticity1 = static_cast<PhysicsBody*>(userData)->getElasticity();
  126. }
  127. F32 friction = friction0 + friction1;
  128. F32 elasticity = elasticity0 + elasticity1;
  129. void* contact = NewtonContactJointGetFirstContact(contactJoint);
  130. while(contact)
  131. {
  132. NewtonMaterial* material = NewtonContactGetMaterial(contact);
  133. NewtonMaterialSetContactFrictionCoef(material, friction + 0.1, friction, 0);
  134. NewtonMaterialSetContactFrictionCoef(material, friction + 0.1, friction, 1);
  135. NewtonMaterialSetContactElasticity(material, elasticity);
  136. contact = NewtonContactJointGetNextContact(contactJoint, contact);
  137. }
  138. }
  139. } // end namespace anki