#Get closest turning to given angle

14 messages · Page 1 of 1 (latest)

sacred finch
#

My top down spaceship is given with vector2 which shows the way it should be rotated. To get angle to that vector I use .angle() method. But there is one problem: rad_to_deg(.angle) returns a degree from -179.999 to 180 degrees. Let's say my ship is already oriented to -170 degrees and now new vector is set at 170 degrees. The obvious way to rotate the ship is all around from -170 to 170 = 340 degrees but really it should just rotate a bit in negative direction - from -170 to -190.
So, question: how can I make ship to understand the closest rotation direction by given initial rotation of ship itself and required rotation?
Also there may be a problem when ship's initial rotation is more than 180. If ship was rotated too much in one direction, let's say it's 1000 degrees now, I just fmod it's rotation every frame to 360 to avoid too big numbers here.

trim tiger
sacred finch
trim tiger
#

Ah.

storm verge
#

is it a physical ship aka apply_force()? Then you could IIRC apply_torque() to rotate.

sacred finch
#

No it's CharacterBody2D

deft basin
#

How are you doing the rotation?

sacred finch
deft basin
#

So I assume the speed calculation is where the issue is coming from?

#

Somewhere you're doing a calculation to determine which direction to turn. You need to check if abs(current_angle - target_angle) > 180. If it is, you need to add/subtract 360 so that you can get the proper direction

sacred finch
deft basin
#

I guess you don't even need to add/subtract 360, you just need to tell it to go the other way

#

If the "rot deg remaining" is more than 180, then you're rotating the wrong way

sacred finch