Results 1 to 7 of 7

Thread: [RESOLVED] A little maths help... determine angle to face

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Resolved [RESOLVED] A little maths help... determine angle to face

    Hey,

    I'm working on my own project at the moment, it's a tower defence game. I want to rotate a tower to face an enemy and I'm having a little trouble figuring it out.

    What I need to know is how to get the real x and real y of the enemy. Let's say the tower is at x 200, y 200, and the monster at x 300, y 50, which would put the monster above and to the right of the tower, how exactly do I determine the real value of the monster from those coordinates?

    Code:
    float realX = ...
    float realY = ...
    
    float radians = atanf(realY / realX);
    Would appreciate any help, thanks

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: A little maths help... determine angle to face

    RealX, RealY ?? Aren't you trying to calculate the direction (in Radians or Degrees)?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: A little maths help... determine angle to face

    Quote Originally Posted by opus View Post
    RealX, RealY ?? Aren't you trying to calculate the direction (in Radians or Degrees)?
    I am, but don't I need those to do that? I remember needing them a long time ago. I need the result in radians, too.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: A little maths help... determine angle to face

    Now i get it, RealX should be the differnce between the XPos of the Tower and the XPos of the enemy (I normally use names like DiffX and DiffY)
    I didn't consider this one as a problem (subtraction is sort of a first class topic)
    I your example it would calculate as RealX (200-300) and RealY(200-50)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: A little maths help... determine angle to face

    All right this is mostly working:

    Code:
            
    float diffX = tower.x-enemy.x;
    float diffY = tower.y-enemy.y;
            
    float angleRadians = atanf(diffX / diffY);
    angleRadians *= -1;
    The tower now follows the enemy but as soon as the enemy moves below the tower on the screen it flips out.

    EDIT: I've solved it. After some Googling, I tried atan2f instead of atanf and it worked. I'm not entirely sure why, and would appreciate an explanation if anyone has the time. Thanks again for your help opus.
    Last edited by Icyculyr; Feb 1st, 2012 at 03:47 AM.

  6. #6
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] A little maths help... determine angle to face

    The short answer to why atan2 worked and atan didn't is that atan2 is defined to work in the situation you've described, whereas atan is not.

    To give an example, suppose tower is at (1, 1) and enemy is at (0, 0). The angle from the enemy to the tower is 45 degrees, measured clockwise with 0 degrees represented by the positive y direction. diff is then (1, 1), so the angle you calculate is atan(diff.X / diff.Y) is atan(1) (= pi/4 radians = 45 degrees, as it should be). Swap the tower and enemy; this makes diff (-1, -1), so the angle you calculate is again atan(-1/-1) = atan(1), the same as before, even though you know physically the angle is now 45 degrees + 180 degrees.

    Essentially, the inverse tangent (arctangent; atan; atn) function can't distinguish between (1/-1) or (-1/1); (1/1) or (-1/-1). The atan2 function can since the top and bottom are passed as separate arguments.

    The atan function is mathematically nicer, which is why it exists at all. When making games most people just want atan2. It happens that one can implement atan2 in terms of atan very easily, and atan can be calculated using a variety of efficient series.
    Last edited by jemidiah; Feb 1st, 2012 at 11:49 PM. Reason: Noticed it's diff.X / diff.Y instead of the more traditional diff.Y / diff.X; reversed example orientation accordingly.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  7. #7

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [RESOLVED] A little maths help... determine angle to face

    Quote Originally Posted by jemidiah View Post
    The short answer to why atan2 worked and atan didn't is that atan2 is defined to work in the situation you've described, whereas atan is not.

    To give an example, suppose tower is at (1, 1) and enemy is at (0, 0). The angle from the enemy to the tower is 45 degrees, measured counterclockwise with 0 degrees represented by the positive x direction. diff is then (1, 1), so the angle you calculate is atan(diff.X / diff.Y) is atan(1) (= pi/4 radians = 45 degrees, as it should be). Swap the tower and enemy; this makes diff (-1, -1), so the angle you calculate is again atan(-1/-1) = atan(1), the same as before, even though you know physically the angle is now 45 degrees + 180 degrees.

    Essentially, the inverse tangent (arctangent; atan; atn) function can't distinguish between (1/-1) or (-1/1); (1/1) or (-1/-1). The atan2 function can since the top and bottom are passed as separate arguments.

    The atan function is mathematically nicer, which is why it exists at all. When making games most people just want atan2. It happens that one can implement atan2 in terms of atan very easily, and atan can be calculated using a variety of efficient series.
    I see. Thanks for the explanation!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width