Hello, I’m struggling to pickup and put down objects in VR because I don’t understand the physics simulation design.
How do I turn physics on/off for an Actor?
Is there a single API call for the Actor?
I can DropActor->GetRootComponent()
, but it doesn’t have SetSimulatePhysics()
. Do I have to turn off physics for all child components simulating as well?
In this code, I check for a “FoundGripComponent” - a special component for props like tools and whatnot.
When I test this code picking up an actor w/o that component (GripFoundActor && !GripFoundComponent), I can pickup and drop the actor once - but I cannot pick up the actor again (BlueprintActor->DefaultSceneRoot->StaticMesh)
void ARealityMotionController::GripBegin()
{
// Grab previously identified item.
WantsToGrip = true;
if (GripFoundComponent != nullptr)
{
UKismetSystemLibrary::PrintString(this, TEXT("MotionController->GripBegin PROP"));
// This is an interesting PROP,
GrippedActor = GripFoundActor;
GrippedGripComponent = GripFoundComponent;
// GripComponent will handle physics and attach the GripComponent properly
GrippedGripComponent->ReceiveGripBegin(this, HandMesh, FName("GRIP_ATTACHMENT")); // GripComponent will handle physics and attachment.
}
else if (GripFoundActor != nullptr)
{
UKismetSystemLibrary::PrintString(this, TEXT("MotionController->GripBegin DECOR"));
// This is random DECOR, and we should hold onto it till the player gets bored.
GrippedActor = GripFoundActor;
// Turn off physics
TArray<UPrimitiveComponent*> AllComponents;
GrippedActor->GetComponents(AllComponents, true);
for (UPrimitiveComponent* MyComp : AllComponents)
{
MyComp->SetSimulatePhysics(false);
}
// Attach to Motion Controller
GrippedActor->GetRootComponent()->AttachToComponent(HandMesh, FAttachmentTransformRules::SnapToTargetNotIncludingScale, FName("GRIP_ATTACHMENT"));
//GrippedActor->AttachToActor(this, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
else
{
UKismetSystemLibrary::PrintString(this, TEXT("MotionController->GripBegin NOTHING"));
}
}
void ARealityMotionController::GripEnd()
{
// Drop previously grabbed item
if (GrippedGripComponent != nullptr)
{
UKismetSystemLibrary::PrintString(this, TEXT("MotionController->GripEnd PROP"));
// GripComponent knows how to detach and simulate physics.
GrippedGripComponent->ReceiveGripEnd(this);
}
else if (GrippedActor != nullptr)
{
UKismetSystemLibrary::PrintString(this, TEXT("MotionController->GripEnd DECOR"));
// Get list of all components
TArray<UPrimitiveComponent*> AllComponents;
GrippedActor->GetComponents(AllComponents, true);
// Enable physics
for (UPrimitiveComponent* MyComp : AllComponents)
{
// "Beginning simulation will detach it" https://docs.unrealengine.com/en-US/API/Runtime/Engine/Components/UPrimitiveComponent/SetSimulatePhysics/index.html
MyComp->SetSimulatePhysics(true);
}
}
GrippedGripComponent = nullptr;
GrippedActor = nullptr;
WantsToGrip = false;
}
Thanks!