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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: MouseListener Conundrum

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    Thanks in advance,
    Dirnol


  2. #2

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Dirnol; October 10th, 2011 at 10:18 AM.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
     
    	}
     
     
     
    }
    Last edited by Dirnol; October 10th, 2011 at 06:20 PM.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: MouseListener Conundrum

    Quote Originally Posted by Dirnol View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. problem with the MouseListener
    By boaz1001 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 23rd, 2011, 07:23 AM
  2. 3Dmouse mouselistener
    By zimzille in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 03:12 AM
  3. Help with mouselistener and mousemotionlistener pls..
    By rentaw02 in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2011, 05:45 PM
  4. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  5. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM