+1 vote

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!!

in Engine by (121 points)

1 Answer

+1 vote
Best answer

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.

by (121 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories