Yes, precisely. If you identify the point (x,y) with the number x+iy, you can translate by adding a complex number, and you can rotate by multiplying by a unit-length complex number. I'll show you some examples below.By complex number you mean a number with a real and imaginary part? How does that work / is relevant in this context?
You may want to have a class Movement (or Transform, but I think "movement" captures the meaning better) that contains a rotation and a translation. You can compose two Movements, with the result being another Movement. So the vehicle and the turret both have Movements associated with them, and you need to compose the movement of the vehicle with the Movement of the turret (relative to the vehicle) to get the global Movement for the turret.
- Dealing with objects that are attached to other objects, but with some offset (e.g. say a turret on the front left part of a vehicle, not its centre). Specifically when this "parent" object rotates. (e.g. like the updatePos stuff)
If you need help with this Movement class, I can try to write some sample code.
- Dealing with rotation. Its easy to set a unit vectors direction to be the direction from point A to B, but what if I want to limit its turn rate?
rotation = target_direction / current_direction; if (rotation.real() < cos(max_angle)) rotation = Complex(cos(max_angle), sign(rotation.imag())*sin(max_angle)); current_direction *= rotation;
What if I want to add some random angle to a vector?
double random_angle = (2.0*drand48()-1.0) * max_angle; Complex random_rotation(cos(random_angle), sin(random_angle)); vector *= random_rotation;
- Comparing the direction objects are facing / heading. E.g. to see if a turret is near enough to facing the target or not (same goes for fixed weapons, and many other cases).
rotation = desired_direction / current_direction; if (rotation.real() >= cos(threshold_angle)) ...
Notice that some of the cos() and sin() calls in there have arguments that are constant. They are only needed because you are specifying the rate or the threshold for similarity as angles. If you also think of them as unit-length vectors instead, you rarely need any trigonometric calls at all.