How to rotate around an arbitrary point?

If you already know the distance and the rotation, you can do something like this:

This function will return the final location for the mesh. Also, you should set the mesh’s rotation to the same rotation used in this function.

2 Likes

How can I rotate a mesh around an arbitrary point in space?

For example a moon rotating around a planet.

Thanks

1 Like

Perfect. Thanks!!

Would be possible to create a blueprint function that generates an ellipse movement around an arbitrary point in space. A function that we could input the center point position + the two ellipse focus points positions (that will define the ellipse shape). Is there a way to do it?

Here is my solution to it. It will rotate the object around the given Pivot using the objects own rotation. E.g. A delta rotation around Z will rotate around the objects Z axis. Optionally you can retain the rotation of the object itself.
You can write this as a function, for sure.

	UFUNCTION(BlueprintPure)
		static void RotateAroundPivot(const FTransform& Original, const FVector PivotLocation, const FRotator DeltaRotation, bool bRetainOrientation, FTransform& New)
	{
		FTransform PivotTransform = FTransform(Original.GetRotation(), PivotLocation);
		FTransform DeltaPivotToOriginal = Original * PivotTransform.Inverse();
		New = FTransform(DeltaRotation.GetInverse() * (float)bRetainOrientation, FVector(0,0,0)) * DeltaPivotToOriginal * FTransform(DeltaRotation, FVector(0,0,0)) * PivotTransform;
	}
8 Likes

This works regardless of the initial orientation as it doesn’t require the forward vector and has solved my issue of rotating a parent actor around a child actor. I’ve been struggling with this for hours. Thank you so much!

(post deleted by author)

I found a good way to do this was to do something like this:


    FVector pointLocation = ...
    FVector thingYouRotatePosition = ...
    float amountYouWantToRotate = ... //(0-2pi)

    // make a Quaternion with an upward axis (rotating around the upwards axis)
    FQuat rotation = FQuat(FVector::UpVector, amountYouWantToRotate);

    // get the offset of you the thing you are rotating, from the point
    FVector offset = pointLocation - thingYouRotatePosition;

    // rotate it and add that to the point location
    FVector newRotation = rotation.RotateVector(offset) + pointLocation;

Quaternions are really handy once you learn how to use them. Though most of the explanations I’ve seen seem a little complicated.

How they work is that you give them a vector that tells it what to rotate around, and then you tell it how much to rotate around that ‘axis’. So if you give it a vector that points straight up, it’ll rotate around that like I did above.