Download

[4.9.2] Given two vectors, how to rotate one vector around the other?

I tried RotateAngleAxis(), but after it kept returning -2,000,000,000 for the resulting Vectors X and Y, I double check and realized it wants a normal for the axis.

How do I rotate a location vector around ANOTHER location vector, by a specified angle?

RotateAngleAxis is the right method. If you make a copy of the vector you are trying to rotate around and simply normalize it to get the direction, it should be fine.

It’s unclear from your question whether the other vector is actually a rotation axis, or another vector in the plane of rotation.
What does “around the other” really mean? Do you have two points, and one point is the “center”?

If the other vector is an axis, then just normalize it, and plug it in as the axis in RotateAngleAxis.
If the other vector is in the plane of rotation, then cross the two vectors and normalize to get the axis of rotation. You will want to verify that the magnitude of the cross is not very close to zero (as in, the two vectors are nearly co-linear) and if that happens, use some default axis vector (or not rotate.)

However, when you say “location vector” it sounds like you’re using “points.” While you can “rotate a point around a point,” that means subtracting the center point from the point, then rotating around your rotation vector (up?), then adding back the location of the center point. This is different from what’s traditionally meant by “rotating a vector around a vector.”

Perhaps what you want is:

(Point - Center) -> RotateAroundAxis(Up, Amount) -> (+ Center)

I have a vehicle that has several weapon turret actors attached.
I am trying to represent those weapon turret actors onto a HUD Menu that is supposed to display a top down of the vehicle, and icons representing its turrets at the turret positions.
The problem is that the turret position icons(the whole group of them not each individual icon) turn to match the vehicles facing. I need the group to always be oriented towards the top of the screen.

jwatte’s last suggestion has lead me to getting something that half works:

FVector WorldNorth = FVector(1,0,0);
FVector CompDIR = (Center - CompPos).GetSafeNormal();
float OffsetAngle = FMath::RadiansToDegrees(acosf(FVector:: DotProduct(CompDIR, WorldNorth)));

FVector Up = FVector(0,0,1);
OutVector = (CompPos - Center).RotateAngleAxis(OffsetAngle, Up);
OutVector += Center;

The resulting OffsetAngle is always positive, climbs to 180 as the vehicle turns south, then falls back to 0 when turning to face north again. Regardless of east or west facing.
The turret icon, will follow the rotation of the vehicle when it is turning east, but remain fixed to the top of the screen(this is what I want!) when the vehicle is facing west.
Now I just need to get it to do that for the east side as well…

EDIT:
Got it.
I just added a check to see what side of the ship WorldNorth was on and then multiplied the OffsetAngle by -1 or 1 depending on the result.

EDIT2:
BUUUT… it only works for the one turret that is dead forward and center of the vehicles location.
The icons for turrets that are forward and left or right are pushed to the center, while anything behind is still following the vehicle’s rotation.
:frowning:

If you could provide an image with what you are seeing vs what you want, that could help quite a bit.

Actually, I solved it.
And with a much easier and much “cleaner” solution:

TurretTransform.InverseTransformPosition(VehicleLocation);

It required leveling out the rotation and .Z values of the Transform, but it works perfectly now.
Thank you to everyone for helping me, I appreciate it.

The rotate vector around axis node lets a celestial object rotate around the sky axis just fine, too. So this was a great help. Thank you very much.