Graphics/Math Problem

Started by
2 comments, last by Punx 21 years, 7 months ago
I have a sprite in the middle of my screen and depending on where the mouse is I want to change the direction the sprite is facing. I want to start out with 4 different directions, North, South, West and East. Here's an ascii drawing of what I want. (except the quadrants would be of equal size) \ / x / Now what is the math for determining what sector the mouse is in? EDIT: Ahhh can't get the formatting right [edited by - Punx on September 4, 2002 1:21:30 AM]
~punx
Advertisement
Say the quadrants are numbered as such:
\1/2x3/4\ 


Create 2 variables: dx and dy (meaning delta x and delta y)
dx = mousex - playerx //Calculate the difference in xdy = mousey - playery //Same for yif(abs(dx) > abs(dy)) { //Does the dx have greater magnitude than the dy?    if(dx > 0) {         //Cursor is in quadrant 3    } else {        //Cursor is in quadrant 2    }} else {    if(dy > 0) {        //Cursor is in quadrant 1    } else {        //Cursor is in quadrant 4    }} 

Let me know if you need an explination of what is going on.
Yeah, I''ll need some further explaining on this. I tried it but it only worked right some of the time.
~punx
The way it works is you make dx and dy hold the distance from the player to the cursor in the x and y directions. So if you take the absolute value of this distance, you can compare them to see whether the cursor is further from the player in the x direction or the y direction. If you look at your drawing, you can see that if the x distance is greater than the y distance, the cursor must be in quadrants 2 or 3. Otherwise, it must be in 1 or 4. Then, all you have to do is find the sign of dx or dy to find exactly which quadrant it is in.

Are you converting the coordinates so that the player and mouse are represented using the same scale? This is necesary in order for dx and dy to provide useful information.

This topic is closed to new replies.

Advertisement