Search code examples
javaswinggame-physicsmouselistenerisometric

Isometric Game Mouse logic and movement around an environment


I have am making an Isometric game environment and I wish to navigate around the environment using a hold Right click and move the mouse to look around the map.

The movement works fine the first time but when i try it the second time it resets itself to the original location before moving again. I know this is to do with mouse position realtive to the background that is moving but ive tried a number of solutions and still the logic eludes me, is there anyone that can help me figuire out what im doing wrong, thanks in advance.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class CameraMovementTest extends JPanel{

private Timer timer;
private int DELAY = 10;
private  CustomMouseListener mouseListener;
private int positionX = 0, positionY = 0;

public CameraMovementTest() {

    mouseListener  = new CustomMouseListener();
    this.addMouseListener(mouseListener);
    this.addMouseMotionListener(mouseListener);
    this.setSize(500,500);
    this.setVisible(true);

    //Swing Timer
    timer = new Timer(DELAY, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            update();
            repaint();
            validate(); 
        }
    }); 
    timer.start();
}

private void update() {
    if(mouseListener!=null){
        positionX = mouseListener.getX();
        positionY = mouseListener.getY();
    }
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.fillRect( positionX,  positionY, 300,300);
}

public class CustomMouseListener implements MouseListener, MouseMotionListener {
    private int positionX = 0, positionY = 0;
    private int mouseClickX, mouseClickY;

    @Override
    public void mousePressed(MouseEvent evt) {

        if(SwingUtilities.isRightMouseButton(evt)) {

            mouseClickX = evt.getX();
            mouseClickY = evt.getY();
        }   
    }
    @Override
    public void mouseDragged(MouseEvent evt) {
        if(SwingUtilities.isRightMouseButton(evt)) {
            positionX = mouseClickX - evt.getX();
            positionY = mouseClickY - evt.getY();
        }
    }
    @Override
    public void mouseMoved(MouseEvent arg0) {}
    @Override
    public void mouseClicked(MouseEvent arg0) { }
    @Override
    public void mouseEntered(MouseEvent arg0) { }
    @Override
    public void mouseExited(MouseEvent arg0) {}

    @Override
    public void mouseReleased(MouseEvent arg0) {}
    public int getX(){
        return positionX;
    }
    public int getY(){
        return positionY;
    }
}


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame f = new JFrame();
            f.setVisible(true);
            f.setSize(new Dimension(500,500));
            f.setContentPane(new CameraMovementTest());
        }
    });
}

}


Solution

  • it resets itself to the original location before moving again.

    Did you add any debug code to look at your calculation of the x/y position?

    @Override
    public void mousePressed(MouseEvent evt) {
    
        if(SwingUtilities.isRightMouseButton(evt)) {
            mouseClickX = evt.getX();
            mouseClickY = evt.getY();
        }
    }
    
    @Override
    public void mouseDragged(MouseEvent evt) {
        if(SwingUtilities.isRightMouseButton(evt)) {
            System.out.println(mouseClickX + " : " + evt.getX());
            positionX = mouseClickX - evt.getX();
            positionY = mouseClickY - evt.getY();
        }
    }
    

    You set the mouseClickX/Y to the mouse point, then in the mouseDragged you subtract the mouse point from the value. Which means you basically get 0 (well actually 1, since the mouse moved 1 pixel to generate the drag event).

    So in the mousePressed logic you can't just reset the mouseClickX/Y to the current mouse point. You need to track that last mouse point as well:

    @Override
    public void mousePressed(MouseEvent evt) {
    
        if(SwingUtilities.isRightMouseButton(evt)) {
            //mouseClickX = evt.getX();
            //mouseClickY = evt.getY();
            mouseClickX = positionX + evt.getX();
            mouseClickY = positionY + evt.getY();
        }
    }