Sign in to follow this  
P0jahn

Turret rotation

Recommended Posts

P0jahn    307

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.

Share this post


Link to post
Share on other sites
markr    1692

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.

Share this post


Link to post
Share on other sites
P0jahn    307

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.

Share this post


Link to post
Share on other sites
alvaro    21247
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

Share this post


Link to post
Share on other sites
P0jahn    307

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?

Share this post


Link to post
Share on other sites
nobodynews    3126
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.

Share this post


Link to post
Share on other sites
alexsok90    127

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;
}

Share this post


Link to post
Share on other sites
P0jahn    307

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

Share this post


Link to post
Share on other sites
P0jahn    307
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;
}

Share this post


Link to post
Share on other sites
alexsok90    127

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

Share this post


Link to post
Share on other sites
P0jahn    307

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

Share this post


Link to post
Share on other sites
P0jahn    307

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.

Share this post


Link to post
Share on other sites
alvaro    21247
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.

Share this post


Link to post
Share on other sites
P0jahn    307

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);
   currRotation = (float) Math.toRadians(currRotation);

   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);
}

Share this post


Link to post
Share on other sites
alvaro    21247

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

Share this post


Link to post
Share on other sites
P0jahn    307

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:

Share this post


Link to post
Share on other sites
CC Ricers    1491

Hey Alvaro, thanks for the sample and link on complex numbers. This is high school level stuff to me, but it's been so long since I've used it that I've now finally come to grips with it and it makes perfect sense for 2D rotations. Perhaps now I can get back to working on the rolling marble game I so much put off :D

Share this post


Link to post
Share on other sites
nobodynews    3126
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.

 

A thing you should understand to help get radians is that the units for degrees are arbitrary. What is a degree defined as? 1/360th of a full rotation. Why use 360 instead of any other number? It's just so common we keep on using it. Once you learn it it's easy to use in conversation and it has more whole numbers than radians which are harder to talk about. Despite being harder to talk about, radians are more natural. They don't rely on an aribtrary definition like degrees do and as such they are easier to work with in the language of math. A radian is defined as the rotation around an arc that's the same length as the radius. This means the radian can be derived independantly for any circle you'll ever look at. The reason radians come up in trigonometry is because of the relationship between triangles and circles.

 

As you continue learning about trigonometry you'll gain a better understanding of radians.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this