Download

How to enable Physics for a list of actors?

I have a list of actors:


TArray<AActor*> movableActors; 

And inside the body of a for loop I am disabling the physics for these actors:


 movableActors*->DisableComponentsSimulatePhysics(); 

How can I now enable physics again for an actor at a given index of this particular list of actors?

Why is it not as simple as EnableComponentsSimulatePhysics()?

Bump. Really would appreciate some help on this. If anything, I just need to access the static mesh component of each actor in the list and enable / disable physics for that component.

Short answer:
If you are stuck looking at the code will often give you some clues.



void AActor::DisableComponentsSimulatePhysics()
{
	TInlineComponentArray<UPrimitiveComponent*> Components;
	GetComponents(Components);

	for (UPrimitiveComponent* Component : Components)
	{
		Component->SetSimulatePhysics(false);
	}
}


Long Answer:
Usually there is only one mesh associated with an actor that can be found with AActor->GetRootPrimitiveComponent() which can be null.

DisableComponentsSimulatePhysics() Sets the physics body component response from simulated (respond to gravity and collisions with other simulated objects) to kinematic (only move when you move them).
There is a performance hit involved in doing this (not something you want to do every frame).

What is it that you are actually trying to do?

Other options you might want to look at include putting physics objects to sleep until something else hits them:
MeshComponent->IsAnyRigidBodyAwake(), MeshComponent->WakeRigidBody(), MeshComponent->PutRigidBodyToSleep() etc.

Turning off collisions:
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly), MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics) etc.

Or something more advanced with custom collision response channels.

Hope this helps…

Thank you very much for the help.

I went with the simple solution which you mentioned.

I’m simply getting access to the root component which I’m making sure is always a static mesh, and then setting the physics state from there:



movableActors*->GetRootPrimitiveComponent()->SetSimulatePhysics(false);