PhysicsHandleComponent, GrabbedComponent and rotation

Hello!
In the crypt tutorial, I would like to add a feature to the Grabber.
I want to enable the ability to rotate the object we have grabbed using the q and e keys.
To achieve this, I added key handling for a and e in the character, and I call a function in the grabber to set a rotation offset. In the TickComponent, I add the rotation to the PhysicsHandle.
However, the rotation center is currently at the attachment point. I want the object to rotate around itself and not at the point where the player grabbed it. I tried using SetWorldRotation directly on the object with PhysicsHandle->GetGrabbedComponent()->SetWorldRotation. Depending on the order and how I handle it, I experienced two different behaviors. Either the object stays in place while I rotate it, so if I move the mouse, it no longer follows the view and only moves when I stop the rotation. Or the object moves as if it has Parkinson’s, in a very strange way. It’s only when I press the keys to rotate that it seems to calm down, only to eventually go back to behaving very strangely.
Have you ever tried something similar, and if so, do you know what might be causing this behavior and why?

// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
	if (PhysicsHandle == nullptr)
	{
		return;
	}

	if(HasGrabAnObject())
	{
		FVector TargetLocation = GetComponentLocation() + GetForwardVector() * HoldDistance;

		FRotator ObjectRotation(0, GetComponentRotation().Yaw + ObjectRotationOffset, 0);
		// UE_LOG(LogTemp, Warning, TEXT("Add objectRotationOffset off : %f"), ObjectRotationOffset);
		//PhysicsHandle->GetGrabbedComponent()->SetWorldLocation(TargetLocation, true);
		//PhysicsHandle->GetGrabbedComponent()->SetWorldRotation(ObjectRotation);
		PhysicsHandle->SetTargetLocationAndRotation(TargetLocation, ObjectRotation);
		UE_LOG(LogTemp, Warning, TEXT("Move Target Object"));
		//PhysicsHandle->SetTargetLocation(TargetLocation);
		//PhysicsHandle->GetGrabbedComponent()->SetWorldRotation(ObjectRotation);
	}
}

void UGrabber::SetObjectRotationOffset(float NewOffset)
{
	UPhysicsHandleComponent* PhysicsHandle = GetPhysicsHandle();
	if (PhysicsHandle == nullptr)
	{
		return;
	}

	if (HasGrabAnObject())
	{
		ObjectRotationOffset = ObjectRotationOffset + NewOffset * RotationAmount * UGameplayStatics::GetWorldDeltaSeconds(GetWorld());
		UE_LOG(LogTemp, Warning, TEXT("New offset: %f"), ObjectRotationOffset);
		//FRotator ObjectRotation(0, GetComponentRotation().Yaw + ObjectRotationOffset, 0);
		//PhysicsHandle->GetGrabbedComponent()->SetWorldRotation(ObjectRotation);
	}
}

I’ll implement this myself to get a better understanding and get back to you.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms