Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Trying to calculate a position in a circle

Author
Message
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jul 2015 00:40 Edited at: 29th Jul 2015 00:41
I'm sure there are genii who can solve this for me....

Here is the diagram:



I know the position of the centre (+). For simplicity call it 0,0
I know the position of the red dot (x and y)
I can calculate a1 (angle) and length h (hypotenuse of the triangle)

Now the red dot rotates around the circle to the green point. I want to calculate the coordinates of the green point.

I know the angle a2 (because I have set it)
I know that I have another length h because the dot is rotating around the centre.
But how do I get the x and y coordinates of the green dot?

In my final solution, the red and green dots could be at any position (but always have the same h as each other. I will always measure zero degrees as vertically down.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Attachments

Login to view attachments
29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 29th Jul 2015 01:03 Edited at: 29th Jul 2015 01:09
Just add a1 to a2 and then use trig to find the new x and y positions (then add the centre position of the circle):

new_x = h*sin(a1+a2) + (x position of circle centre)
new_y = h*cos(a1+a2) + (y position of circle centre)



I'm not entirely sure what the code snippet is for but I did it anyway.

I've added a diagram:



Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jul 2015 01:14
Thanks!
I was sat on the sofa thinking I needed another triangle with a right angle in it, and there it is. Appreciated

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jul 2015 01:44
You might be able to help me with this one too. Using the same diagram, I work out the angle of the red point like this:

a1 = asin(x / h)

For the lower part of the circle I get -90 to + 90 (left to right), with zero at bottom centre
But for the upper part I also get -90 to + 90 with 0 at top centre

What I want to get is 0 to 360 (or 0 to 180 then -180 back to 0) with zero at bottom centre and 180 at top centre.

Do I need to make a manual adjustment based on whether I am above or below the centre?

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jul 2015 02:02
I got there.

I needed atan2 which gives you -180 to +180, based on the coordinates. It's the simplest option of them all!

I just have to offset by 90 degrees, because it starts centre right.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 29th Jul 2015 02:25
Cool. Can I go to bed now?

Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 29th Jul 2015 02:40
other idea with SpriteOffset



AGK 108 (B)19 + AppGameKit (Steam) V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Uncle Martin
18
Years of Service
User Offline
Joined: 9th Jul 2006
Location: Tampa, Florida, USA
Posted: 29th Jul 2015 08:35
Another useful algorithm that covers these types of situations is the 2D rotational transformation. You do not have to worry about the hypotenuse. Simply provide the x/y coordinates of your points(s) & the angle you want to rotate them. The textbook formula is something like this:

NewPnt.x = OldPnt.x * cos(Ang) - OldPnt.y * sin(Ang)
NewPnt.y = OldPnt.y * cos(Ang) + OldPnt.x * sin(Ang)

You may need to swap the signs based on the coordinate & rotation conventions you are using.

If you need to rotate around an arbitrary point simply add the x/y offset after the rotation.

Here's some code to play with. The A & D keys rotate the points, Esc to exit. Enjoy.



Code every line like it might be your last...
Someday it will be.
xGEKKOx
AGK Master
15
Years of Service
User Offline
Joined: 7th Dec 2008
Location: Italy
Posted: 29th Jul 2015 11:53
A new Agar.io is coming?

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jul 2015 13:52
Thanks again everyone, some useful stuff here.

@Markus, I started with a similar idea using SetSpriteOffset. But I also have to rotate the sprite around it's centre. This made it impossible to use the offset idea.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 29th Jul 2015 14:42 Edited at: 29th Jul 2015 14:43
@BatVink,
yo that means u need 2 sprites or just math beside

AGK 108 (B)19 + AppGameKit (Steam) V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)

Login to post a reply

Server time is: 2024-11-25 19:33:43
Your offset time is: 2024-11-25 19:33:43