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.
#Get closest turning to given angle
14 messages · Page 1 of 1 (latest)
lerp_angle() might be what you are looking for.
https://docs.godotengine.org/en/stable/classes/[email protected]#class-globalscope-method-lerp-angle
Global scope constants and functions. Description: A list of global scope enumerated constants and built-in functions. This is all that resides in the globals, constants regarding error codes, keyc...
I need to have my own rotation speed, required by game design, so lerp_angle is not an option.
Ah.
is it a physical ship aka apply_force()? Then you could IIRC apply_torque() to rotate.
No it's CharacterBody2D
How are you doing the rotation?
just rotation += value * delta where value is current rotation speed (it's calculated in other formulas)
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
no, I can simply multiply it by -1, I don't understand how to check which way is closer to rotate
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
oh, I didn't thought about that, thanks