Przeglądaj źródła

Fixed physics update so it actually gets called the requested amount of times instead of skipping
Fixed colliders so they properly register with their static actors
Fixed physics mesh and material so they are properly created as core shared pointers, instead of normals ones
Flipped triangle mesh normals so collisions are handled properly

BearishSun 9 lat temu
rodzic
commit
5f58440c58

+ 1 - 1
Source/BansheePhysX/Source/BsFPhysXCollider.cpp

@@ -51,7 +51,7 @@ namespace BansheeEngine
 			PxActor* actor = mShape->getActor();
 			PxActor* actor = mShape->getActor();
 			if (actor != nullptr)
 			if (actor != nullptr)
 			{
 			{
-				PxRigidActor* rigidActor = mShape->is<PxRigidActor>();
+				PxRigidActor* rigidActor = actor->is<PxRigidActor>();
 				if (rigidActor != nullptr)
 				if (rigidActor != nullptr)
 				{
 				{
 					rigidActor->detachShape(*mShape);
 					rigidActor->detachShape(*mShape);

+ 10 - 4
Source/BansheePhysX/Source/BsPhysX.cpp

@@ -518,7 +518,7 @@ namespace BansheeEngine
 			return;
 			return;
 		}
 		}
 
 
-		float simulationAmount = curFrameTime - mLastSimulationTime;
+		float simulationAmount = std::max(curFrameTime - mLastSimulationTime, mSimulationStep); // At least one step
 		INT32 numIterations = Math::floorToInt(simulationAmount / mSimulationStep);
 		INT32 numIterations = Math::floorToInt(simulationAmount / mSimulationStep);
 
 
 		// If too many iterations are required, increase time step. This should only happen in extreme situations (or when
 		// If too many iterations are required, increase time step. This should only happen in extreme situations (or when
@@ -538,6 +538,7 @@ namespace BansheeEngine
 
 
 			mScene->simulate(step, nullptr, scratchBuffer, SCRATCH_BUFFER_SIZE);
 			mScene->simulate(step, nullptr, scratchBuffer, SCRATCH_BUFFER_SIZE);
 			simulationAmount -= step;
 			simulationAmount -= step;
+			mLastSimulationTime += step;
 
 
 			UINT32 errorState;
 			UINT32 errorState;
 			if(!mScene->fetchResults(true, &errorState))
 			if(!mScene->fetchResults(true, &errorState))
@@ -557,6 +558,12 @@ namespace BansheeEngine
 			iterationCount++;
 			iterationCount++;
 		}
 		}
 
 
+		if(iterationCount == 0)
+		{
+			LOGWRN(toString(step) + " - " + toString(mSimulationStep) + " - " + 
+				toString(numIterations) + " - " + toString(curFrameTime) + " - " + toString(mLastSimulationTime) + " - " + toString(nextFrameTime));
+		}
+
 		// Update rigidbodies with new transforms
 		// Update rigidbodies with new transforms
 		PxU32 numActiveTransforms;
 		PxU32 numActiveTransforms;
 		const PxActiveTransform* activeTransforms = mScene->getActiveTransforms(numActiveTransforms);
 		const PxActiveTransform* activeTransforms = mScene->getActiveTransforms(numActiveTransforms);
@@ -580,7 +587,6 @@ namespace BansheeEngine
 
 
 		// TODO - Consider extrapolating for the remaining "simulationAmount" value
 		// TODO - Consider extrapolating for the remaining "simulationAmount" value
 
 
-		mLastSimulationTime = curFrameTime; 
 		mUpdateInProgress = false;
 		mUpdateInProgress = false;
 
 
 		triggerEvents();
 		triggerEvents();
@@ -699,12 +705,12 @@ namespace BansheeEngine
 
 
 	SPtr<PhysicsMaterial> PhysX::createMaterial(float staticFriction, float dynamicFriction, float restitution)
 	SPtr<PhysicsMaterial> PhysX::createMaterial(float staticFriction, float dynamicFriction, float restitution)
 	{
 	{
-		return bs_shared_ptr_new<PhysXMaterial>(mPhysics, staticFriction, dynamicFriction, restitution);
+		return bs_core_ptr_new<PhysXMaterial>(mPhysics, staticFriction, dynamicFriction, restitution);
 	}
 	}
 
 
 	SPtr<PhysicsMesh> PhysX::createMesh(const MeshDataPtr& meshData, PhysicsMeshType type)
 	SPtr<PhysicsMesh> PhysX::createMesh(const MeshDataPtr& meshData, PhysicsMeshType type)
 	{
 	{
-		return bs_shared_ptr_new<PhysXMesh>(meshData, type);
+		return bs_core_ptr_new<PhysXMesh>(meshData, type);
 	}
 	}
 
 
 	SPtr<Rigidbody> PhysX::createRigidbody(const HSceneObject& linkedSO)
 	SPtr<Rigidbody> PhysX::createRigidbody(const HSceneObject& linkedSO)

+ 20 - 4
Source/BansheePhysX/Source/BsPhysXMesh.cpp

@@ -201,7 +201,8 @@ namespace BansheeEngine
 			meshDesc.points.data = meshData->getElementData(VES_POSITION);
 			meshDesc.points.data = meshData->getElementData(VES_POSITION);
 
 
 			meshDesc.triangles.count = meshData->getNumIndices() / 3;
 			meshDesc.triangles.count = meshData->getNumIndices() / 3;
-			
+			meshDesc.flags |= PxMeshFlag::eFLIPNORMALS;
+
 			IndexType indexType = meshData->getIndexType();
 			IndexType indexType = meshData->getIndexType();
 			if (indexType == IT_32BIT)
 			if (indexType == IT_32BIT)
 			{
 			{
@@ -372,13 +373,28 @@ namespace BansheeEngine
 			if(mTriangleMesh->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES)
 			if(mTriangleMesh->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES)
 			{
 			{
 				const UINT16* indices = (const UINT16*)mTriangleMesh->getTriangles();
 				const UINT16* indices = (const UINT16*)mTriangleMesh->getTriangles();
-				for (UINT32 i = 0; i < numIndices; i++)
-					outIndices[i] = (UINT32)indices[i];
+
+				UINT32 numTriangles = numIndices / 3;
+				for (UINT32 i = 0; i < numTriangles; i++)
+				{
+					// Flip triangles as PhysX keeps them opposite to what Banshee expects
+					outIndices[i * 3 + 0] = (UINT32)indices[i * 3 + 0];
+					outIndices[i * 3 + 1] = (UINT32)indices[i * 3 + 2];
+					outIndices[i * 3 + 2] = (UINT32)indices[i * 3 + 1];
+				}
 			}
 			}
 			else
 			else
 			{
 			{
 				const UINT32* indices = (const UINT32*)mTriangleMesh->getTriangles();
 				const UINT32* indices = (const UINT32*)mTriangleMesh->getTriangles();
-				memcpy(outIndices, indices, numIndices * sizeof(UINT32));
+
+				UINT32 numTriangles = numIndices / 3;
+				for (UINT32 i = 0; i < numTriangles; i++)
+				{
+					// Flip triangles as PhysX keeps them opposite to what Banshee expects
+					outIndices[i * 3 + 0] = indices[i * 3 + 0];
+					outIndices[i * 3 + 1] = indices[i * 3 + 2];
+					outIndices[i * 3 + 2] = indices[i * 3 + 1];
+				}
 			}
 			}
 		}
 		}