Explorar el Código

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 hace 9 años
padre
commit
5f58440c58

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

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

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

@@ -518,7 +518,7 @@ namespace BansheeEngine
 			return;
 		}
 
-		float simulationAmount = curFrameTime - mLastSimulationTime;
+		float simulationAmount = std::max(curFrameTime - mLastSimulationTime, mSimulationStep); // At least one step
 		INT32 numIterations = Math::floorToInt(simulationAmount / mSimulationStep);
 
 		// 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);
 			simulationAmount -= step;
+			mLastSimulationTime += step;
 
 			UINT32 errorState;
 			if(!mScene->fetchResults(true, &errorState))
@@ -557,6 +558,12 @@ namespace BansheeEngine
 			iterationCount++;
 		}
 
+		if(iterationCount == 0)
+		{
+			LOGWRN(toString(step) + " - " + toString(mSimulationStep) + " - " + 
+				toString(numIterations) + " - " + toString(curFrameTime) + " - " + toString(mLastSimulationTime) + " - " + toString(nextFrameTime));
+		}
+
 		// Update rigidbodies with new transforms
 		PxU32 numActiveTransforms;
 		const PxActiveTransform* activeTransforms = mScene->getActiveTransforms(numActiveTransforms);
@@ -580,7 +587,6 @@ namespace BansheeEngine
 
 		// TODO - Consider extrapolating for the remaining "simulationAmount" value
 
-		mLastSimulationTime = curFrameTime; 
 		mUpdateInProgress = false;
 
 		triggerEvents();
@@ -699,12 +705,12 @@ namespace BansheeEngine
 
 	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)
 	{
-		return bs_shared_ptr_new<PhysXMesh>(meshData, type);
+		return bs_core_ptr_new<PhysXMesh>(meshData, type);
 	}
 
 	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.triangles.count = meshData->getNumIndices() / 3;
-			
+			meshDesc.flags |= PxMeshFlag::eFLIPNORMALS;
+
 			IndexType indexType = meshData->getIndexType();
 			if (indexType == IT_32BIT)
 			{
@@ -372,13 +373,28 @@ namespace BansheeEngine
 			if(mTriangleMesh->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES)
 			{
 				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
 			{
 				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];
+				}
 			}
 		}