0

In the game im making I want to use the mouse cursor to aim with. This is the code I have for know to make the bullet move towards the mouse cursor:

Locating the mouse position private PointerInfo mouse = MouseInfo.getPointerInfo(); private Point point = new Point(mouse.getLocation()); Making the bullet kinda move towards the mouse cursor

if(point.getX() > player.getX()){
        setX(getX() + 1);
}
if(point.getX() < player.getX()){
        setX(getX() - 1);
}
if(point.getY() > player.getY()){
        setY(getY() + 1);
}
if(point.getY() < player.getY()){
        setY(getY() - 1);
}

The problem is that the bullet won't travel precisly to the mouse cursor but travel in a 45 degree path dow to the left if the mouse cursor is somewhere in that area, the same thing goes for up to the left, down to the right and up to the right.

4
  • 1
    For better help sooner, post an SSCCE. Apr 22, 2013 at 17:09
  • @RobWatts Yes they are
    – programmer
    Apr 22, 2013 at 17:28
  • Should bullets follow the cursor, or head in the direction of where the cursor was when they were fired?
    – Rob Watts
    Apr 22, 2013 at 17:38
  • @RobWatts head in the direction of where the cursor was when they were fired
    – programmer
    Apr 22, 2013 at 17:38

1 Answer 1

2

In order to make the bullets move directly toward the cursor and not always in 45 degree paths, use double for your internal representation of X and Y. Also, since you want the bullet to head in a certain direction, you'll want to compute the X velocity and Y velocity when you first create the bullet.

For example:

double deltaX = player.getX() - point.getX();
double deltaY = player.getY() - point.getY();
double magnitude = Math.sqrt(deltaX*deltaX + deltaY*deltaY);

// It's possible for magnitude to be zero (cursor is over the person)
// Decide what you want the default direction to be to handle that case
double xVelocity = 1;
double yVelocity = 0;
if (magnitude != 0) {
    xVelocity = deltaX / magnitude;
    yVelocity = deltaY / magnitude;
}

With the velocities computed when you first create the bullet, at each tick you just need to use xPosition += xVelocity and yPosition += yVelocity.

8
  • double deltaX = x - point.getX(); double deltaY = y - point.getY(); double deltaXX = deltaX * deltaX; double deltaYY = deltaY * deltaY; double magnitude = Math.sqrt(deltaXX = deltaYY); double xVelocity = 1; double yVelocity = 0; if (magnitude != 0) { xVelocity = deltaX / magnitude; yVelocity = deltaY / magnitude; } x += xVelocity; y += yVelocity;
    – programmer
    Apr 22, 2013 at 18:51
  • Why does the bullet go int the exact oposite direction of the mouse cursor and whydo the bullet have different speed depending on where im aiming?
    – programmer
    Apr 22, 2013 at 18:52
  • Sorry, I had a typo in the magnitude calculation (used = when I meant +). See if that helps.
    – Rob Watts
    Apr 22, 2013 at 18:56
  • And as for going in the opposite direction, try switching the deltaX and deltaY calculations, i.e deltaX = point.getX() - x.
    – Rob Watts
    Apr 22, 2013 at 18:57
  • now it does move in the same speed and it goes in the right direction but for some reason the bullets stop at a random location after they have traveled for a certain distance. This is a picture of how it looks like: link
    – programmer
    Apr 22, 2013 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.