void UGripMotionControllerComponent::NotifyDrop_Implementation(const FBPActorGripInformation &NewDrop, bool bSimulate)
{
	if (bIsServer)
	{
		if (NewDrop.Actor)
		{
			NewDrop.Actor->RemoveTickPrerequisiteComponent(this);
			this->IgnoreActorWhenMoving(NewDrop.Actor, false);
			NewDrop.Actor->SetReplicateMovement(NewDrop.bOriginalReplicatesMovement);
		}
		else if (NewDrop.Component)
		{
			NewDrop.Component->RemoveTickPrerequisiteComponent(this);
			
			if (NewDrop.Component->GetOwner())
			{
				this->IgnoreActorWhenMoving(NewDrop.Component->GetOwner(), false);
				NewDrop.Component->GetOwner()->SetReplicateMovement(NewDrop.bOriginalReplicatesMovement);
			}
		}
	}

	DestroyPhysicsHandle(NewDrop);

	UPrimitiveComponent *root = NewDrop.Component;

	if (!root && NewDrop.Actor)
	{
		root = Cast<UPrimitiveComponent>(NewDrop.Actor->GetRootComponent());

		if (root)
		{
			root->IgnoreActorWhenMoving(this->GetOwner(), false);
			root->SetSimulatePhysics(bSimulate);
			root->WakeAllRigidBodies();
			root->SetEnableGravity(true);
		}
	}
	else if (root)
	{
		root->IgnoreActorWhenMoving(this->GetOwner(), false);
		root->SetSimulatePhysics(bSimulate);
		root->WakeAllRigidBodies();
		root->SetEnableGravity(true);
	}
}
void APawn::MoveIgnoreActorRemove(AActor* ActorToIgnore)
{
	UPrimitiveComponent* RootPrimitiveComponent = Cast<UPrimitiveComponent>(GetRootComponent());
	if( RootPrimitiveComponent )
	{
		RootPrimitiveComponent->IgnoreActorWhenMoving(ActorToIgnore, false);
	}
}
bool UGripMotionControllerComponent::GripActor(
	AActor* ActorToGrip, 
	const FTransform &WorldOffset, 
	bool bWorldOffsetIsRelative, 
	FName OptionalSnapToSocketName, 
	TEnumAsByte<EGripCollisionType> GripCollisionType, 
	bool bAllowSetMobility, 
	float GripStiffness, 
	float GripDamping, 
	bool bTurnOffLateUpdateWhenColliding
	)
{
	if (!bIsServer || !ActorToGrip)
	{
		UE_LOG(LogTemp, Warning, TEXT("VRGripMotionController grab function was passed an invalid or already gripped actor"));
		return false;
	}

	UPrimitiveComponent *root = Cast<UPrimitiveComponent>(ActorToGrip->GetRootComponent());

	if (!root)
	{
		UE_LOG(LogTemp, Warning, TEXT("VRGripMotionController tried to grip an actor without a UPrimitiveComponent Root"));
		return false; // Need a primitive root
	}

	// Has to be movable to work
	if (root->Mobility != EComponentMobility::Movable)
	{
		if (bAllowSetMobility)
			root->SetMobility(EComponentMobility::Movable);
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("VRGripMotionController tried to grip an actor set to static mobility and bAllowSetMobility is false"));
			return false; // It is not movable, can't grip it
		}
	}

	root->IgnoreActorWhenMoving(this->GetOwner(), true);
	// So that events caused by sweep and the like will trigger correctly
	ActorToGrip->AddTickPrerequisiteComponent(this);

	FBPActorGripInformation newActorGrip;
	newActorGrip.GripCollisionType = GripCollisionType;
	newActorGrip.Actor = ActorToGrip;
	newActorGrip.bOriginalReplicatesMovement = ActorToGrip->bReplicateMovement;
	newActorGrip.Stiffness = GripStiffness;
	newActorGrip.Damping = GripDamping;
	newActorGrip.bTurnOffLateUpdateWhenColliding = bTurnOffLateUpdateWhenColliding;

	if (OptionalSnapToSocketName.IsValid() && root->DoesSocketExist(OptionalSnapToSocketName))
	{
		// I inverse it so that laying out the sockets makes sense
		FTransform sockTrans = root->GetSocketTransform(OptionalSnapToSocketName, ERelativeTransformSpace::RTS_Component);
		newActorGrip.RelativeTransform = sockTrans.Inverse();
		newActorGrip.RelativeTransform.SetScale3D(ActorToGrip->GetActorScale3D());
	}
	else if (bWorldOffsetIsRelative)
		newActorGrip.RelativeTransform = WorldOffset;
	else
		newActorGrip.RelativeTransform = WorldOffset.GetRelativeTransform(this->GetComponentTransform());
	
	NotifyGrip(newActorGrip);
	GrippedActors.Add(newActorGrip);

	return true;
}