How do you rotate a Spatial node around an axis at a given point in space?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By path9263

I would like to rotate a spatial node around a given point in 3D space not just around it’s origin point. I only need to rotate on one axis at a time. Adding the node to a parent and rotating the parent will not work well for me as this has to be fairly dynamic with the rotation point’s relation to the spatial node changing all the time.

Something like this function that Unity has.

Using Transforms I believe is the way to do this but I can’t quite wrap my head around it.

Thank you for any help you can offer!!

:bust_in_silhouette: Reply From: path9263

Well for anyone else trying to do this here is what I came up with.

obj is the Spatial object you want to rotate.
point is a vector3, the point in world space you want to rotate around.
axis is the axis you want to rotate around, ie y axis would be Vector3(0, 1, 0)
angle is the angle (in radians) you want to set the objects rotation to, not the amount to rotate it by.

func rotateAround(obj, point, axis, angle):
	var rot = angle + obj.rotation.y 
	var tStart = point
	obj.global_translate (-tStart)
	obj.transform = obj.transform.rotated(axis, -rot)
	obj.global_translate (tStart)

Note that if you want to rotate around a different axis other than Y you will also have to change obj.rotation.y to a different axis, this could be modified to support any axis but this is all I need for now.