Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Badlands on August 09, 2010, 03:08:36 pm

Title: Angle from x, y to new_x, new_y
Post by: Badlands on August 09, 2010, 03:08:36 pm
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  :'(
Title: Re: Angle from x, y to new_x, new_y
Post by: Blizzard on August 09, 2010, 03:21:46 pm
angle1 = atan(y1 / x1)
angle2 = atan(y2 / x2)
angle = abs(angle1 - angle2)
Title: Re: Angle from x, y to new_x, new_y
Post by: G_G on August 09, 2010, 04:13:58 pm
Holy crap BLizz thanks, I myself have been trying to figure this out ._.
Title: Re: Angle from x, y to new_x, new_y
Post by: Blizzard on August 09, 2010, 04:22:18 pm
That is if you are rotating it around (x0,y0).
Title: Re: Angle from x, y to new_x, new_y
Post by: Badlands on August 09, 2010, 05:32:28 pm
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)

Title: Re: Angle from x, y to new_x, new_y
Post by: Blizzard on August 09, 2010, 06:21:56 pm
It returns the angle in radians. Use this to convert to degrees:

    return (angle * 180 / Math::PI)
Title: Re: Angle from x, y to new_x, new_y
Post by: Badlands on August 09, 2010, 06:56:59 pm
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
Title: Re: Angle from x, y to new_x, new_y
Post by: Blizzard on August 10, 2010, 02:15:28 am
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

Title: Re: Angle from x, y to new_x, new_y
Post by: winkio on August 10, 2010, 09:55:02 am
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

Title: Re: Angle from x, y to new_x, new_y
Post by: Blizzard on August 10, 2010, 10:46:26 am
But that doesn't make any sense. #_#
Title: Re: Angle from x, y to new_x, new_y
Post by: Badlands on August 10, 2010, 07:26:08 pm
Yes Winkio! Thats exactly what I was trying to say!  :shy:
Thanks both of you, seriously n.n
I'm not male ._.
Title: Re: Angle from x, y to new_x, new_y
Post by: winkio on August 10, 2010, 07:28:16 pm
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.
Title: Re: Angle from x, y to new_x, new_y
Post by: Blizzard on August 11, 2010, 02:20:42 am
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.