How to calculate the rotation speed of a 2d object

Godot Version



In a 2d scene and through code, how can I calculate the speed at which an object is rotating? I have a formula in my code but it works very strangely. The formula is in the physics process function:

rotation_speed = (plrcontainer.rotation - previous rotation)/delta

previous_rotation = plrcontainer.rotation

The problem with this formula is that when the object has rotated too far, the value becomes negative. It will go from 13 to -364 instantly at a certain point. Other than that it works just fine.

I should’ve described what I’m actually doing. I’m making a top down sword game and basically if you hold down middle click you hold the sword outward and the character looks toward the mouse. I wanna make the damage change based on rotation speed. I also wanna make it so if you try to change the direction of the swing it’ll end the swing with an attack, just to kind of balance out the attack. Issue I’m having is that my formula relies on comparing current rotation to previous rotation, which will go from 4 to -359 at certain angles if you change the direction

Calculating the speed of rotation can be complicated because you don’t know if the object has rotated once or twice.

But from my point of view, if you’re driving the object yourself, then the velocity is actually known and doesn’t need to be calculated.

If the object is driven by a physical system, then you can get the velocity from the

var speed =  rigid_body.angular_velocity
1 Like

Thanks for your response, I tried it out but control bodies don’t have angular velocity. I tried making a sort of rotation meter using a rigid body that was parented to it, but it worked weirdly and caused collision issues, and the velocity didn’t reach 0 when the player object stopped rotating

What is the code where you rotate the node?
If you have some set amount of radians/degrees rotated each phys/frame then it would be something like:

rate = amount * (1 / delta)

which would be measured in rads/degrees per second

1 Like

It rotates based on mouse position

Here is some pseudocode (non-tested) I came up with, hopefully this gives you what you seek

var previous_rotation = 0
var rotation_speed = 0

func _physics_process(delta):
    var frame_rotation = plrcontainer.rotation
    if frame_rotation < 0:
        frame_rotation = frame_rotation + TAU
    rotation_speed = (frame_rotation - previous_rotation) * (1.0/delta)
    print("Speed: ", rotation_speed, " rads/s")
    previous_rotation = frame_rotation
1 Like

I tried it out and unfortunately it doesn’t work. The speed will often stay in the 370s range even if it’s staying still, and only resets to 0 when i rotate it back toward the rotation it was at right before i started rotating. Thank you for trying though, i did look up what TAU is (I never learned about it in math class back in hs lol) and maybe I’ll figure something out to do with TAU or PI

Have you tried angle_difference(previous rotation, plrcontainer.rotation) instead of plrcontainer.rotation - previous rotation? That will normalize the result to the shorter direction and should keep it from drastic flips.


I didn’t even know that function existed. I just tried it out and it’s what I needed. Thank you a lot