Search code examples
javamouseslick2d

Shoot to the mouse direction


The problem:

I've got this "Shot" class. In the code, the target variables are the mouseX and mouseY. So when i click the mouse button, my player class will create a new shot object. But the shooting is inaccurate. How can i calculate the correct dx and dy?

If i add the dx and dy to the "bullet's" x and y, the bullet will move to the mouse's direction.This is what i want. The mouse position is stored in targetX and targetY, when the object is created. This is the point what the oval wants to reach.

Links:

The game (finished)

The code (from Shot.java):

public class Shot extends Entity {
    private float targetX, targetY;

    public Shot(World world, float x, float y, int width, int height, Color color, float targetX, float targetY) {
        super(world, x, y, width, height, color);
        this.targetX = targetX;
        this.targetY = targetY;
    }

    @Override
    public void render(GameContainer gc, Graphics g, Camera camera) {
        g.setColor(color);
        g.fillOval(x - camera.getX(), y - camera.getY(), width, height);
    }

    @Override
    public void update(GameContainer gc, int delta) {
        float dx = targetX - x;
        float dy = targetY - y;

        x += dx * delta * .001f;
        y += dy * delta * .001f;
    }
}

I tried this, but still not work:

@Override
    public void update(GameContainer gc, int delta) {
        float length = (float) Math.sqrt((targetX - x) * (targetX - x) + (targetY - y) * (targetY - y));

        double dx = (targetX - x) / length * delta;
        double dy = (targetY - y) / length * delta;

        x += dx;
        y += dy;
    }

I did it! Here is my solution:
The problem was that, the target was the window's mouse position, and not the world's mouse position.

This is how i calculated the world's mouse positions:

float mouseWorldX = x + (mouseX - screen_width / 2); // x = player's x position
float mouseWorldY = y + (mouseY - screen_height / 2); // y = player's y position

Solution

  • This is code from my game at the moment is used to move a unit to the mouse when the right mouse button is pressed:

    length = Math.sqrt((target_X - player_X)*(target_X - player_X) + (target_Y - player_Y)*(target_Y - player_Y)); //calculates the distance between the two points
    
    speed_X = (target_X - player_X) /length * player_Speed;
    
    speed_Y = (target_Y - player_Y) /length * player_Speed;
    

    This will move an object to the target in a line at a set speed.

    Edit: this is the actual code right from my game

    if(input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
        {
            length = (float) Math.sqrt((player_waypoint_X - player_X)*(player_waypoint_X - player_X) + (player_waypoint_Y - player_Y)*(player_waypoint_Y - player_Y));
            velocityX = (float) (player_waypoint_X - player_X) /length * (float) PlayerStats.player.db_player_Speed;
            velocityY = (float) (player_waypoint_Y - player_Y) /length * (float) PlayerStats.player.db_player_Speed; 
    
            player_waypoint_X = input.getMouseX() - 2;
            player_waypoint_Y = input.getMouseY() - 2;
    
        }
    

    For testing purposes the velocity's are defined in the init method along with length. Every time the right mouse is pressed the waypoints's X and Y are changed to the mouse location.

    I learned this from this question velocity calculation algorithm.