Reputation: 370
I am building a game where a playernode can rotate and has to match the angle of a target. When the target spawns it has an angle between 0 and 359. To match the angles I am working with the zRotation of the player and the target.
My question is, how to tell the player to just rotate from 0 to 359 and not more like 360, 361, 362... and not less than 0 like -1, -2...
I tried to set the players zRotation to 0 if it is larger than 360 and to 360 if it is smaller than 0, but I don't think this is the best way to go.
Anyone has done something similar?
Upvotes: 0
Views: 306
Reputation: 3190
You can do this with modulus. Either after your calculation:
zRotation %= CGFloat(M_PI*2)
Or on your calculation:
zRotation = (zRotation + amount) % CGFloat(M_PI*2)
Upvotes: 1