• Create Account

## Turret rotation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

22 replies to this topic

### #1P0jahn  Members

305
Like
0Likes
Like

Posted 15 January 2013 - 09:03 AM

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.

Then, we use an algorithm explained here: http://www.gamedev.net/topic/636343-get-a-points-coordinate/ , check if the target is intersecting with this line. If it is, we set c's movespeed to 0 and the turret can fire.

### #2markr  Members

1692
Like
0Likes
Like

Posted 15 January 2013 - 11:20 AM

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.

### #3P0jahn  Members

305
Like
0Likes
Like

Posted 15 January 2013 - 12:10 PM

I am more interested in your approach. My way was just an overkill.
So this is what I came up with.

public static double getAngle(float x1, float y1, float x2, float y2)
{
float deltaX = x2 - x1;
float deltaY = y2 - y1;

return Math.atan2(deltaY, deltaX) * 180 / Math.PI;
}

public static float rotateTowardsPoint(float x, float y, float targetX, float targetY, float speed)
{
Point2D.Float normalized = normalize(x, y, targetX, targetY);
double angle = getAngle(x, y, normalized.x, normalized.y);

return (float) clamp(angle, -speed, speed);
}

public static double clamp (double i, double low, double high)
{
return java.lang.Math.max (java.lang.Math.min (i, high), low);
}

public static Point2D.Float normalize(float x1, float y1, float x2, float y2)
{
float dx = x1 - x2;
float dy = y1 - y2;
double length = Math.sqrt( dx*dx + dy*dy );
dx /= length;
dy /= length;

return new Point2D.Float(dx,dy);
}

The results ingame, is that the unit that is suppose to rotate, rotates, but just to the same angle every frame. And the rotation is not even pointing at the target.

### #4P0jahn  Members

305
Like
0Likes
Like

Posted 15 January 2013 - 12:13 PM

(I dont want to edit my prev post because this site keeps fucking the text in the code tags whenever I edit my post)

### #5Álvaro  Members

20255
Like
1Likes
Like

Posted 15 January 2013 - 12:47 PM

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.
#include <iostream>
#include <complex>
#include <cmath>

typedef std::complex<float> C;

float const pi = std::atan(1.0f)*4.0f;

C normalize(C z) {
return z/std::abs(z);
}

C limited_rotation_towards(C original, C target, C max_rotation) {
C rotation = normalize(target / original);
if (rotation.real() < max_rotation.real())
return rotation.imag() > 0.0f ? max_rotation : conj(max_rotation);
return rotation;
}

int main() {
C z = 1.0f;
C target (-1.0f, -1.0f);
float max_angle = pi*0.125f;
C max_rotation(std::cos(max_angle), std::sin(max_angle));

for (int i=0; i<20; ++i) {
std::cout << z << '\n';
z *= limited_rotation_towards(z, target, max_rotation);
}
}


The output is this:
(1,0)
(0.92388,-0.382683)
(0.707107,-0.707107)
(0.382683,-0.92388)
(-1.49012e-07,-1)
(-0.382684,-0.923879)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)
(-0.707107,-0.707107)

Edited by Álvaro, 15 January 2013 - 12:54 PM.

### #6P0jahn  Members

305
Like
0Likes
Like

Posted 15 January 2013 - 01:49 PM

Thanks, but I cant understand that code.

typedef std::complex<float> C;?

Why do I need a max rotation?

Why doesnt it return a float/double?

### #7nobodynews  Members

3016
Like
0Likes
Like

Posted 15 January 2013 - 04:34 PM

Thanks, but I cant understand that code.

typedef std::complex<float> C;?

Why do I need a max rotation?

Why doesnt it return a float/double?

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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

### #8alexsok90  Members

127
Like
0Likes
Like

Posted 16 January 2013 - 02:54 AM

Here is the code from tank game I developed. It rotates the turret clockwise or counter-clockwise (depeding on which path is shorter) with desired speed.

destinationRotation = Math.Atan2(turret.Y - target.Y, turret.X - target.X) + Math.PI;

