Re: MouseListener Conundrum
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 :)
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();
}
}
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.
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 :)
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.