Use AddRadialImpulseToBody or something to upside / down rotation ?

Hi, please help me to understand the rotation of physical objects related to Physical Constraint.

There are two kinds of objects:

  1. A standard door (microwave door), as you can see in the video, it works perfectly and rotates via AddRadialImpulseToBody.
  2. A door that opens from top to bottom, rotating it via AddRadialImpulseToBody function doesn’t work, so considering that the direction of movement is different, I try to do it via AddAngularImpulseInRadians function, but it works very strange.

I tried to video all the details and differences in operation, I will be glad for any help.

All the best!

Here is my code

HDoor (works nice)

void UVRGrabHDoorComponent::TickComponent(float DeltaTime, ELevelTick TickType,
                                          FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (IsGrabbed())
	{
		const FRotator LookAndRotation = UKismetMathLibrary::FindLookAtRotation(
			GetComponentLocation(), MotionControllerComponent->GetComponentLocation());

		const float ImpulseStrength = UKismetMathLibrary::NormalizeAxis(
			LookAndRotation.Vector().Dot(GetRightVector()));

		const UStaticMeshComponent* MeshComponent = GetAttachParentActor()->GetComponentByClass<UStaticMeshComponent>();

		MeshComponent->GetBodyInstance()->AddRadialImpulseToBody(GetComponentLocation(), 20.0f,
		                                                         ImpulseStrength * ImpulseForce,
		                                                         RIF_Constant, false);
	}
}

VDoor (works wrong)

void UVRGrabVDoorComponent::TickComponent(float DeltaTime, ELevelTick TickType,
                                          FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (IsGrabbed())
	{
		const FRotator LookAndRotation = UKismetMathLibrary::FindLookAtRotation(
			GetComponentLocation(), MotionControllerComponent->GetComponentLocation());

		const float ImpulseStrength = UKismetMathLibrary::NormalizeAxis(
			LookAndRotation.Vector().Dot(GetRightVector()));

		const UStaticMeshComponent* MeshComponent = GetAttachParentActor()->GetComponentByClass<UStaticMeshComponent>();

		MeshComponent->GetBodyInstance()->AddAngularImpulseInRadians(FVector(0, -ImpulseStrength * 2000.0f, 0),
		                                                             false);
	}
}

Anyone?

I did it.

Because UStaticMeshComponent was declared as a static variable, Autocomplete did not offer me a transformation function like AddRelativeRotation, which I used to get the desired result.

BUT! Now the PhysicConstraints rotation limits don’t work :frowning:

I was able to do this using the SetAngularVelocityInRadians function, and turning off the gravity in advance makes it work like a charm!

Here is a code
void UVRGrabVDoorComponent::TickComponent(float DeltaTime, ELevelTick TickType,
                                          FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (IsGrabbed())
	{
		const FRotator LookAndRotation = UKismetMathLibrary::FindLookAtRotation(
			GetComponentLocation(), MotionControllerComponent->GetComponentLocation());

		float ImpulseStrength = UKismetMathLibrary::NormalizeAxis(
			LookAndRotation.Vector().Dot(GetRightVector()));

		ImpulseStrength *= ImpulseForce;

		const UStaticMeshComponent* MeshComponent = GetAttachParentActor()->GetComponentByClass<UStaticMeshComponent>();

		UE_LOG(LogTemp, Log, TEXT("STRENGTH %f NAME %s"), ImpulseStrength, *MeshComponent->GetName());

		MeshComponent->GetBodyInstance()->SetAngularVelocityInRadians(
			FVector(0.0f, -ImpulseStrength, 0.0f),
			false, true);
	}
}

void UVRGrabVDoorComponent::OnGrabbed()
{
	UStaticMeshComponent* MeshComponent = GetAttachParentActor()->GetComponentByClass<UStaticMeshComponent>();
	MeshComponent->SetEnableGravity(false);
}

void UVRGrabVDoorComponent::OnDropped()
{
	UStaticMeshComponent* MeshComponent = GetAttachParentActor()->GetComponentByClass<UStaticMeshComponent>();
	MeshComponent->SetEnableGravity(true);
	MeshComponent->GetBodyInstance()->ClearForces();
}

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