Moving an Object around in a Circle?

Started by legacyblade, March 04, 2011, 04:16:38 pm

Previous topic - Next topic

legacyblade

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.

winkio

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

legacyblade

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)

Blizzard

March 04, 2011, 05:09:20 pm #3 Last Edit: March 04, 2011, 05:11:01 pm by Blizzard
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).
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.

legacyblade

I'll already have the angle. What I want are the x and y of the point on the edge of the circle  :'(

Ryex

        /|
   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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

legacyblade

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]

winkio

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.

legacyblade

How would I obtain the cos and sin for use in the equation though?

Ryex

cos, sin, and tan are all in built functions
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

legacyblade

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

Ryex

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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

legacyblade

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.