How do I count rotations


I got two question:

First, I want to count the rotation of an object in degrees. Not only from 0 to 360 but also keep counting after a full rotation was done, so add up the rotation (540, 720, 900, 1080 etc. with every step inbetween)

How can I do this?
To get the z-rotation I have the follwing script:

		angleXYZ = self.object.worldOrientation.to_euler()
		angleZ = math.degrees(angleXYZ[2])+180

With this conversion the angle goes from -180 to 180. So I add 180 to have it range from 0-360.
Another problem I got with this is that the player is always starting at angle 180 instead of 0/360.
Any idea how to fix this?

Thanks for any help in advance!!

What is driving the rotation?

If it’s a script, then you can just store the amount you’re rotating the object by in a property.

Or, maybe use a animation instead which extrudes to infinity. Simply keep moving it along the frames and keep track of what frame you’re on.

Unfortunately, the rotation is impulsed by torque force

If it never rotated more than 180 degrees in one frame, then you could keep track of the change in rotation every frame. If it goes from +180 to -180 then add (or remove, depending on direction) 360 to your count.

Thanks for the ideas…
I had this approach before. The problem is that the object is a dynamic object which has some slight variations in its rotation due to its physics.
So it will rotate slightly a bit forwards and backwards naturally resulting in this adding up to the “rotation counter” and giving unprecise results.

I was thinking about adding 4 objects around the main object and everytime one of them is detected via a ray or something, it would add up 90° to the rotation counter.
But this would limit it to 90° steps

Presumably you’d only need to know how many full rotations it’s done. That would make your calculation something like: (num_full_rotations * 360) + current_rotation

That way, it doesn’t matter if your object rocks back and forward, as long as you’re accurately tracking when it goes under -180 or over 180.

If last frame it was on 160, and now it’s on -155, then the closest path to that assumes it went over 180 and rolled over to -180, thus we’d add 1 to the num_full_rotations.

1 Like

That makes sense! Thanks a lot :slight_smile: