I have a turret in my 2D game, a turret capable of firing projectiles at its target(often the main character).

The turret is rotating so it is "pointing" at the target, if it can see it.

However, the rotation speed is instant. For example, the target is at the bottom-right-edge of the map, now the target somehow teleported to the top-left-edge of the map. You wouldnt see the turret rotating towards the top-left-edge. Instead, it would just immediately change.

So I want to implement some sort of rotation speed mechanism.

Here is an idea I came up with:

The turret is associated with an object that is moving in a circular path. Lets call this circular-moving object for c.

c is following the target. Since c is restricted to move in a circular path, it cant follow the target. So by follow, I mean whether it should move clockwise or counter clockwise. How to detect this, I haven no idea.

The easiest way to do this, is using vectors and the angle-between-two-vectors algorithm.

Suppose you know the turret's location and the target's, then you can find a vector to the target.

And suppose you know the turret's current angle, you can find a vector pointing "forwards" from the turret.

Normalise these two vectors, then you can find the dot-product, which gives you the angle.

But how do you know which way to turn? This can be done by doing the same thing with the "right" vector of the turret. If you dot the target with the right vector, then a + answer means you should turn "right" (i.e. clockwise), -ve, means you should turn left.

So:

* Find the "right" vector of the turret

* Find the vector to the target

* Assuming both are normalised, the angle between them is arc-sine of the dot-product.

You can then decide whether to turn the turret right,left, or shoot the gun. Presumably the turret has a maximum turn rate, so you'll clamp the turn rate at that. You might also want to model accelerator and/or movement delay for the turret.

I like to use complex numbers to do computations in 2D geometry. Applying a rotation is represented by multiplication by a complex number whose modulus is 1. If I have a vector A and I am trying to make it point towards vector B, the rotation I need to apply is B/A normalized. I can then limit that rotation to something close to 1 (1 being no rotation, since I am multiplying).

This code is in C++, but hopefully I managed to make it reasonably clear. Notice how the loop doesn't have any trigonometric functions at all. There are also basically no special cases.

typedef is used to create alias for types in c and c++. In all places after this where you see C, you can substitute that for std::complex<float>.

Max rotation is basically the speed at which you can rotate.

It returns the angle as a complex number. If you really want an angular rotation represented by degrees you'll need to use trig to get that information. When the complex number is normalized then the real component represents the cosine of the angle of rotation and the imaginary component represents the sine of the angle of rotation. To get the angle of rotation as a float then you just take the inverse cosine of the real or the inverse sine of the imaginary. If you don't know if the complex number you have is normalized you can also use arctan of the real and imaginary numbers.

You might want to read this guide about complex numbers. It could help explain more of the math Alvaro does and it's also pretty informative in general.

These values don't make any sense. Are you sure you are using Atan2 method? Also keep in mind that this code makes use of radian angles (not degrees). Or maybe your rotationSpeed value is too high. In my project it was like 0.02f (i was calling this method every update).

#EDIT You can also try replacing that "else if" with "if" only. Or swapping Atan2 arguments from "src - target" to "target - src".

No, the problem is that you are always adding or subtracting `speed', even if the difference between the angles is smaller than `speed'. When the current angle is less than `speed' away from the target angle, you should set the current angle to the target angle.

I don't know much Java, but I suspect that code will do the wrong thing when `currRotation' is 0 and `destinationRotation' is 359. The angles will be considered to be 359 degrees away, instead of 1.

I really think you should give up using angles: They force you to use expensive trigonometric functions all over the place, they make the code hard to get right because there are so many special cases that one has to consider, and in the end it is hard to decide if that code won't blow up in some circumstances.

The code using complex numbers is really much much simpler, once you get past the mental block of representing rotations as complex numbers with mudulus 1. I don't quite know why you dismissed the idea earlier.

Well, the reason I use degrees is because the functions that rotates images wants the value to be in degrees and not radians. And radians is something I am unfamiliar with. I never really understood what radians tbh. I know it is a way to measure/represent angles, but thats about it. Wiki was not good at explaining it either.

As for complex numbers. I dont even know how a complex number work, the equivalent in java etc.

This is what the C++ API says:

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them: