How do I get relative angle of rotation for physical object?

Hi - Just a little background so you can understand what I am doing.

I am creating a game that has several cubes fall to the floor. All the cubes have physical bodies attached to them. As the cubes fall they may be rotated in either direction due to a couple of moving obstacles. Eventually each of the cubes come to rest on the floor. Once they have come to rest (that brings up another question - how do I know when a physical object has come to rest?). Here is what I need …

I need to get the relative (user’s point of view) rotation of a cube when I “click” on it (I got the touch event). For example:

If a cube rotates 90 degrees CW (clock-wise) then I should get 90 degrees

If a cube rotates 180 degrees CW (clock-wise) then I should get 180 degrees

If a cube rotates 270 degrees CW (clock-wise) then I should get 270 degrees

If a cube rotates 360 degrees CW (clock-wise) then I should get 0 degrees!!

If a cube rotates -90 degrees CCW (counter clock-wise) then I should get -90 degrees!!

If a cube rotates -360 degrees CCW (counter clock-wise) then I should get 0 degrees!!

If a cube rotates 540 degrees CW (clock-wise) then I should get 180 degrees!!

See, the angle of rotation should be relative to the viewers point of view. I can handle the rotation of the device by reversing the rotation value.

Thank you in advance - Looking forward to hearing your solutions :slight_smile:

If you just want to normalize the cube’s rotation to an angle between 0 and 360, just do angle % 360.  (The % symbol is the modulo operator in Lua.)

This covers all the cases in your post except the fifth case.  In the fifth case, you suggested that a -90 CCW rotation should return -90.  If you’re comfortable with it returning 270 (which is equivalent), then angle % 360 should be all you need.

  • Andrew

If you just want to normalize the cube’s rotation to an angle between 0 and 360, just do angle % 360.  (The % symbol is the modulo operator in Lua.)

This covers all the cases in your post except the fifth case.  In the fifth case, you suggested that a -90 CCW rotation should return -90.  If you’re comfortable with it returning 270 (which is equivalent), then angle % 360 should be all you need.

  • Andrew