Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

How to calculate an angle between 2 points..

Ok I just want to know how to calculate an angle between two points, I think i should trignometric right? But how Could I calculate the angle let's say in this picture:

http://www.academiarpg.com/personal/dam ... s/img1.gif[/IMG]

This is what I did:
Code:
if @point_right.y > @point_left.y
        @oox = @point_right.x
        @ooy = @point_left.y
        @oposite = Math.sqrt(((@oox - @point_right.x)**2)+((@ooy - @point_right.y)**2))
        @adjacent = Math.sqrt((@oox - @point_left.x)**2) + ((@ooy - @point_left.y**2))
        @hypn = Math.sqrt(((@point_left.x - @point_right.x)**2)+((@point_left.y - @point_right.y)**2))
        @sene = @oposite / @hypn
        $game_player.angle = Math.cosh(@sene)
     elsif @point_right.y < @point_left.y
        @oox = @point_left.x
        @ooy = @point_right.y
        @oposite = Math.sqrt(((@oox - @point_left.x)**2)+((@ooy - @point_left.y)**2))
        @adjacent = Math.sqrt((@oox - @point_right.x)**2) + ((@ooy - @point_right.y**2))
        @hypn = Math.sqrt(((@point_left.x - @point_right.x)**2)+((@point_left.y - @point_right.y)**2))
        @sene = @oposite / @hypn
        $game_player.angle = Math.cosh(@sene)
      else
        @angle = 0
      end
note: my trignometric is a bit rusty....lol

Thanx for your help!
 
Okay, you first have to determine at least 2 side lengths.. For example, the altitude(vertical) is 5 tiles(5units), then the base(horizontal) is 10 tiles(10units). You dont have to calculate for the hypotenuse but use TANGENT...

Like:
x = angle

tan x = 5/10
tan x = 1/2
x = arctan(1/20

*where arctan is tan with a -1 exponent(inverse)

x will be the angle...(use a calculator of course)
 
Soh Cah Toa

sin = opp / hyp
cos = adj / hyp
tan = opp / adj

Meaning, if you have a side of a right triangle and and angle, you can figure out everything else.

So lets take a look at seph's crappy drawing
Code:
       A
  ----------
   \ a     |
    \      |
     \     |
     C\    |B
       \   |
        \  |
         \b|
          \|

However, since you (If I am right) are rotating a sprite. Right?

If you are wanting to rotate it from the center point, I could work out a method to offset the x and y for you.
 
I'll throw some code at you (but you have to figure out how to use it, but it does answer your question)

Code:
    if @distance_x == 0
      # The Angle is sign(distance_y) * - π / 2 (90° or 270°)
      @angle = @distance_y.sign * Math::PI / 2
    # If Y distance is 0 (Prevent Incorrect Direction for later)
    elsif @distance_y == 0
      # The Angle is sign(distance_x) - 1 * π / 2 (0° or 180°)
      @angle = (@distance_x.sign - 1) * Math::PI / 2
    else
      # The Angle is the Arctangent of @distance_y / @distance_x (slope)
      # Returns [-Ï€,Ï€]
      @angle = Math.atan2(@distance_y, @distance_x.to_f)
    end
    # Convert the angle to degrees
    @angle *= 180 / Math::PI

class Numeric
  def sign
    return 0 if self == 0 or self == 0.0
    return (self / self.abs).to_i
  end
end
 
@seph that's right it's for rotating a sprite...If you fell like doing the method, thanx.
@Trickster : I am trying to decifre that code.. But I must say that its a bit complicate...
@all_others: thanx for your help...
 
Perhaps you want the whole method I took that from, I'll leave the comments in also (My battle systems movement routine)

Code:
#--------------------------------------------------------------------------
  # * Move the sprite
  #   x     : x coordinate of the destination point
  #   y     : y coordinate of the destination point
  #   speed : Speed of movement (0 = delayed, 1+ = faster)
  #   delay : Movement delay if speed is at 0
  #--------------------------------------------------------------------------
  def move(x, y, speed = 1, delay = 0)
    # Set Destination Points speed and delay and move count set moving flag
    @destination_x = x
    @destination_y = y
    @move_speed = speed
    @move_delay = delay
    @move_old = @count
    @moving = true
    # Get the distance + (negative if to left or up positive if down or right)
    @distance_x = (@destination_x - self.x).to_f
    @distance_y = (@destination_y - self.y).to_f
    # Get slant distance (hypotenuse of the triangle (xf,yi) (xi,yf) and (xf,yf))
    @distance = Math.sqrt(@distance_x ** 2 + @distance_y ** 2)
    # Calculate angle of movement which is later used to determine direction
    # If X distance is 0 (Prevent Infinity Error)
    if @distance_x == 0
      # The Angle is sign(distance_y) * - π / 2 (90° or 270°)
      @angle = @distance_y.sign * Math::PI / 2
    # If Y distance is 0 (Prevent Incorrect Direction for later)
    elsif @distance_y == 0
      # The Angle is sign(distance_x) - 1 * π / 2 (0° or 180°)
      @angle = (@distance_x.sign - 1) * Math::PI / 2
    else
      # The Angle is the Arctangent of @distance_y / @distance_x (slope)
      # Returns [-Ï€,Ï€]
      @angle = Math.atan2(@distance_y, @distance_x.to_f)
    end
    # Convert the angle to degrees
    @angle *= 180 / Math::PI
  end
 
Well the PIs are for eating, but I never heard of a square root :P

But really here is a Full Explaination aside from the commented lines which explains that code

First I got the X Distance, and the Y Distance which represent the two shorter sides of the triangle, the longest side (the hypotenuse of the triangle) can be gotten by the Pythagorean identity SQRT(side1^2 + side2^2) = hypotenuse, so now I have all three sides so now to get the angle I can either do one of the four

Math.asin(ydistance / hypotenuse)
Math.acos(xdistance / hypotenuse)
Math.atan(ydistance / xdistance)
Math.atan2(ydistance, xdistance)

but noting the range of what each of these returns I choose Math.atan2

If X Distance is 0 then the angle is shooting straight up is the angle is PI / 2 or 3 * PI / 2 (To prevent Infinity Errors the script checks for this)

If Y Distance is 0 then the angle is flat so the angle is PI or 0

the angle is in radians so to convert to degrees you multiply by 180 / Pi

Trigonometry is fun :)

and I added a sign method to the Numeric class which gets the sign (returns 1 if postive -1 if negative and 0 if zero) and basically Its just the number divided by the Absolute value of that number
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top