# MouseListener Conundrum

• October 10th, 2011, 09:22 AM
Dirnol
MouseListener Conundrum
Hello,

I'm creating a top down dungeon crawler - esque game as a java application, and one of the problems I'm facing is changing the position of the player depending on where the mouse is in relation to the player.

Basically, if the mouse is in the top right corner of the screen, I want my player to be facing north east respectively.

Using my advanced paint skills, I have created an image to help illustrate my problem. Any help is greatly appreciated.

Attachment 787

Dirnol
• October 10th, 2011, 09:27 AM
copeg
Re: MouseListener Conundrum
• October 10th, 2011, 09:33 AM
Dirnol
Re: MouseListener Conundrum
I know how to make a MouseListener as well as how it functions, my problem is with the math I need to use to calculate the angle depending on where the mouse is on the screen.

For instance, let's say the mouse is at (750, 50), I know how to get that coordinate using the MouseListener, I also know that the player should be facing north east assuming that is where the mouse is. However I do not know what calculation I should use to tell the program where to face the player no matter where the mouse is.

I should also clarify that the player is always at a fixed position. (375, 275) Which positions them perfectly in the center of the screen.

So i just had an idea. I think if I find the slope of a line between the player (375, 275) and the mouse (x, y), I can change the image based on the answer.
I'm going to play around with that some, if anyone has any ideas, or a more effective way to go about doing it, please let me know :)
• October 10th, 2011, 06:13 PM
Dirnol
Re: MouseListener Conundrum
After getting a crash course on angles from my trig teacher (I haven't used sin, cosin, or tan since I was a freshman), I found a way to do it. It's probably not the most efficient way to do it but it worked for me. Here's what I did in case anyone was wondering:

Code java:

```import java.awt.Image; import java.awt.event.*; import javax.swing.ImageIcon; import java.lang.Math;   public class Player {   private Image img;   private double mouseX, mouseY, slopeX, slopeY; private double angle;   private final int MIDDLE_X = 400; private final int MIDDLE_Y = 300;   ImageIcon N = new ImageIcon(this.getClass().getResource("n.png")); ImageIcon E = new ImageIcon(this.getClass().getResource("e.png")); ImageIcon W = new ImageIcon(this.getClass().getResource("w.png")); ImageIcon S = new ImageIcon(this.getClass().getResource("s.png")); ImageIcon NE = new ImageIcon(this.getClass().getResource("ne.png")); ImageIcon NW = new ImageIcon(this.getClass().getResource("nw.png")); ImageIcon SE = new ImageIcon(this.getClass().getResource("se.png")); ImageIcon SW = new ImageIcon(this.getClass().getResource("sw.png"));   public Player(){   img = N.getImage();   }   public Image getImage(){ return img; }   public void mouseMoved(MouseEvent e) {   mouseX = e.getX(); mouseY = e.getY();   slopeX = mouseX - MIDDLE_X; slopeY = MIDDLE_Y - mouseY;   angle = Math.atan(slopeX / slopeY);   angle = Math.toDegrees(angle);   System.out.println("(" + slopeX + ", " + slopeY + ")"); System.out.println(angle);       if(slopeY > 0 && angle >= -22.5 && angle <= 22.5) img = N.getImage();   if(slopeY > 0 && angle > 22.5 && angle <= 66.5) img = NE.getImage();   if(slopeY > 0 && angle >= -66.5 && angle < -22.5) img = NW.getImage();   if(slopeX > 0 && angle > 66.5 && angle > -66.5) img = E.getImage();   if(slopeX < 0 && angle < 66.5 && angle < -66.5) img = W.getImage();   if(slopeY < 0 && angle > 22.5 && angle <= 66.5) img = SW.getImage();   if(slopeY < 0 && angle >= -22.5 && angle <= 22.5) img = S.getImage();   if(slopeY < 0 && angle >= -66.5 && angle < -22.5) img = SE.getImage();   }       }```
• October 10th, 2011, 07:59 PM
KevinWorkman
Re: MouseListener Conundrum
If you use atan2 instead, you don't have to double check the slope again. Play around with this:

Code java:

`double heading = Math.toDegrees(Math.atan2(yDistance, xDistance));`

PS- The reason copeg steered you towards the MouseListener tutorials was because it was really hard to tell what step you were stuck on (which is also why he referred you to the article on helping yourself get help). We can't just assume that people know the basics, so we always have to start there. Next time, throw together an SSCCE that shows what you have so far, that way we know exactly what step you're stuck on.
• October 11th, 2011, 06:49 AM
Dirnol
Re: MouseListener Conundrum
Thanks for the help, atan2 makes much more sense to use in this case. Also, I will definitely try to be more thorough in explaining my problem in the future, thanks for the advice :)
• October 11th, 2011, 07:11 AM
KevinWorkman
Re: MouseListener Conundrum
Quote:

Originally Posted by Dirnol
Thanks for the help, atan2 makes much more sense to use in this case. Also, I will definitely try to be more thorough in explaining my problem in the future, thanks for the advice :)

No problem. Glad you got it sorted.