Calculating Rotation

Hello,

I am trying to figure out an efficient way to rotate an actor relative to another. I am sure there is something I can use in FMath, maybe a quaternion or something, but I’m not quite sure what.

What I want to accomplish is making an actor rotate away from another actor in X and Y, leaving Z (yaw) as it is. I know I can use LookAtRotation to figure out the first step, but I have no idea how to solve the rest.

This picture explains the rotational direction I’m describing, away from the other actor:

Thank you for the reply. I’ll try to explain it better.

What I want to do is make the rotated actor “lie down” before the other actor, partially as in this picture or all the way. My intended use of it is to make foliage become trampled when moved over.

From this original rotation:

What exactly are you trying to achieve? I don’t quite get what “rotate away from another actor” means. Do you want this parallelepiped to rotate around the character and rotate along X and Y? Could you, please, show or explain the end result

I’ll explain what to do on this example:

250074-answer-4.png

Let’s say that the sphere is the character, the cube is the pipe. We need to rotate the cube relatively to the sphere.

I prototyped it in BP, but it can be easily converted to C++. This is how it looks altogether:

Now let’s look at the first to sections:

Firstly, I cached the transforms just for conveniency, then I calculated the direction vector (FVector in the BP) from the sphere to the cube (for some reason I wrote it in the comments as ‘cylinder’) but without Z-value. I was not sure if you needed this, but I assumed that the character and the pipes could be on different heights and in this case the FVector would not be parallel to the surface. If you actually need Z-value, just omit the projection part.

Now that we have the FVector, let’s move on to the rotation calculation:

Here I rotated the FVector around the axis which is orthogonal to the FVector (remember, that the FVector is projected to the XY plane and there is only one orthogonal axis) by the desired angle, normalized the result and set it as the world rotation of the cube. This is the end result:

250080-answer-5.png

Here the blue line is the FVector, the red line is the orthogonal axis and the green line is the result vector.

That is a very extensive answer! Thank you very much.

Now the only issue is that this will turn the cube in Z before lying it down in Y. This results in a rather strange effect when used on foliage which is meant to be rooted and unable to yaw.

The blue line denotes the undesired rotation. The purple line are the combined X and Y axes I wish to use.

250116-rot04.png

Terribly sorry to complicate things further, heh.