So I'm making a TD game where I can place a gatling gun, and depending on which sector of a surrounding circle the mouse is in, the sprite and bullet path will change.
My difficulty is with creating an algorithm which will tell me which sector my mouse is in.
My circle has 16 sectors, and a radius of 300. Each arc has a length of 117.81. Extending from (300,300), I have an exact list of all the coordinates of the lines, so I am able to currently draw the sector like this: Circle
I'm using a mouse listener to detect the coordinates of my mouse whenever my mouse moves, so I have a "currentPoint" to check within which sector it's in. Based on this information, can anyone think of an easy way to simply return an integer of which sector the mouse is currently inside? Preferably somewhat efficiently.
These are the two ways I'm thinking about how it would look: Two_Ideas
And I did look at this StackOverflow which seemed like a similar problem: Efficiently find points inside a circle sector And I implemented it with Java, but it doesn't seem to translate without having Vectors and I'm a bit too confused about the math to make it work.
Been trying to figure this out for a while, I would love any help with an implementation of any kind, (don't mind adding Trig calculations), along with any help understanding the problem. Thank you!!
CodePudding user response:
To get sector, you need to get angle relative to point center.
Pseudocode (I am not sure how math functions and rounding look in Java):
double angle = math.atan2(mouse.y-center.y, mouse.x-center.x);
angle = angle - math.pi / 16.0;
while (angle < 0) {
angle = angle 2*math.pi;
}
sector = math.floor(angle * 8.0 / math.pi);
I made correction by half-sector becouse your first sector is centered around OX axis.