Archived

This topic is now archived and is closed to further replies.

Graphics/Math Problem

This topic is 5582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 x
dy = mousey - playery //Same for y
if(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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites