Browse Source

Move the PhysicsWorld to the new engine

Panagiotis Christopoulos Charitos 7 years ago
parent
commit
e919c278c4
3 changed files with 36 additions and 168 deletions
  1. 7 0
      src/anki/physics/Common.h
  2. 21 109
      src/anki/physics/PhysicsWorld.cpp
  3. 8 59
      src/anki/physics/PhysicsWorld.h

+ 7 - 0
src/anki/physics/Common.h

@@ -10,6 +10,13 @@
 #include <anki/util/Ptr.h>
 #include <anki/util/Ptr.h>
 #include <anki/Math.h>
 #include <anki/Math.h>
 
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Winfinite-recursion"
+#define BT_THREADSAFE 0
+#include <btBulletCollisionCommon.h>
+#include <btBulletDynamicsCommon.h>
+#pragma GCC diagnostic pop
+
 // Have all the newton headers here because they polute the global namespace
 // Have all the newton headers here because they polute the global namespace
 #include <Newton.h>
 #include <Newton.h>
 #include <dLinearAlgebra.h>
 #include <dLinearAlgebra.h>

+ 21 - 109
src/anki/physics/PhysicsWorld.cpp

@@ -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

+ 8 - 59
src/anki/physics/PhysicsWorld.h

@@ -11,9 +11,6 @@
 namespace anki
 namespace anki
 {
 {
 
 
-// Forward
-class CharacterControllerManager;
-
 /// @addtogroup physics
 /// @addtogroup physics
 /// @{
 /// @{
 
 
@@ -29,57 +26,30 @@ public:
 	template<typename T, typename... TArgs>
 	template<typename T, typename... TArgs>
 	PhysicsPtr<T> newInstance(TArgs&&... args);
 	PhysicsPtr<T> newInstance(TArgs&&... args);
 
 
-	/// Start asynchronous update.
-	Error updateAsync(Second dt);
-
-	/// End asynchronous update.
-	void waitUpdate();
-
-	const Vec4& getGravity() const
-	{
-		return m_gravity;
-	}
+	/// Do the update.
+	Error update(Second dt);
 
 
 anki_internal:
 anki_internal:
-	NewtonWorld* getNewtonWorld() const
+	btDynamicsWorld* getBtWorld() const
 	{
 	{
 		ANKI_ASSERT(m_world);
 		ANKI_ASSERT(m_world);
 		return m_world;
 		return m_world;
 	}
 	}
 
 
-	/// Used for static collision.
-	NewtonCollision* getNewtonScene() const
-	{
-		return m_sceneCollision;
-	}
-
-	Second getDeltaTime() const
-	{
-		return m_dt;
-	}
-
 	void deleteObjectDeferred(PhysicsObject* obj)
 	void deleteObjectDeferred(PhysicsObject* obj)
 	{
 	{
 		LockGuard<Mutex> lock(m_mtx);
 		LockGuard<Mutex> lock(m_mtx);
 		m_forDeletion.pushBack(m_alloc, obj);
 		m_forDeletion.pushBack(m_alloc, obj);
 	}
 	}
 
 
-	CharacterControllerManager& getCharacterControllerManager()
-	{
-		ANKI_ASSERT(m_playerManager);
-		return *m_playerManager;
-	}
-
 private:
 private:
 	HeapAllocator<U8> m_alloc;
 	HeapAllocator<U8> m_alloc;
-	mutable NewtonWorld* m_world = nullptr;
-	NewtonCollision* m_sceneCollision = nullptr;
-	NewtonBody* m_sceneBody = nullptr;
-	Vec4 m_gravity = Vec4(0.0f, -9.8f, 0.0f, 0.0f);
-	Second m_dt = 0.0;
 
 
-	/// @note Don't delete it. Newton will
-	CharacterControllerManager* m_playerManager = nullptr;
+	btBroadphaseInterface* m_broadphase = nullptr;
+	btDefaultCollisionConfiguration* m_collisionConfig = nullptr;
+	btCollisionDispatcher* m_dispatcher = nullptr;
+	btSequentialImpulseConstraintSolver* m_solver = nullptr;
+	btDiscreteDynamicsWorld* m_world = nullptr;
 
 
 	Mutex m_mtx;
 	Mutex m_mtx;
 	List<PhysicsObject*> m_forDeletion;
 	List<PhysicsObject*> m_forDeletion;
@@ -88,26 +58,6 @@ private:
 	PhysicsPtr<T> newObjectInternal(TArgs&&... args);
 	PhysicsPtr<T> newObjectInternal(TArgs&&... args);
 
 
 	void cleanupMarkedForDeletion();
 	void cleanupMarkedForDeletion();
-
-	static void destroyCallback(const NewtonWorld* const world, void* const listenerUserData)
-	{
-	}
-
-	void registerObject(PhysicsObject* ptr);
-
-	static int onAabbOverlapCallback(const NewtonMaterial* const material,
-		const NewtonBody* const body0,
-		const NewtonBody* const body1,
-		int threadIndex)
-	{
-		(void)material;
-		(void)body0;
-		(void)body1;
-		(void)threadIndex;
-		return 1;
-	}
-
-	static void onContactCallback(const NewtonJoint* contactJoint, F32 timestep, int threadIndex);
 };
 };
 
 
 template<typename T, typename... TArgs>
 template<typename T, typename... TArgs>
@@ -121,7 +71,6 @@ inline PhysicsPtr<T> PhysicsWorld::newInstance(TArgs&&... args)
 
 
 	if(!err)
 	if(!err)
 	{
 	{
-		registerObject(ptr);
 		out.reset(ptr);
 		out.reset(ptr);
 	}
 	}
 	else
 	else