Detección de colisiones en el método mouseMoved

I am creating a game in java. In it, you control a square that follows your mouse. I want to implement collision detection for the square so that it stops slightly within the JFrame and not at the edge. This is very easy when doing this with the arrow keys, but I can't figure it out with the mouseMoved method. Here is the code where the mouseMoved method is:

public void mouseMoved(MouseEvent e){

            repaint();
            if(e.getX() <= 0)
                playerX = 0;
            if(e.getX() >= 300)
                playerX = 500;
            if(e.getY() <= 0)
                playerY = 0;
            if(e.getY() >= 300)
                playerY = 500;
            else
            playerX = e.getX()-25;
            playerY = e.getY()-25;
            repaint();

        }

Here's the code where the square is created:

public void paintComponent(Graphics g) {

        Rectangle player = new Rectangle(playerX, playerY, 50, 50);
        g.setColor(Color.blue);
        g.fillRect(player.x, player.y, player.width, player.height);
        repaint();

    }

I don't think you'll need this, but just in case, here's all the code for the GamePanel class, which serves as the panel for my JFrame in the Main class. If you need the Main class let me know but I doubt you will:

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{

    //Global variables

    //Double buffering
    private Image dbImage;
    private Graphics dbg;


    //JPanel variables
    static final int GWIDTH = 500, GHEIGHT = 500;
    static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);

    //Game variable
    private Thread game;
    private volatile boolean running = false;
    public boolean mouseClicked = false;

    //Character variables
    int playerX = 150, playerY = 150;

    public class Mouse extends MouseAdapter{

        public void mousePressed(MouseEvent e){

            mouseClicked = true;

        }

        public void mouseReleased(MouseEvent e){

            mouseClicked = false;

        }

        public void mouseMoved(MouseEvent e){

            repaint();
            if(e.getX() <= 0)
                playerX = 0;
            if(e.getX() >= 300)
                playerX = 500;
            if(e.getY() <= 0)
                playerY = 0;
            if(e.getY() >= 300)
                playerY = 500;
            else
            playerX = e.getX()-25;
            playerY = e.getY()-25;
            repaint();

        }
    }



    public GamePanel(){

        addMouseMotionListener(new Mouse());
        setPreferredSize(gameDim);
        setBackground(Color.BLUE);
        setFocusable(true);
        requestFocus(true);

    }

    public void run(){

        while(running){



        }

    }

    public void addNotify(){

        super.addNotify();
        startGame();

    }

    private void startGame(){

        if(game == null || !running){

            game = new Thread(this);
            game.start();
            running = true;

        }

    }

    public void stopGame(){

        if(running){

            running = false;

        }

        //Paint method


    }

    public void paint(Graphics g){

        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);

    }

    public void paintComponent(Graphics g) {

        Rectangle player = new Rectangle(playerX, playerY, 50, 50);
        g.setColor(Color.blue);
        g.fillRect(player.x, player.y, player.width, player.height);
        repaint();

    }

    private void log(String s){

        System.out.println(s);

    }

}

Thank you for your help. Please let me know if you need anything.

preguntado el 09 de septiembre de 13 a las 23:09

Don't rely on magic numbers, use getWidth and getHeight to determine the size of the game area. Don't call repaint within any paint method, this will cause an infinite loop which will slowly consume your CPU. Do call super.paintComponent(g) ;) -

1 Respuestas

Your movement code is a bit off. You set the x position only if the Y is not out of bounds and always replace the Y value. Can I suggest full blocks in future - they help avoiding problems like this.

playerX = e.getX()-25;
playerY = e.getY()-25;
if(e.getX() <= 0){
    playerX = 0;
}
else if(e.getX() >= 300){
    playerX = 500;
}

if(e.getY() <= 0){
    playerY = 0;
}
else if(e.getY() >= 300){
     playerY = 500;
}

This sets the position first and then corrects it if the player is out of the bounds.

Respondido el 10 de Septiembre de 13 a las 09:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.