Finding the angle of an object on a circle (C++ and SFML)
12 replies, posted
So I've got a circle like this with an object on it: (Think of the circle as a planet)
[img]http://anyhub.net/file/1xnA-one-object.png[/img]
Now, in this picture we see that the object is at a 180 degree angle to circle (think of the origin line going across the y-axis) and we could rotate it 180 degrees to get this:
[img]http://anyhub.net/file/1xnB-updisde-down.png[/img]
But what if it is in a position we don't know about, say, at 213, 366 (~240 degrees)
How can I find the angle of the object (relative to the circle)?
atan2(PlayerY - PlanetY, PlayerX - PlanetX)
Something like that, atan2 in whatever language you are using probably returns radians though.
Angle in degrees: (angle in radians * 180) / Pi
[QUOTE=noctune9;27547460]atan2(PlayerY - PlanetY, PlayerX - PlanetX)
Something like that, atan2 in whatever language you are using probably returns radians though.[/QUOTE]
But don't I need to account for the radius of the circle? I'll try it anyway
[editline]20th January 2011[/editline]
Doesn't work, I get stuff like this:
[img]http://anyhub.net/file/1xo5-atan2-test.png[/img]
I really need to get a book on higher math, I'm only taking geometry.
-Snip-
These were not the equations you are looking for.
[QUOTE=eXeC64;27547967][img_thumb]http://img832.imageshack.us/img832/590/ss20110120210925.png[/img_thumb]
Unless I've cocked up the maths in an obvious and embarassing way.[/QUOTE]
Alright, but thing about that is I need the angle to rotate the object so it is facing outward from the center of the circle. If I'm not mistaken, there is no way the 0(theta?) symbol can equal ~160
[QUOTE=neos300;27547725]But don't I need to account for the radius of the circle? I'll try it anyway
[editline]20th January 2011[/editline]
Doesn't work, I get stuff like this:
[img_thumb]http://anyhub.net/file/1xo5-atan2-test.png[/img_thumb]
I really need to get a book on higher math, I'm only taking geometry.[/QUOTE]
I think your programming language might have a variation where atan2 is (x,y) instead of the usual (y,x).
Try atan2(PlayerX - PlanetX, PlayerY - PlanetY) instead.
[QUOTE=noctune9;27548332]I think your programming language might have a variation where atan2 is (x,y) instead of the usual (y,x).
Try atan2(PlayerX - PlanetX, PlayerY - PlanetY) instead.[/QUOTE]
It's C++, the documentation said it's y, x but I tried it anyway and found this:
[img]http://anyhub.net/file/1xot-atan2-fail.png[/img]
But.... if I add 180 to each angle...
[img]http://anyhub.net/file/1xou-atan2-win.png[/img]
SUCCESS
Problem solved.
Dude please post the code, thanks ;).
[QUOTE=HeatPipe;27604911]Dude please post the code, thanks ;).[/QUOTE]
It's pretty simple, just figure it out for yourself :v:
You know in a top down shooter where the character looks at the crosshair? Well if they were then repositioned to the location of the crosshair it would be identical to them being on a circle of radius player position - crosshair position.
Thousands of code snippets etc. are available on the internet for rotating a top down character based on crosshair position, so it should be simple to adapt that.
[QUOTE=Jallen;27605944]It's pretty simple, just figure it out for yourself :v:
You know in a top down shooter where the character looks at the crosshair? Well if they were then repositioned to the location of the crosshair it would be identical to them being on a circle of radius player position - crosshair position.
Thousands of code snippets etc. are available on the internet for rotating a top down character based on crosshair position, so it should be simple to adapt that.[/QUOTE]
I was more interested in how you wrote the code in "OOP" sense, not actual rendering and rotating, that's easy.
[QUOTE=HeatPipe;27606063]I was more interested in how you wrote the code in "OOP" sense, not actual rendering and rotating, that's easy.[/QUOTE]
In the actually OOP sense?
The code is just contained in the main loop with an if statement.
[QUOTE=neos300;27606486]In the actually OOP sense?
The code is just contained in the main loop with an if statement.[/QUOTE]
Oh ok then ;).
Sorry, you need to Log In to post a reply to this thread.