|
@@ -14,14 +14,16 @@ namespace anki
|
|
|
// Ugly but there is no other way
|
|
// Ugly but there is no other way
|
|
|
static HeapAllocator<U8>* gAlloc = nullptr;
|
|
static HeapAllocator<U8>* gAlloc = nullptr;
|
|
|
|
|
|
|
|
-static void* newtonAlloc(int size)
|
|
|
|
|
|
|
+static void* btAlloc(size_t size)
|
|
|
{
|
|
{
|
|
|
- return gAlloc->allocate(size + 16);
|
|
|
|
|
|
|
+ ANKI_ASSERT(gAlloc);
|
|
|
|
|
+ return gAlloc->getMemoryPool().allocate(size, 16);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static void newtonFree(void* const ptr, int size)
|
|
|
|
|
|
|
+static void btFree(void* ptr)
|
|
|
{
|
|
{
|
|
|
- gAlloc->deallocate(ptr, size + 16);
|
|
|
|
|
|
|
+ ANKI_ASSERT(gAlloc);
|
|
|
|
|
+ gAlloc->getMemoryPool().free(ptr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
PhysicsWorld::PhysicsWorld()
|
|
PhysicsWorld::PhysicsWorld()
|
|
@@ -32,88 +34,44 @@ PhysicsWorld::~PhysicsWorld()
|
|
|
{
|
|
{
|
|
|
cleanupMarkedForDeletion();
|
|
cleanupMarkedForDeletion();
|
|
|
|
|
|
|
|
- if(m_sceneBody)
|
|
|
|
|
- {
|
|
|
|
|
- NewtonDestroyBody(m_sceneBody);
|
|
|
|
|
- m_sceneBody = nullptr;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(m_sceneCollision)
|
|
|
|
|
- {
|
|
|
|
|
- NewtonDestroyCollision(m_sceneCollision);
|
|
|
|
|
- m_sceneCollision = nullptr;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(m_world)
|
|
|
|
|
- {
|
|
|
|
|
- NewtonDestroy(m_world);
|
|
|
|
|
- m_world = nullptr;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ m_alloc.deleteInstance(m_world);
|
|
|
|
|
+ m_alloc.deleteInstance(m_solver);
|
|
|
|
|
+ m_alloc.deleteInstance(m_dispatcher);
|
|
|
|
|
+ m_alloc.deleteInstance(m_collisionConfig);
|
|
|
|
|
+ m_alloc.deleteInstance(m_broadphase);
|
|
|
|
|
|
|
|
gAlloc = nullptr;
|
|
gAlloc = nullptr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Error PhysicsWorld::create(AllocAlignedCallback allocCb, void* allocCbData)
|
|
Error PhysicsWorld::create(AllocAlignedCallback allocCb, void* allocCbData)
|
|
|
{
|
|
{
|
|
|
- Error err = Error::NONE;
|
|
|
|
|
-
|
|
|
|
|
m_alloc = HeapAllocator<U8>(allocCb, allocCbData);
|
|
m_alloc = HeapAllocator<U8>(allocCb, allocCbData);
|
|
|
|
|
|
|
|
// Set allocators
|
|
// Set allocators
|
|
|
gAlloc = &m_alloc;
|
|
gAlloc = &m_alloc;
|
|
|
- NewtonSetMemorySystem(newtonAlloc, newtonFree);
|
|
|
|
|
|
|
+ btAlignedAllocSetCustom(btAlloc, btFree);
|
|
|
|
|
|
|
|
- // Initialize world
|
|
|
|
|
- m_world = NewtonCreate();
|
|
|
|
|
- if(!m_world)
|
|
|
|
|
- {
|
|
|
|
|
- ANKI_PHYS_LOGE("NewtonCreate() failed");
|
|
|
|
|
- return Error::FUNCTION_FAILED;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Create objects
|
|
|
|
|
+ m_broadphase = m_alloc.newInstance<btDbvtBroadphase>();
|
|
|
|
|
+ m_collisionConfig = m_alloc.newInstance<btDefaultCollisionConfiguration>();
|
|
|
|
|
+ m_dispatcher = m_alloc.newInstance<btCollisionDispatcher>(m_collisionConfig);
|
|
|
|
|
+ m_solver = m_alloc.newInstance<btSequentialImpulseConstraintSolver>();
|
|
|
|
|
+ m_world = m_alloc.newInstance<btDiscreteDynamicsWorld>(m_dispatcher, m_broadphase, m_solver, m_collisionConfig);
|
|
|
|
|
|
|
|
- // Set the simplified solver mode (faster but less accurate)
|
|
|
|
|
- NewtonSetSolverModel(m_world, 1);
|
|
|
|
|
-
|
|
|
|
|
- // Create the character controller manager. Newton needs it's own allocators
|
|
|
|
|
- m_playerManager = new CharacterControllerManager(this);
|
|
|
|
|
-
|
|
|
|
|
- // Create scene collision
|
|
|
|
|
- m_sceneCollision = NewtonCreateSceneCollision(m_world, 0);
|
|
|
|
|
- Mat4 trf = Mat4::getIdentity();
|
|
|
|
|
- m_sceneBody = NewtonCreateDynamicBody(m_world, m_sceneCollision, &trf[0]);
|
|
|
|
|
- NewtonBodySetMaterialGroupID(m_sceneBody, NewtonMaterialGetDefaultGroupID(m_world));
|
|
|
|
|
-
|
|
|
|
|
- NewtonDestroyCollision(m_sceneCollision); // destroy old scene
|
|
|
|
|
- m_sceneCollision = NewtonBodyGetCollision(m_sceneBody);
|
|
|
|
|
-
|
|
|
|
|
- // Set callbacks
|
|
|
|
|
- NewtonMaterialSetCollisionCallback(m_world,
|
|
|
|
|
- NewtonMaterialGetDefaultGroupID(m_world),
|
|
|
|
|
- NewtonMaterialGetDefaultGroupID(m_world),
|
|
|
|
|
- onAabbOverlapCallback,
|
|
|
|
|
- onContactCallback);
|
|
|
|
|
-
|
|
|
|
|
- return err;
|
|
|
|
|
|
|
+ return Error::NONE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-Error PhysicsWorld::updateAsync(Second dt)
|
|
|
|
|
|
|
+Error PhysicsWorld::update(Second dt)
|
|
|
{
|
|
{
|
|
|
- m_dt = dt;
|
|
|
|
|
-
|
|
|
|
|
// Do cleanup of marked for deletion
|
|
// Do cleanup of marked for deletion
|
|
|
cleanupMarkedForDeletion();
|
|
cleanupMarkedForDeletion();
|
|
|
|
|
|
|
|
// Update
|
|
// Update
|
|
|
- NewtonUpdateAsync(m_world, dt);
|
|
|
|
|
|
|
+ m_world->stepSimulation(dt, 1, 1.0 / 60.0);
|
|
|
|
|
|
|
|
return Error::NONE;
|
|
return Error::NONE;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void PhysicsWorld::waitUpdate()
|
|
|
|
|
-{
|
|
|
|
|
- NewtonWaitForUpdateToFinish(m_world);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void PhysicsWorld::cleanupMarkedForDeletion()
|
|
void PhysicsWorld::cleanupMarkedForDeletion()
|
|
|
{
|
|
{
|
|
|
LockGuard<Mutex> lock(m_mtx);
|
|
LockGuard<Mutex> lock(m_mtx);
|
|
@@ -131,50 +89,4 @@ void PhysicsWorld::cleanupMarkedForDeletion()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void PhysicsWorld::registerObject(PhysicsObject* ptr)
|
|
|
|
|
-{
|
|
|
|
|
- // TODO Remove
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void PhysicsWorld::onContactCallback(const NewtonJoint* contactJoint, F32 timestep, int threadIndex)
|
|
|
|
|
-{
|
|
|
|
|
- const NewtonBody* body0 = NewtonJointGetBody0(contactJoint);
|
|
|
|
|
- const NewtonBody* body1 = NewtonJointGetBody1(contactJoint);
|
|
|
|
|
-
|
|
|
|
|
- F32 friction0 = 0.01f;
|
|
|
|
|
- F32 elasticity0 = 0.001f;
|
|
|
|
|
- F32 friction1 = friction0;
|
|
|
|
|
- F32 elasticity1 = elasticity0;
|
|
|
|
|
-
|
|
|
|
|
- void* userData = NewtonBodyGetUserData(body0);
|
|
|
|
|
- if(userData)
|
|
|
|
|
- {
|
|
|
|
|
- friction0 = static_cast<PhysicsBody*>(userData)->getFriction();
|
|
|
|
|
- elasticity0 = static_cast<PhysicsBody*>(userData)->getElasticity();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- userData = NewtonBodyGetUserData(body1);
|
|
|
|
|
- if(userData)
|
|
|
|
|
- {
|
|
|
|
|
- friction1 = static_cast<PhysicsBody*>(userData)->getFriction();
|
|
|
|
|
- elasticity1 = static_cast<PhysicsBody*>(userData)->getElasticity();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- F32 friction = friction0 + friction1;
|
|
|
|
|
- F32 elasticity = elasticity0 + elasticity1;
|
|
|
|
|
-
|
|
|
|
|
- void* contact = NewtonContactJointGetFirstContact(contactJoint);
|
|
|
|
|
- while(contact)
|
|
|
|
|
- {
|
|
|
|
|
- NewtonMaterial* material = NewtonContactGetMaterial(contact);
|
|
|
|
|
-
|
|
|
|
|
- NewtonMaterialSetContactFrictionCoef(material, friction + 0.1, friction, 0);
|
|
|
|
|
- NewtonMaterialSetContactFrictionCoef(material, friction + 0.1, friction, 1);
|
|
|
|
|
-
|
|
|
|
|
- NewtonMaterialSetContactElasticity(material, elasticity);
|
|
|
|
|
-
|
|
|
|
|
- contact = NewtonContactJointGetNextContact(contactJoint, contact);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
} // end namespace anki
|
|
} // end namespace anki
|