void FEndPhysicsTickFunction::ExecuteTick(float DeltaTime, enum ELevelTick TickType, ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent) { check(Target); FPhysScene* PhysScene = Target->GetPhysicsScene(); if (PhysScene == NULL) { return; } FGraphEventRef PhysicsComplete = PhysScene->GetCompletionEvent(); if (PhysicsComplete.GetReference() && !PhysicsComplete->IsComplete()) { // don't release the next tick group until the physics has completed and we have run FinishPhysicsSim DECLARE_CYCLE_STAT(TEXT("FSimpleDelegateGraphTask.FinishPhysicsSim"), STAT_FSimpleDelegateGraphTask_FinishPhysicsSim, STATGROUP_TaskGraphTasks); MyCompletionGraphEvent->DontCompleteUntil( FSimpleDelegateGraphTask::CreateAndDispatchWhenReady( FSimpleDelegateGraphTask::FDelegate::CreateUObject(Target, &UWorld::FinishPhysicsSim), GET_STATID(STAT_FSimpleDelegateGraphTask_FinishPhysicsSim), PhysicsComplete, ENamedThreads::GameThread ) ); } else { // it was already done, so let just do it. Target->FinishPhysicsSim(); } }
void UDestructibleComponent::RefreshBoneTransforms() { #if WITH_APEX if(ApexDestructibleActor != NULL && SkeletalMesh) { UDestructibleMesh* TheDestructibleMesh = GetDestructibleMesh(); // Save a pointer to the APEX NxDestructibleAsset physx::NxDestructibleAsset* ApexDestructibleAsset = TheDestructibleMesh->ApexDestructibleAsset; check(ApexDestructibleAsset); { // Lock here so we don't encounter race conditions with the destruction processing FPhysScene* PhysScene = World->GetPhysicsScene(); check(PhysScene); const uint32 SceneType = (BodyInstance.bUseAsyncScene && PhysScene->HasAsyncScene()) ? PST_Async : PST_Sync; PxScene* PScene = PhysScene->GetPhysXScene(SceneType); check(PScene); SCOPED_SCENE_WRITE_LOCK(PScene); SCOPED_SCENE_READ_LOCK(PScene); // Try to acquire event buffer const physx::NxDestructibleChunkEvent* EventBuffer; physx::PxU32 EventBufferSize; if (ApexDestructibleActor->acquireChunkEventBuffer(EventBuffer, EventBufferSize)) { // Buffer acquired while (EventBufferSize--) { const physx::NxDestructibleChunkEvent& Event = *EventBuffer++; // Right now the only events are visibility changes. So as an optimization we won't check for the event type. // if (Event.event & physx::NxDestructibleChunkEvent::VisibilityChanged) const bool bVisible = (Event.event & physx::NxDestructibleChunkEvent::ChunkVisible) != 0; SetChunkVisible(Event.chunkIndex, bVisible); } // Release buffer (will be cleared) ApexDestructibleActor->releaseChunkEventBuffer(); } } // Update poses for visible chunks const physx::PxU16* VisibleChunks = ApexDestructibleActor->getVisibleChunks(); physx::PxU16 VisibleChunkCount = ApexDestructibleActor->getNumVisibleChunks(); while (VisibleChunkCount--) { const physx::PxU16 ChunkIndex = *VisibleChunks++; // BRGTODO : Make a direct method to access the Px objects' quats const physx::PxMat44 ChunkPoseRT = ApexDestructibleActor->getChunkPose(ChunkIndex); // Unscaled const physx::PxTransform Transform(ChunkPoseRT); SetChunkWorldRT(ChunkIndex, P2UQuat(Transform.q), P2UVector(Transform.p)); } // Send bones to render thread at end of frame MarkRenderDynamicDataDirty(); } #endif // #if WITH_APEX }
void UWorld::StartClothSim() { FPhysScene* PhysScene = GetPhysicsScene(); if (PhysScene == NULL) { return; } PhysScene->StartCloth(); }
void UWorld::FinishPhysicsSim() { FPhysScene* PhysScene = GetPhysicsScene(); if (PhysScene == NULL) { return; } PhysScene->EndFrame(LineBatcher); }
void FEndClothSimulationFunction::ExecuteTick(float DeltaTime, enum ELevelTick TickType, ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent) { //We currently have nothing to do in this tick group, but we still want to wait on cloth simulation so that PostPhysics is ensured this is done check(Target); FPhysScene* PhysScene = Target->GetPhysicsScene(); if (PhysScene == NULL) { return; } PhysScene->WaitClothScene(); }
bool FConstraintInstance::CreatePxJoint(physx::PxRigidActor* PActor1, physx::PxRigidActor* PActor2, physx::PxScene* PScene, const float Scale) { ConstraintData = nullptr; FTransform Local1 = GetRefFrame(EConstraintFrame::Frame1); Local1.ScaleTranslation(FVector(Scale)); checkf(Local1.IsValid() && !Local1.ContainsNaN(), TEXT("%s"), *Local1.ToString()); FTransform Local2 = GetRefFrame(EConstraintFrame::Frame2); Local2.ScaleTranslation(FVector(Scale)); checkf(Local2.IsValid() && !Local2.ContainsNaN(), TEXT("%s"), *Local2.ToString()); SCOPED_SCENE_WRITE_LOCK(PScene); // Because PhysX keeps limits/axes locked in the first body reference frame, whereas Unreal keeps them in the second body reference frame, we have to flip the bodies here. PxD6Joint* PD6Joint = PxD6JointCreate(*GPhysXSDK, PActor2, U2PTransform(Local2), PActor1, U2PTransform(Local1)); if (PD6Joint == nullptr) { UE_LOG(LogPhysics, Log, TEXT("URB_ConstraintInstance::InitConstraint - Invalid 6DOF joint (%s)"), *JointName.ToString()); return false; } ///////// POINTERS PD6Joint->userData = &PhysxUserData; // Remember reference to scene index. FPhysScene* RBScene = FPhysxUserData::Get<FPhysScene>(PScene->userData); if (RBScene->GetPhysXScene(PST_Sync) == PScene) { SceneIndex = RBScene->PhysXSceneIndex[PST_Sync]; } else if (RBScene->GetPhysXScene(PST_Async) == PScene) { SceneIndex = RBScene->PhysXSceneIndex[PST_Async]; } else { UE_LOG(LogPhysics, Log, TEXT("URB_ConstraintInstance::InitConstraint: PxScene has inconsistent FPhysScene userData. No joint created.")); return false; } ConstraintData = PD6Joint; return true; }
bool UDestructibleComponent::ExecuteOnPhysicsReadWrite(TFunctionRef<void()> Func) const { #if WITH_APEX if (ApexDestructibleActor) { FPhysScene* PhysScene = GetWorld()->GetPhysicsScene(); // Destructibles are always dynamic or kinematic, and therefore only go into one of the scenes const uint32 SceneType = BodyInstance.UseAsyncScene(PhysScene) ? PST_Async : PST_Sync; PxScene* PScene = PhysScene->GetPhysXScene(SceneType); SCOPED_SCENE_WRITE_LOCK(PScene); Func(); return true; } #endif return false; }
void FEndPhysicsTickFunction::ExecuteTick(float DeltaTime, enum ELevelTick TickType, ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent) { QUICK_SCOPE_CYCLE_COUNTER(FEndPhysicsTickFunction_ExecuteTick); check(Target); FPhysScene* PhysScene = Target->GetPhysicsScene(); if (PhysScene == NULL) { return; } FGraphEventRef PhysicsComplete = PhysScene->GetCompletionEvent(); if (PhysicsComplete.GetReference() && !PhysicsComplete->IsComplete()) { // don't release the next tick group until the physics has completed and we have run FinishPhysicsSim DECLARE_CYCLE_STAT(TEXT("FSimpleDelegateGraphTask.FinishPhysicsSim"), STAT_FSimpleDelegateGraphTask_FinishPhysicsSim, STATGROUP_TaskGraphTasks); MyCompletionGraphEvent->DontCompleteUntil( FSimpleDelegateGraphTask::CreateAndDispatchWhenReady( FSimpleDelegateGraphTask::FDelegate::CreateUObject(Target, &UWorld::FinishPhysicsSim), GET_STATID(STAT_FSimpleDelegateGraphTask_FinishPhysicsSim), PhysicsComplete, ENamedThreads::GameThread ) ); } else { // it was already done, so let just do it. Target->FinishPhysicsSim(); } #if PHYSX_MEMORY_VALIDATION static int32 Frequency = 0; if (Frequency++ > 10) { Frequency = 0; GPhysXAllocator->ValidateHeaders(); } #endif }
void UWorld::SetupPhysicsTickFunctions(float DeltaSeconds) { StartPhysicsTickFunction.bCanEverTick = true; StartPhysicsTickFunction.Target = this; EndPhysicsTickFunction.bCanEverTick = true; EndPhysicsTickFunction.Target = this; StartClothTickFunction.bCanEverTick = true; StartClothTickFunction.Target = this; EndClothTickFunction.bCanEverTick = true; EndClothTickFunction.Target = this; // see if we need to update tick registration bool bNeedToUpdateTickRegistration = (bShouldSimulatePhysics != StartPhysicsTickFunction.IsTickFunctionRegistered()) || (bShouldSimulatePhysics != EndPhysicsTickFunction.IsTickFunctionRegistered()) || (bShouldSimulatePhysics != StartClothTickFunction.IsTickFunctionRegistered()) || (bShouldSimulatePhysics != EndClothTickFunction.IsTickFunctionRegistered()); if (bNeedToUpdateTickRegistration && PersistentLevel) { if (bShouldSimulatePhysics && !StartPhysicsTickFunction.IsTickFunctionRegistered()) { StartPhysicsTickFunction.TickGroup = TG_StartPhysics; StartPhysicsTickFunction.RegisterTickFunction(PersistentLevel); } else if (!bShouldSimulatePhysics && StartPhysicsTickFunction.IsTickFunctionRegistered()) { StartPhysicsTickFunction.UnRegisterTickFunction(); } if (bShouldSimulatePhysics && !EndPhysicsTickFunction.IsTickFunctionRegistered()) { EndPhysicsTickFunction.TickGroup = TG_EndPhysics; EndPhysicsTickFunction.RegisterTickFunction(PersistentLevel); EndPhysicsTickFunction.AddPrerequisite(this, StartPhysicsTickFunction); } else if (!bShouldSimulatePhysics && EndPhysicsTickFunction.IsTickFunctionRegistered()) { EndPhysicsTickFunction.RemovePrerequisite(this, StartPhysicsTickFunction); EndPhysicsTickFunction.UnRegisterTickFunction(); } //cloth if (bShouldSimulatePhysics && !StartClothTickFunction.IsTickFunctionRegistered()) { StartClothTickFunction.TickGroup = TG_StartCloth; StartClothTickFunction.RegisterTickFunction(PersistentLevel); } else if (!bShouldSimulatePhysics && StartClothTickFunction.IsTickFunctionRegistered()) { StartClothTickFunction.UnRegisterTickFunction(); } if (bShouldSimulatePhysics && !EndClothTickFunction.IsTickFunctionRegistered()) { EndClothTickFunction.TickGroup = TG_EndCloth; EndClothTickFunction.RegisterTickFunction(PersistentLevel); EndClothTickFunction.AddPrerequisite(this, StartClothTickFunction); } else if (!bShouldSimulatePhysics && EndClothTickFunction.IsTickFunctionRegistered()) { EndClothTickFunction.RemovePrerequisite(this, StartClothTickFunction); EndClothTickFunction.UnRegisterTickFunction(); } } FPhysScene* PhysScene = GetPhysicsScene(); if (PhysicsScene == NULL) { return; } #if WITH_PHYSX // When ticking the main scene, clean up any physics engine resources (once a frame) DeferredPhysResourceCleanup(); #endif // Update gravity in case it changed FVector DefaultGravity( 0.f, 0.f, GetGravityZ() ); static const auto CVar_MaxPhysicsDeltaTime = IConsoleManager::Get().FindTConsoleVariableDataFloat(TEXT("p.MaxPhysicsDeltaTime")); PhysScene->SetUpForFrame(&DefaultGravity, DeltaSeconds, UPhysicsSettings::Get()->MaxPhysicsDeltaTime); }
PxFilterFlags PhysXSimFilterShader( PxFilterObjectAttributes attributes0, PxFilterData filterData0, PxFilterObjectAttributes attributes1, PxFilterData filterData1, PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize ) { //UE_LOG(LogPhysics, Log, TEXT("filterData0 (%s): %x %x %x %x"), *ObjTypeToString(attributes0), filterData0.word0, filterData0.word1, filterData0.word2, filterData0.word3); //UE_LOG(LogPhysics, Log, TEXT("filterData1 (%s): %x %x %x %x"), *ObjTypeToString(attributes1), filterData1.word0, filterData1.word1, filterData1.word2, filterData1.word3); bool k0 = PxFilterObjectIsKinematic(attributes0); bool k1 = PxFilterObjectIsKinematic(attributes1); // Find out which channels the objects are in ECollisionChannel Channel0 = GetCollisionChannel(filterData0.word3); ECollisionChannel Channel1 = GetCollisionChannel(filterData1.word3); // ignore kinematic-kinematic interactions which don't involve a destructible if(k0 && k1 && (Channel0 != ECC_Destructible) && (Channel1 != ECC_Destructible)) { //return PxFilterFlag::eKILL; return PxFilterFlag::eSUPPRESS; //NOTE: Waiting on physx fix for refiltering on aggregates. For now use supress which automatically tests when changes to simulation happen } bool s0 = PxGetFilterObjectType(attributes0) == PxFilterObjectType::eRIGID_STATIC; bool s1 = PxGetFilterObjectType(attributes1) == PxFilterObjectType::eRIGID_STATIC; //ignore static-kinematic (this assumes that statics can't be flagged as kinematics) // should return eSUPPRESS here instead eKILL so that kinematics vs statics will still be considered once kinematics become dynamic (dying ragdoll case) if((k0 || k1) && (s0 || s1)) { return PxFilterFlag::eSUPPRESS; } // if these bodies are from the same component, use the disable table to see if we should disable collision. This case should only happen for things like skeletalmesh and destruction. The table is only created for skeletal mesh components at the moment if(filterData0.word2 == filterData1.word2) { check(constantBlockSize == sizeof(FPhysSceneShaderInfo)); const FPhysSceneShaderInfo* PhysSceneShaderInfo = (const FPhysSceneShaderInfo*) constantBlock; check(PhysSceneShaderInfo); FPhysScene * PhysScene = PhysSceneShaderInfo->PhysScene; check(PhysScene); const TMap<uint32, TMap<FRigidBodyIndexPair, bool> *> & CollisionDisableTableLookup = PhysScene->GetCollisionDisableTableLookup(); TMap<FRigidBodyIndexPair, bool>* const * DisableTablePtrPtr = CollisionDisableTableLookup.Find(filterData1.word2); if (DisableTablePtrPtr) //Since collision table is deferred during sub-stepping it's possible that we won't get the collision disable table until the next frame { TMap<FRigidBodyIndexPair, bool>* DisableTablePtr = *DisableTablePtrPtr; FRigidBodyIndexPair BodyPair(filterData0.word0, filterData1.word0); // body indexes are stored in word 0 if (DisableTablePtr->Find(BodyPair)) { return PxFilterFlag::eKILL; } } } // see if 0/1 would like to block the other PxU32 BlockFlagTo1 = (ECC_TO_BITFIELD(Channel1) & filterData0.word1); PxU32 BlockFlagTo0 = (ECC_TO_BITFIELD(Channel0) & filterData1.word1); bool bDoesWantToBlock = (BlockFlagTo1 && BlockFlagTo0); // if don't want to block, suppress if ( !bDoesWantToBlock ) { return PxFilterFlag::eSUPPRESS; } PxU32 FilterFlags0 = (filterData0.word3 & 0xFFFFFF); PxU32 FilterFlags1 = (filterData1.word3 & 0xFFFFFF); pairFlags = PxPairFlag::eCONTACT_DEFAULT; //todo enabling CCD objects against everything else for now if(!(k0 && k1) && ((FilterFlags0&EPDF_CCD) || (FilterFlags1&EPDF_CCD))) { pairFlags |= PxPairFlag::eDETECT_CCD_CONTACT | PxPairFlag::eSOLVE_CONTACT; } if((FilterFlags0&EPDF_ContactNotify) || (FilterFlags1&EPDF_ContactNotify)) { pairFlags |= (PxPairFlag::eNOTIFY_TOUCH_FOUND | PxPairFlag::eNOTIFY_TOUCH_PERSISTS | PxPairFlag::eNOTIFY_CONTACT_POINTS ); } if ((FilterFlags0&EPDF_ModifyContacts) || (FilterFlags1&EPDF_ModifyContacts)) { pairFlags |= (PxPairFlag::eMODIFY_CONTACTS); } return PxFilterFlags(); }
void USkeletalMeshComponent::UpdateKinematicBonesToAnim(const TArray<FTransform>& InSpaceBases, ETeleportType Teleport, bool bNeedsSkinning) { SCOPE_CYCLE_COUNTER(STAT_UpdateRBBones); // This below code produces some interesting result here // - below codes update physics data, so if you don't update pose, the physics won't have the right result // - but if we just update physics bone without update current pose, it will have stale data // If desired, pass the animation data to the physics joints so they can be used by motors. // See if we are going to need to update kinematics const bool bUpdateKinematics = (KinematicBonesUpdateType != EKinematicBonesUpdateToPhysics::SkipAllBones); const bool bTeleport = Teleport == ETeleportType::TeleportPhysics; // If desired, update physics bodies associated with skeletal mesh component to match. if(!bUpdateKinematics && !(bTeleport && IsAnySimulatingPhysics())) { // nothing to do return; } // Get the scene, and do nothing if we can't get one. FPhysScene* PhysScene = nullptr; if (GetWorld() != nullptr) { PhysScene = GetWorld()->GetPhysicsScene(); } if(PhysScene == nullptr) { return; } const FTransform& CurrentLocalToWorld = ComponentToWorld; // Gracefully handle NaN if(CurrentLocalToWorld.ContainsNaN()) { return; } // If desired, draw the skeleton at the point where we pass it to the physics. if (bShowPrePhysBones && SkeletalMesh && InSpaceBases.Num() == SkeletalMesh->RefSkeleton.GetNum()) { for (int32 i = 1; i<InSpaceBases.Num(); i++) { FVector ThisPos = CurrentLocalToWorld.TransformPosition(InSpaceBases[i].GetLocation()); int32 ParentIndex = SkeletalMesh->RefSkeleton.GetParentIndex(i); FVector ParentPos = CurrentLocalToWorld.TransformPosition(InSpaceBases[ParentIndex].GetLocation()); GetWorld()->LineBatcher->DrawLine(ThisPos, ParentPos, AnimSkelDrawColor, SDPG_Foreground); } } // warn if it has non-uniform scale const FVector& MeshScale3D = CurrentLocalToWorld.GetScale3D(); #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST) if( !MeshScale3D.IsUniform() ) { UE_LOG(LogPhysics, Log, TEXT("USkeletalMeshComponent::UpdateKinematicBonesToAnim : Non-uniform scale factor (%s) can cause physics to mismatch for %s SkelMesh: %s"), *MeshScale3D.ToString(), *GetFullName(), SkeletalMesh ? *SkeletalMesh->GetFullName() : TEXT("NULL")); } #endif if (bEnablePerPolyCollision == false) { const UPhysicsAsset* const PhysicsAsset = GetPhysicsAsset(); if (PhysicsAsset && SkeletalMesh && Bodies.Num() > 0) { #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST) if (!ensure(PhysicsAsset->BodySetup.Num() == Bodies.Num())) { // related to TTP 280315 UE_LOG(LogPhysics, Warning, TEXT("Mesh (%s) has PhysicsAsset(%s), and BodySetup(%d) and Bodies(%d) don't match"), *SkeletalMesh->GetName(), *PhysicsAsset->GetName(), PhysicsAsset->BodySetup.Num(), Bodies.Num()); return; } #endif #if WITH_PHYSX // Lock the scenes we need (flags set in InitArticulated) if(bHasBodiesInSyncScene) { SCENE_LOCK_WRITE(PhysScene->GetPhysXScene(PST_Sync)) } if (bHasBodiesInAsyncScene) { SCENE_LOCK_WRITE(PhysScene->GetPhysXScene(PST_Async)) } #endif // Iterate over each body for (int32 i = 0; i < Bodies.Num(); i++) { // If we have a physics body, and its kinematic... FBodyInstance* BodyInst = Bodies[i]; check(BodyInst); if (bTeleport || (BodyInst->IsValidBodyInstance() && !BodyInst->IsInstanceSimulatingPhysics())) { const int32 BoneIndex = BodyInst->InstanceBoneIndex; // If we could not find it - warn. if (BoneIndex == INDEX_NONE || BoneIndex >= GetNumSpaceBases()) { const FName BodyName = PhysicsAsset->BodySetup[i]->BoneName; UE_LOG(LogPhysics, Log, TEXT("UpdateRBBones: WARNING: Failed to find bone '%s' need by PhysicsAsset '%s' in SkeletalMesh '%s'."), *BodyName.ToString(), *PhysicsAsset->GetName(), *SkeletalMesh->GetName()); } else { #if WITH_PHYSX // update bone transform to world const FTransform BoneTransform = InSpaceBases[BoneIndex] * CurrentLocalToWorld; if(BoneTransform.ContainsNaN()) { const FName BodyName = PhysicsAsset->BodySetup[i]->BoneName; UE_LOG(LogPhysics, Warning, TEXT("UpdateKinematicBonesToAnim: Trying to set transform with bad data %s on PhysicsAsset '%s' in SkeletalMesh '%s' for bone '%s'"), *BoneTransform.ToHumanReadableString(), *PhysicsAsset->GetName(), *SkeletalMesh->GetName(), *BodyName.ToString()); continue; } // If kinematic and not teleporting, set kinematic target PxRigidDynamic* PRigidDynamic = BodyInst->GetPxRigidDynamic_AssumesLocked(); if (!IsRigidBodyNonKinematic_AssumesLocked(PRigidDynamic) && !bTeleport) { PhysScene->SetKinematicTarget_AssumesLocked(BodyInst, BoneTransform, true); } // Otherwise, set global pose else { const PxTransform PNewPose = U2PTransform(BoneTransform); ensure(PNewPose.isValid()); PRigidDynamic->setGlobalPose(PNewPose); } #endif // now update scale // if uniform, we'll use BoneTranform if (MeshScale3D.IsUniform()) { // @todo UE4 should we update scale when it's simulated? BodyInst->UpdateBodyScale(BoneTransform.GetScale3D()); } else { // @note When you have non-uniform scale on mesh base, // hierarchical bone transform can update scale too often causing performance issue // So we just use mesh scale for all bodies when non-uniform // This means physics representation won't be accurate, but // it is performance friendly by preventing too frequent physics update BodyInst->UpdateBodyScale(MeshScale3D); } } } else { //make sure you have physics weight or blendphysics on, otherwise, you'll have inconsistent representation of bodies // @todo make this to be kismet log? But can be too intrusive if (!bBlendPhysics && BodyInst->PhysicsBlendWeight <= 0.f && BodyInst->BodySetup.IsValid()) { UE_LOG(LogPhysics, Warning, TEXT("%s(Mesh %s, PhysicsAsset %s, Bone %s) is simulating, but no blending. "), *GetName(), *GetNameSafe(SkeletalMesh), *GetNameSafe(PhysicsAsset), *BodyInst->BodySetup.Get()->BoneName.ToString()); } } } #if WITH_PHYSX // Unlock the scenes if (bHasBodiesInSyncScene) { SCENE_UNLOCK_WRITE(PhysScene->GetPhysXScene(PST_Sync)) } if (bHasBodiesInAsyncScene) { SCENE_UNLOCK_WRITE(PhysScene->GetPhysXScene(PST_Async)) } #endif } } else { //per poly update requires us to update all vertex positions if (MeshObject)
void USkeletalMeshComponent::PerformBlendPhysicsBones(const TArray<FBoneIndexType>& InRequiredBones, TArray<FTransform>& InLocalAtoms) { // Get drawscale from Owner (if there is one) FVector TotalScale3D = ComponentToWorld.GetScale3D(); FVector RecipScale3D = TotalScale3D.Reciprocal(); UPhysicsAsset * const PhysicsAsset = GetPhysicsAsset(); check( PhysicsAsset ); if (GetNumSpaceBases() == 0) { return; } // Get the scene, and do nothing if we can't get one. FPhysScene* PhysScene = nullptr; if (GetWorld() != nullptr) { PhysScene = GetWorld()->GetPhysicsScene(); } if (PhysScene == nullptr) { return; } // Make sure scratch space is big enough. TArray<FAssetWorldBoneTM> WorldBoneTMs; WorldBoneTMs.Reset(); WorldBoneTMs.AddZeroed(GetNumSpaceBases()); FTransform LocalToWorldTM = ComponentToWorld; LocalToWorldTM.RemoveScaling(); TArray<FTransform>& EditableSpaceBases = GetEditableSpaceBases(); struct FBodyTMPair { FBodyInstance* BI; FTransform TM; }; TArray<FBodyTMPair> PendingBodyTMs; #if WITH_PHYSX // Lock the scenes we need (flags set in InitArticulated) if (bHasBodiesInSyncScene) { SCENE_LOCK_READ(PhysScene->GetPhysXScene(PST_Sync)) } if (bHasBodiesInAsyncScene) { SCENE_LOCK_READ(PhysScene->GetPhysXScene(PST_Async)) } #endif // For each bone - see if we need to provide some data for it. for(int32 i=0; i<InRequiredBones.Num(); i++) { int32 BoneIndex = InRequiredBones[i]; // See if this is a physics bone.. int32 BodyIndex = PhysicsAsset ? PhysicsAsset->FindBodyIndex(SkeletalMesh->RefSkeleton.GetBoneName(BoneIndex)) : INDEX_NONE; // need to update back to physX so that physX knows where it was after blending bool bUpdatePhysics = false; FBodyInstance* BodyInstance = NULL; // If so - get its world space matrix and its parents world space matrix and calc relative atom. if(BodyIndex != INDEX_NONE ) { #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST) // tracking down TTP 280421. Remove this if this doesn't happen. if ( !ensure(Bodies.IsValidIndex(BodyIndex)) ) { UE_LOG(LogPhysics, Warning, TEXT("%s(Mesh %s, PhysicsAsset %s)"), *GetName(), *GetNameSafe(SkeletalMesh), *GetNameSafe(PhysicsAsset)); if ( PhysicsAsset ) { UE_LOG(LogPhysics, Warning, TEXT(" - # of BodySetup (%d), # of Bodies (%d), Invalid BodyIndex(%d)"), PhysicsAsset->BodySetup.Num(), Bodies.Num(), BodyIndex); } continue; } #endif BodyInstance = Bodies[BodyIndex]; //if simulated body copy back and blend with animation if(BodyInstance->IsInstanceSimulatingPhysics()) { FTransform PhysTM = BodyInstance->GetUnrealWorldTransform_AssumesLocked(); // Store this world-space transform in cache. WorldBoneTMs[BoneIndex].TM = PhysTM; WorldBoneTMs[BoneIndex].bUpToDate = true; float UsePhysWeight = (bBlendPhysics)? 1.f : BodyInstance->PhysicsBlendWeight; // Find this bones parent matrix. FTransform ParentWorldTM; // if we wan't 'full weight' we just find if(UsePhysWeight > 0.f) { if(BoneIndex == 0) { ParentWorldTM = LocalToWorldTM; } else { // If not root, get parent TM from cache (making sure its up-to-date). int32 ParentIndex = SkeletalMesh->RefSkeleton.GetParentIndex(BoneIndex); UpdateWorldBoneTM(WorldBoneTMs, ParentIndex, this, LocalToWorldTM, TotalScale3D); ParentWorldTM = WorldBoneTMs[ParentIndex].TM; } // Then calc rel TM and convert to atom. FTransform RelTM = PhysTM.GetRelativeTransform(ParentWorldTM); RelTM.RemoveScaling(); FQuat RelRot(RelTM.GetRotation()); FVector RelPos = RecipScale3D * RelTM.GetLocation(); FTransform PhysAtom = FTransform(RelRot, RelPos, InLocalAtoms[BoneIndex].GetScale3D()); // Now blend in this atom. See if we are forcing this bone to always be blended in InLocalAtoms[BoneIndex].Blend( InLocalAtoms[BoneIndex], PhysAtom, UsePhysWeight ); if(BoneIndex == 0) { //We must update RecipScale3D based on the atom scale of the root TotalScale3D *= InLocalAtoms[0].GetScale3D(); RecipScale3D = TotalScale3D.Reciprocal(); } if (UsePhysWeight < 1.f) { bUpdatePhysics = true; } } } } // Update SpaceBases entry for this bone now if( BoneIndex == 0 ) { EditableSpaceBases[0] = InLocalAtoms[0]; } else { const int32 ParentIndex = SkeletalMesh->RefSkeleton.GetParentIndex(BoneIndex); EditableSpaceBases[BoneIndex] = InLocalAtoms[BoneIndex] * EditableSpaceBases[ParentIndex]; /** * Normalize rotations. * We want to remove any loss of precision due to accumulation of error. * i.e. A componentSpace transform is the accumulation of all of its local space parents. The further down the chain, the greater the error. * SpaceBases are used by external systems, we feed this to PhysX, send this to gameplay through bone and socket queries, etc. * So this is a good place to make sure all transforms are normalized. */ EditableSpaceBases[BoneIndex].NormalizeRotation(); } if (bUpdatePhysics && BodyInstance) { //This is extremely inefficient. We need to obtain a write lock which will block other threads from blending //For now I'm juts deferring it to the end of this loop, but in general we need to move it all out of here and do it when the blend task is done FBodyTMPair* BodyTMPair = new (PendingBodyTMs) FBodyTMPair; BodyTMPair->BI = BodyInstance; BodyTMPair->TM = EditableSpaceBases[BoneIndex] * ComponentToWorld; } } #if WITH_PHYSX //See above for read lock instead of write lock // Unlock the scenes if (bHasBodiesInSyncScene) { SCENE_UNLOCK_READ(PhysScene->GetPhysXScene(PST_Sync)) } if (bHasBodiesInAsyncScene) { SCENE_UNLOCK_READ(PhysScene->GetPhysXScene(PST_Async)) } if(PendingBodyTMs.Num()) { //This is extremely inefficient. We need to obtain a write lock which will block other threads from blending //For now I'm juts deferring it to the end of this loop, but in general we need to move it all out of here and do it when the blend task is done if (bHasBodiesInSyncScene) { SCENE_LOCK_WRITE(PhysScene->GetPhysXScene(PST_Sync)) } if (bHasBodiesInAsyncScene) { SCENE_LOCK_WRITE(PhysScene->GetPhysXScene(PST_Async)) } for (const FBodyTMPair& BodyTMPair : PendingBodyTMs) { BodyTMPair.BI->SetBodyTransform(BodyTMPair.TM, ETeleportType::TeleportPhysics); } if (bHasBodiesInSyncScene) { SCENE_UNLOCK_WRITE(PhysScene->GetPhysXScene(PST_Sync)) } if (bHasBodiesInAsyncScene) { SCENE_UNLOCK_WRITE(PhysScene->GetPhysXScene(PST_Async)) } } #endif // Transforms updated, cached local bounds are now out of date. InvalidateCachedBounds(); }
void UDestructibleComponent::CreatePhysicsState() { // to avoid calling PrimitiveComponent, I'm just calling ActorComponent::CreatePhysicsState // @todo lh - fix me based on the discussion with Bryan G UActorComponent::CreatePhysicsState(); bPhysicsStateCreated = true; // What we want to do with BodySetup is simply use it to store a PhysicalMaterial, and possibly some other relevant fields. Set up pointers from the BodyInstance to the BodySetup and this component UBodySetup* BodySetup = GetBodySetup(); BodyInstance.OwnerComponent = this; BodyInstance.BodySetup = BodySetup; BodyInstance.InstanceBodyIndex = 0; #if WITH_APEX if( SkeletalMesh == NULL ) { return; } FPhysScene* PhysScene = World->GetPhysicsScene(); check(PhysScene); if( GApexModuleDestructible == NULL ) { UE_LOG(LogPhysics, Log, TEXT("UDestructibleComponent::CreatePhysicsState(): APEX must be enabled to init UDestructibleComponent physics.") ); return; } if( ApexDestructibleActor != NULL ) { UE_LOG(LogPhysics, Log, TEXT("UDestructibleComponent::CreatePhysicsState(): NxDestructibleActor already created.") ); return; } UDestructibleMesh* TheDestructibleMesh = GetDestructibleMesh(); if( TheDestructibleMesh == NULL || TheDestructibleMesh->ApexDestructibleAsset == NULL) { UE_LOG(LogPhysics, Log, TEXT("UDestructibleComponent::CreatePhysicsState(): No DestructibleMesh or missing ApexDestructibleAsset.") ); return; } int32 ChunkCount = TheDestructibleMesh->ApexDestructibleAsset->getChunkCount(); // Ensure the chunks start off invisible. RefreshBoneTransforms should make them visible. for (int32 ChunkIndex = 0; ChunkIndex < ChunkCount; ++ChunkIndex) { SetChunkVisible(ChunkIndex, false); } #if WITH_EDITOR if (GIsEditor && !World->IsGameWorld()) { // In the editor, only set the 0 chunk to be visible. if (TheDestructibleMesh->ApexDestructibleAsset->getChunkCount() > 0) { SetChunkVisible(0, true); } return; } #endif // WITH_EDITOR // Only create physics in the game if( !World->IsGameWorld() ) { return; } // Set template actor/body/shape properties // Find the PhysicalMaterial we need to apply to the physics bodies. UPhysicalMaterial* PhysMat = BodyInstance.GetSimplePhysicalMaterial(); // Get the default actor descriptor NxParameterized data from the asset NxParameterized::Interface* ActorParams = TheDestructibleMesh->GetDestructibleActorDesc(PhysMat); // Create PhysX transforms from ComponentToWorld const PxMat44 GlobalPose(PxMat33(U2PQuat(ComponentToWorld.GetRotation())), U2PVector(ComponentToWorld.GetTranslation())); const PxVec3 Scale = U2PVector(ComponentToWorld.GetScale3D()); // Set the transform in the actor descriptor verify( NxParameterized::setParamMat44(*ActorParams,"globalPose",GlobalPose) ); verify( NxParameterized::setParamVec3(*ActorParams,"scale",Scale) ); // Set the (initially) dynamic flag in the actor descriptor // See if we are 'static' verify( NxParameterized::setParamBool(*ActorParams,"dynamic", BodyInstance.bSimulatePhysics != false) ); // Set the sleep velocity frame decay constant (was sleepVelocitySmoothingFactor) - a new feature that should help sleeping in large piles verify( NxParameterized::setParamF32(*ActorParams,"sleepVelocityFrameDecayConstant", 20.0f) ); // Set up the shape desc template // Get collision channel and response PxFilterData PQueryFilterData, PSimFilterData; uint8 MoveChannel = GetCollisionObjectType(); FCollisionResponseContainer CollResponse; if(IsCollisionEnabled()) { // Only enable a collision response if collision is enabled CollResponse = GetCollisionResponseToChannels(); LargeChunkCollisionResponse.SetCollisionResponseContainer(CollResponse); SmallChunkCollisionResponse.SetCollisionResponseContainer(CollResponse); SmallChunkCollisionResponse.SetResponse(ECC_Pawn, ECR_Overlap); } else { // now since by default it will all block, if collision is disabled, we need to set to ignore MoveChannel = ECC_WorldStatic; CollResponse.SetAllChannels(ECR_Ignore); LargeChunkCollisionResponse.SetAllChannels(ECR_Ignore); SmallChunkCollisionResponse.SetAllChannels(ECR_Ignore); } const bool bEnableImpactDamage = IsImpactDamageEnabled(TheDestructibleMesh, 0); const bool bEnableContactModification = TheDestructibleMesh->DefaultDestructibleParameters.DamageParameters.bCustomImpactResistance && TheDestructibleMesh->DefaultDestructibleParameters.DamageParameters.ImpactResistance > 0.f; // Passing AssetInstanceID = 0 so we'll have self-collision AActor* Owner = GetOwner(); CreateShapeFilterData(MoveChannel, GetUniqueID(), CollResponse, 0, 0, PQueryFilterData, PSimFilterData, BodyInstance.bUseCCD, bEnableImpactDamage, false, bEnableContactModification); // Build filterData variations for complex and simple PSimFilterData.word3 |= EPDF_SimpleCollision | EPDF_ComplexCollision; PQueryFilterData.word3 |= EPDF_SimpleCollision | EPDF_ComplexCollision; // Set the filterData in the shape descriptor verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.simulationFilterData.word0", PSimFilterData.word0 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.simulationFilterData.word1", PSimFilterData.word1 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.simulationFilterData.word2", PSimFilterData.word2 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.simulationFilterData.word3", PSimFilterData.word3 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.queryFilterData.word0", PQueryFilterData.word0 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.queryFilterData.word1", PQueryFilterData.word1 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.queryFilterData.word2", PQueryFilterData.word2 ) ); verify( NxParameterized::setParamU32(*ActorParams,"p3ShapeDescTemplate.queryFilterData.word3", PQueryFilterData.word3 ) ); // Set the PhysX material in the shape descriptor PxMaterial* PMaterial = PhysMat->GetPhysXMaterial(); verify( NxParameterized::setParamU64(*ActorParams,"p3ShapeDescTemplate.material", (physx::PxU64)PMaterial) ); // Set the rest depth to match the skin width in the shape descriptor const physx::PxCookingParams& CookingParams = GApexSDK->getCookingInterface()->getParams(); verify( NxParameterized::setParamF32(*ActorParams,"p3ShapeDescTemplate.restOffset", -CookingParams.skinWidth) ); // Set the PhysX material in the actor descriptor verify( NxParameterized::setParamBool(*ActorParams,"p3ActorDescTemplate.flags.eDISABLE_GRAVITY",false) ); verify( NxParameterized::setParamBool(*ActorParams,"p3ActorDescTemplate.flags.eVISUALIZATION",true) ); // Set the PxActor's and PxShape's userData fields to this component's body instance verify( NxParameterized::setParamU64(*ActorParams,"p3ActorDescTemplate.userData", 0 ) ); // All shapes created by this DestructibleActor will have the userdata of the owning component. // We need this, as in some cases APEX is moving shapes accross actors ( ex. FormExtended structures ) verify( NxParameterized::setParamU64(*ActorParams,"p3ShapeDescTemplate.userData", (PxU64)&PhysxUserData ) ); // Set up the body desc template in the actor descriptor verify( NxParameterized::setParamF32(*ActorParams,"p3BodyDescTemplate.angularDamping", BodyInstance.AngularDamping ) ); verify( NxParameterized::setParamF32(*ActorParams,"p3BodyDescTemplate.linearDamping", BodyInstance.LinearDamping ) ); const PxTolerancesScale& PScale = GPhysXSDK->getTolerancesScale(); PxF32 SleepEnergyThreshold = 0.00005f*PScale.speed*PScale.speed; // 1/1000 Default, since the speed scale is quite high if (BodyInstance.SleepFamily == ESleepFamily::Sensitive) { SleepEnergyThreshold /= 20.0f; } verify( NxParameterized::setParamF32(*ActorParams,"p3BodyDescTemplate.sleepThreshold", SleepEnergyThreshold) ); // NxParameterized::setParamF32(*ActorParams,"bodyDescTemplate.sleepDamping", SleepDamping ); verify( NxParameterized::setParamF32(*ActorParams,"p3BodyDescTemplate.density", 0.001f*PhysMat->Density) ); // Convert from g/cm^3 to kg/cm^3 // Enable CCD if requested verify( NxParameterized::setParamBool(*ActorParams,"p3BodyDescTemplate.flags.eENABLE_CCD", BodyInstance.bUseCCD != 0) ); // Ask the actor to create chunk events, for more efficient visibility updates verify( NxParameterized::setParamBool(*ActorParams,"createChunkEvents", true) ); // Enable hard sleeping if requested verify( NxParameterized::setParamBool(*ActorParams,"useHardSleeping", bEnableHardSleeping) ); // Destructibles are always dynamic or kinematic, and therefore only go into one of the scenes const uint32 SceneType = BodyInstance.UseAsyncScene(PhysScene) ? PST_Async : PST_Sync; NxApexScene* ApexScene = PhysScene->GetApexScene(SceneType); PxScene* PScene = PhysScene->GetPhysXScene(SceneType); BodyInstance.SceneIndexSync = SceneType == PST_Sync ? PhysScene->PhysXSceneIndex[PST_Sync] : 0; BodyInstance.SceneIndexAsync = SceneType == PST_Async ? PhysScene->PhysXSceneIndex[PST_Async] : 0; check(ApexScene); ChunkInfos.Reset(ChunkCount); ChunkInfos.AddZeroed(ChunkCount); PhysxChunkUserData.Reset(ChunkCount); PhysxChunkUserData.AddZeroed(ChunkCount); // Create an APEX NxDestructibleActor from the Destructible asset and actor descriptor ApexDestructibleActor = static_cast<NxDestructibleActor*>(TheDestructibleMesh->ApexDestructibleAsset->createApexActor(*ActorParams, *ApexScene)); check(ApexDestructibleActor); // Make a backpointer to this component PhysxUserData = FPhysxUserData(this); ApexDestructibleActor->userData = &PhysxUserData; // Cache cooked collision data // BRGTODO : cook in asset ApexDestructibleActor->cacheModuleData(); // BRGTODO : Per-actor LOD setting // ApexDestructibleActor->forcePhysicalLod( DestructibleActor->LOD ); // Start asleep if requested PxRigidDynamic* PRootActor = ApexDestructibleActor->getChunkPhysXActor(0); // Put to sleep or wake up only if the component is physics-simulated if (PRootActor != NULL && BodyInstance.bSimulatePhysics) { SCOPED_SCENE_WRITE_LOCK(PScene); //Question, since apex is defer adding actors do we need to lock? Locking the async scene is expensive! PRootActor->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, !BodyInstance.bEnableGravity); // Sleep/wake up as appropriate if (!BodyInstance.bStartAwake) { ApexDestructibleActor->setChunkPhysXActorAwakeState(0, false); } } UpdateBounds(); #endif // #if WITH_APEX }