Angle from x, y to new_x, new_y

Started by Badlands, August 09, 2010, 03:08:36 pm

Previous topic - Next topic

Badlands

I do not know how to find the angle from x & y starting positions to new_x & new_y final positions, I need a 360 degrees value.

An example, I have an object at (x24, y440) and I want to know the angle from there to (x240, y320) in a 360 degrees value

I do not know crap about math functions  :'(

Blizzard

angle1 = atan(y1 / x1)
angle2 = atan(y2 / x2)
angle = abs(angle1 - angle2)
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Holy crap BLizz thanks, I myself have been trying to figure this out ._.

Blizzard

That is if you are rotating it around (x0,y0).
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Badlands

It does not return a 0...360 value  :shy:

I'm using this to test it

module Math
def self.w_angle(x1, y1, x2, y2)
   angle1 = atan(y1 / x1)
   angle2 = atan(y2 / x2)
   angle = (angle1 - angle2).abs
   return angle
end
end

p Math.w_angle(24, 24, 390, 240)


Blizzard

It returns the angle in radians. Use this to convert to degrees:

    return (angle * 180 / Math::PI)
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Badlands

It still not working for me :(

 
def self.w_angle(x1, y1, x2, y2)
   angle1 = atan(y1 / x1)
   angle2 = atan(y2 / x2)
   angle = (angle1 - angle2).abs
  return (angle * 180 / Math::PI)
end

end

p Math.w_angle(272, 208, 390, 0)


That is not supposed to return 0.0

Blizzard

Try this:

  def self.w_angle(x1, y1, x2, y2)
   angle1 = atan2(y1, x1)
   angle2 = atan2(y2, x2)
   angle = (angle1 - angle2).abs
   return (angle * 180 / Math::PI)
 end

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

winkio

Pretty sure he wants the angle from the first point to the second.  With this code, zero is to the right, and the angle is measured counterclockwise.

  def self.w_angle(x1, y1, x2, y2)
    angle = atan2(y1-y2, x2-x1) * 180 / Math::PI
    if (angle < 0) angle += 360
    return angle
  end


Blizzard

But that doesn't make any sense. #_#
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Badlands

Yes Winkio! Thats exactly what I was trying to say!  :shy:
Thanks both of you, seriously n.n
I'm not male ._.

winkio

If my suggested reason doesn't make sense: well, say you wanted to fire a projectile off at a random angle.  Or maybe she is trying to make a particle system, or a geometry puzzle, or something that requires a geometric angle.

If my suggested code doesn't make sense: I reversed the y coordinates to make the value negative, to account for the fact that +y is down.  atan2 has the domain from -pi/2 to pi/2, thus after I convert into degrees, i need to add 360 to any negative values to put them between 0 and 360.

Blizzard

I meant that it didn't make sense to subtract the coordinates. But if the coordinates were the center and the point laying on the hypotenuse, then it does make sense. My bad, I wasn't sure what you were thinking about.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.