if (destinationRotation != currentRotation)
{
if (destinationRotation > currentRotation)
{
if (currentRotation < destinationRotation - Math.PI)
currentRotation -= rotationSpeed;
else
currentRotation += rotationSpeed;
}
else if (destinationRotation  < topRotation)
{
if (currentRotation > destinationRotation + Math.PI)
currentRotation += rotationSpeed;
else
currentRotation -= rotationSpeed;
}

if (currentRotation > Math.PI * 2.0f) currentRotation = 0;
if (currentRotation < 0) currentRotation = Math.PI * 2.0f;
}

### #9P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 05:23 AM

Thanks, that code make a lot more sense, Just one thing, what do topRotation represent and how do I get that value?

### #10alexsok90  Members

127
Like
0Likes
Like

Posted 16 January 2013 - 05:27 AM

Ah, sorry - this topRotation should be currentRotation. I was renaming the variables and missed this one.

### #11P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 05:47 AM

Hmm. Does not work for some reason.

I was circulated the turret, and the turret was stuck in two angles.

Results(the returned angle):

1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855
2.2831855
1.2831855

### #12P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 05:48 AM

public static float rotateTowardsPoint(float srcX, float srcY, float targetX, float targetY, float currRotation, float speed)
{
float destinationRotation = (float) (Math.atan2(srcY - targetY, srcX - targetX) + Math.PI);

if (destinationRotation != currRotation)
{
if (destinationRotation > currRotation)
{
if (currRotation < destinationRotation - Math.PI)
currRotation -= speed;
else
currRotation += speed;
}
else if (destinationRotation  < currRotation)
{
if (currRotation > destinationRotation + Math.PI)
currRotation += speed;
else
currRotation -= speed;
}

if (currRotation > Math.PI * 2.0f) currRotation = 0;
if (currRotation < 0) currRotation = (float) (Math.PI * 2.0f);
}
return currRotation;
}

### #13alexsok90  Members

127
Like
0Likes
Like

Posted 16 January 2013 - 05:52 AM

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".

Edited by alexsok90, 16 January 2013 - 05:59 AM.

### #14P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 06:04 AM

Okey, I made small modifications, so it use radians and then at the bottom, convert it back to degrees and return it.

I am still getting erratic results. Btw this function is called every frame.

rotation = rotateTowardsPoint(x1,y1,x2,y2, rotation, rot);
3.4906584E-4
3.491722E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
3.4917222E-4
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227
0.10966227

### #15alexsok90  Members

127
Like
0Likes
Like

Posted 16 January 2013 - 06:46 AM

#deleted

Edited by alexsok90, 16 January 2013 - 06:48 AM.

### #16P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 09:44 AM

Alright, I fixed it. There was some errors with my radians to degree and vice versa conversions.

However, this statement: if (destinationRotation != currRotation) is not sufficient enough. I need better detection.

When it is facing the target, is start shaking. Instead, it should just stand still.

I think the problem is that the double to float cast is destroying the precision.

### #17Álvaro  Members

20255
Like
0Likes
Like

Posted 16 January 2013 - 09:51 AM

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.

### #18P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 10:18 AM

Thanks all, it is fully working now. Here is the complete code if anyone is interested:

public static float rotateTowardsPoint(float srcX, float srcY, float targetX, float targetY, float currRotation, float speed)
{
float destinationRotation = (float) (Math.atan2(srcY - targetY, srcX - targetX) + Math.PI);

if(Math.abs((currRotation + 180 -  destinationRotation) % 360 - 180) < speed)
currRotation = destinationRotation;
else
{
if (destinationRotation > currRotation)
{
if (currRotation < destinationRotation - Math.PI)
currRotation -= speed;
else
currRotation += speed;
}
else if (destinationRotation  < currRotation)
{
if (currRotation > destinationRotation + Math.PI)
currRotation += speed;
else
currRotation -= speed;
}
if (currRotation > Math.PI * 2.0f) currRotation = 0;
if (currRotation < 0) currRotation = (float) (Math.PI * 2.0f);
}
return (float) Math.toDegrees(currRotation);
}

### #19Álvaro  Members

20255
Like
0Likes
Like

Posted 16 January 2013 - 10:31 AM

if(Math.abs((currRotation + 180 -  destinationRotation) % 360 - 180) < speed)

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.

Edited by Álvaro, 16 January 2013 - 10:32 AM.

### #20P0jahn  Members

305
Like
0Likes
Like

Posted 16 January 2013 - 09:39 PM

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:

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.