Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. 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.

myconondrome.jpg

Dirnol

3. 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

4. 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:

```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();

}

}```

5. Re: MouseListener Conundrum

If you use atan2 instead, you don't have to double check the slope again. Play around with this:

`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.

6. 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

7. Re: MouseListener Conundrum

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.