Chaos Project

Game Development => Sea of Code => Topic started by: legacyblade on March 04, 2011, 04:16:38 pm

Title: Moving an Object around in a Circle?
Post by: legacyblade on March 04, 2011, 04:16:38 pm
Ok. I am TERRIBLE at geometry. I started trying to make an RMXP animation where four triangles are rotating around the target. And I fond out something I have literally no idea how to do: calculate the x/y location certain angles would have around a circle. I also want to make ring menus, so this inability simply won't do. I don't need to know anything fancy, like ellipses. I just need to figure out how to calculate where on a circle an object would be. Just a basic math equation would be nice (preferably one that only requires you to have the angle and radius), I know enough about code to implement it.
Title: Re: Moving an Object around in a Circle?
Post by: winkio on March 04, 2011, 04:19:09 pm
x = xcoordinate + radius*cos(angle)
y = ycoordinate + radius*sin(angle)

that's for a circle starting at the right and going counterclockwise.

also, you can check equations for most types of geometry on wikipedia:
http://en.wikipedia.org/wiki/Circle#Equations (http://en.wikipedia.org/wiki/Circle#Equations)
Title: Re: Moving an Object around in a Circle?
Post by: legacyblade on March 04, 2011, 04:44:44 pm
Thanks for such a quick response! But, I've toyed around with that equation you gave me but I'm not sure which bits of data will change which and how I'm supposed to get certain data. If I have the cos and sin, I'll already know where the x and y are. What I want to be able to do is find, say where x and y will be at 87 degrees on a circle that's 120px in radius. How would I do that? (And I don't understand geometry that well. I can't figure out how to apply the many equations I've found on the web to this situation because I haven't done anything geometry related for the past 9 years, and I was bad enough at it back then)
Title: Re: Moving an Object around in a Circle?
Post by: Blizzard on March 04, 2011, 05:09:20 pm
Then you first calculate the atan2 from the x and y to get the angle.

angle = atan2(dy, dx)
angle += 87


atan2 is used because it takes negative values in consideration. atan would return only [0, 180).

Remember that dx and dy here are the differences of the x, y and the center's x, y (basically dx = x2 - x1, dy = y2 - y1 where x1, y1 are the center coordinates of the circle).
Title: Re: Moving an Object around in a Circle?
Post by: legacyblade on March 04, 2011, 05:35:25 pm
I'll already have the angle. What I want are the x and y of the point on the edge of the circle  :'(
Title: Re: Moving an Object around in a Circle?
Post by: Ryex on March 04, 2011, 05:40:49 pm
        /|
   r  /  | y
    /    |
  /      |
/ a      |
--------
     x

r = radius
a = angle
y = y component of circle edge point
x = x " " " "

y = sin(a) / r
x = cos(a) / r

if a is in the other corner then reverse sin and cos
Title: Re: Moving an Object around in a Circle?
Post by: legacyblade on March 04, 2011, 05:55:55 pm
Ok, how would I obtain the sin and cos? I know these are basic concepts to you guys, but I really have always had trouble with everything geometry related. The most advanced geometry I could wrap my head around was the pathagorean theorem. I want to be able to write a function as follows

[code]
get_circle_edge(startAngle,endAngle,steps,radius)
   
   for i in 0...steps
      #draw icon at x,y for currentAngle
      @currentAngle += (startAngle - endAngle) / steps
   end

end


I know you guys have probably already as good as spelled out how to do this with the equations you've posted, but again I'm an idiot when it comes to geometry. Sorry x.x
[/code]
Title: Re: Moving an Object around in a Circle?
Post by: winkio on March 04, 2011, 05:58:08 pm
use the first equations i posted:

Assuming you wanted a circle of radius 120 with center at (0,0):
x = 120cos(angle)
y = 120sin(angle)

done.
Title: Re: Moving an Object around in a Circle?
Post by: legacyblade on March 04, 2011, 06:04:09 pm
How would I obtain the cos and sin for use in the equation though?
Title: Re: Moving an Object around in a Circle?
Post by: Ryex on March 04, 2011, 06:31:07 pm
cos, sin, and tan are all in built functions
Title: Re: Moving an Object around in a Circle?
Post by: legacyblade on March 04, 2011, 07:55:15 pm
lol, I feel dumb now. thanks. I am able to find coordinates now!

However, now I'm running into trouble when trying to draw things in a ring.

For a HUD I'm working on for a blizzABS demo project, I'm adding circles around the bottom left corner of a main circle. I want them to be evenly divided in this area. So I wrote a little function using the help you've all given me, here it is.

def circle_points(startAngle,endAngle,radius,steps)
    @startAngle = startAngle
    @endAngle = endAngle
    @radius = radius
    @steps = steps
    @currentAngle = @startAngle

    for i in 0..steps

      x = @radius * Math.cos(@currentAngle)
      y = @radius * Math.sin(@currentAngle)
      print("x:"+ x.to_s + ", y:" + y.to_s) #replace with draw_icon function when coded
      @currentAngle += ((@startAngle - @endAngle) * 1) / @steps
    end
  end


Now I've graphed the results out in another program, and for some reason when I have it start with an angle of 90 and and end at nintey, it goes 180 degrees. Anyone know what I can do to draw around sections of a circle? I don't know where my math error is coming in x.x
Title: Re: Moving an Object around in a Circle?
Post by: Ryex on March 04, 2011, 08:34:22 pm
you know how radians work right?


            PI/2
    3PI/4         PI/4
  PI                  0, 2PI
    5PI/4         7PI/4
            3PI/2

degrees work the same way, just replace PI with 180.  you can use this knowledge to offset the angle correctly
Title: Re: Moving an Object around in a Circle?
Post by: legacyblade on March 05, 2011, 02:03:47 pm
Alright, I think I know what to do with all the help you guys have given me. Thanks! I'll post if I run into any more problems.