Need help with Delta Rotation

Hi,

so I’m calling the AddLocalRotation-node on tick in order to rotate a space ship to its target.

The GetDeltaRotator looks like this:

This way I get for each axis 0, 1 or -1.
I want the space ship to turn to the target like turn-maneuvers. This looks somehow-okayish. But at some point the ship takes the wrong direction and just flies away from the target instead chasing it. Debugging the values shows that some axis gets flipped values out of nothing, probably gimbal lock, I guess. How can I get rid of that? Do I need quaternions to convert the rotation to quats and than back to rotation? I’m pretty stuck here…

(I haven’t tested it out in UE5 yet though. Last time I worked with the UE5 was a couple of month ago and I couldn’t find any new quat-nodes there inside BPs. Don’t know if it has changed. I’m still working with the UE4 since my Computer is old).

1 Like

If you’re ok with C++, you can create this node that I made to solve this exact issue, since UE did not have one out of the box, essentially it has the option to use ShortestPath and for that it uses Quaternions instead of Rotators.

.h

// Like the editor one, but with Shortest Path option.
UFUNCTION(BlueprintPure, Category = Benerot Utilities|Math|Rotator)
static FRotator RInterpToConstant(FRotator Current, FRotator Target, float DeltaTime, float InterpSpeed, bool bShortestPath);|

.cpp

FRotator UFunctionLibrary::RInterpToConstant(FRotator Current, FRotator Target, float DeltaTime, float InterpSpeed, bool bShortestPath)
{
	// if shortest path, we use Quaternion to interpolate instead of using FRotator
	if (bShortestPath)
	{
		FQuat AQuat(Current);
		FQuat BQuat(Target);

		FQuat Result = FMath::QInterpConstantTo(AQuat, BQuat, DeltaTime, InterpSpeed);

		return Result.Rotator();
	}

	return FMath::RInterpConstantTo(Current, Target, DeltaTime, InterpSpeed);
}

Screenshot 2022-08-14 161721

Then you simple get your Current and Target rotations, set Shortest Path to true, and let quaternion math do the job for you.
You’d use this with a SetRotation on Tick.

Hope it helps.

1 Like

@Evigmae
Hi, thanks for your post. But when I need SetActorRotation for this, will the ship do turn maneuvers like fighter jets do or will it rotate directly to the target?

You can control the sharpness of the turn with the InterpSpeed value. A low value is a wide turn, a high value is a sharp turn.
Example of implementation:

The value is how many degrees it changes per frame.

It looks good but now the ship almost never faces the target, meaning that he can’t really aim the target. He is just rotating around the target. Am I doing it correctly?

So I need him to do this with the red circle being the target:
turntotarget

Does your code work in UE5 aswell?

Yeah, it does work in UE5.

2 for the Interpolation Speed is a lot, you’re making it rotate 2 degrees per frame (or 120 degrees per second at 60 fps) So with 2 is going to rotate quite fast.
But also keep in mind that as it moves the target rotator will change, and the 2 might cancel each other a bit.

And for that you might want to add an extra step to change the rotation rate by the distance between the two.

1 Like

@Evigmae
Thank you very much! I still need to find the perfect numbers and I think it’s not a good idea to have Find Look At-Rotation for the Target-Rotation, but the ship AI is turning to the target much better now. But he is still starting to rotate around the target like a fly instead of turning to an aiming-angle. First I though I have it solved but I haven’t… :confused: Maybe it’s because of the target-rotation. But this only happens when the target stands still, which is still not desirable.
I marked your post as solution since it does help me indeed. I think I just need to find the right parameters.

There is also one thing that’s unclear. How do I code an aileron roll? I posted it here and I haven’t found a solution yet.

I want him to pitch and yaw around but also rolling independently at the same time. Using the AddActorRotation-node right after the Set-node is not doing much.

You can rotate the individual components independently of the actor.
So while you rotate the actor with a SetActorRotation in world space, you can also use the SetRelativeRotation or AddRelativeRotation to rotate the component in local space.

Screenshot 2022-08-16 221933

Just keep in mind that component you’re rotating in local space needs to not be the root component.

Rotating the root component in local space is just world space with extra steps xP

I tried that out and works perfect, thank you again. So it works like I was presuming in my last reply in the post that I linked.

As for this divergencish behavior, the ship at some point changes its path like this:

I count how long it circles like the right path and when a threshold is reached I rotate the ship AI manually to its target.

1 Like