Announcement

Collapse
No announcement yet.

Simulate Physics breaks parenting.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Simulate Physics breaks parenting.

    Hello,

    Sorry if this is super simple, but I'm pretty new to UE4 and trying to stumble my way through things. I've been working with VR and parenting objects to the players hand so they can grab and release certain objects in the world. I'm basically trying to mimic Unreal's BP_PickupCube functionality, but via C++ instead of blue prints.

    What seems to be happening is if the object I'm trying to pick up has ever had physics simulated on it, it no longer allows me to "Pick it up" It appears the parenting is still working, but when I move my hand around the object stays on the ground in the same spot.

    I noticed that I wasn't the only one to encounter this issue, but I found a resolution in the BP section of the forum (Here: Includes video of problem), and I don't fully understand how the solution is different from what I'm doing.

    So when I reach out into the world and overlap an object if I push the grab button I call this function on my "MovableObject":

    Code:
    void UMovableObject::Grab(USceneComponent* hand)
    {
    	GetOwner()->GetComponents(mPhysicsComponents);
    	if (mPhysicsComponents.Num() != 0)
    	{
    		for (UStaticMeshComponent* Component : mPhysicsComponents)
    		{
    			Component->SetSimulatePhysics(false);
    		}
    	}
    
    	GetOwner()->AttachToComponent(hand,FAttachmentTransformRules::KeepWorldTransform, NAME_None);
    	
    }
    When I release the object I'm calling this function:

    Code:
    void UMovableObject::Drop()
    {
    	// Release all buttons. 
    	for (int i = 0; i < 3; i++)
    	{
    		mButtonPressed[i] = false; 
    	}
    	// Re-enable all the physics we turned off when we grabbed it. 
    	if (mPhysicsComponents.Num() != 0)
    	{
    		for (UStaticMeshComponent* Component : mPhysicsComponents)
    		{
    			Component->SetSimulatePhysics(true);
    		}
    	}
    	GetOwner()->DetachFromActor(FDetachmentTransformRules::KeepWorldTransform);
    }
    If I start the game with SimulatePhysics unchecked then when I grab it the first time everything works as expected. However when I release the object (and enable physics) I can never pick it up again. Also if I start the game with SimulatePhysics checked then I can never move the object.

    #2
    When a component is simulating, it can't have any parent, since it doesn't make sense for it to inherit a transform when it is moving under physics forces.
    So when you re-enable physics, it will be detached meaning further changes to the parent actor (or rather its root component) will not be inherited.

    You would need to either manually re-attach components in your Grab function, or use a physics handle component to pick up simulating objects instead.

    Comment

    Working...
    X