How to make a bullet shoot towards the mouse?

The title explains it all.

Apply force in direction of mouse. Integrate.

EDIT: That’s for “homing missile” behavior. Unless you mean, “how to make gun point in direction of mouse”

What if I’m not using a physics engine?

http://www.java-gaming.org/index.php/topic,26757.

Don’t need one. The code to do that is ~10 lines.

Well then, if it isn’t too big of a deal, could i have that code?

I still can’t tell (and you haven’t clarified) if you mean you want “point gun at mouse position” or “mouse-following homing missile bullets.”

Basically, When you click the mouse, I want the bullet to move in that direction. When you move the mouse, the bullets direction will never change.


vec.x = mouse.x-gun.x;
vec.y = mouse.y-gun.y;

Is the vector to the mouse. Normalize it and you have the direction as a vector.

So that would be gun aiming, more or less.


class Bullet {
    float velocityX, velocityY, positionX, positionY;
}

/**
    Sets Bullet [b] to fly towards [x, y] from it's current location at [speed].

*/
private void directBulletToPoint(Bullet b, float x, float y, float speed) {
    float dx = x - b.positionX;
    float dy = y - b.positionY;
    float len = Math.sqrt(dx*dx+dy*dy);
    b.velocityX = dx / len * speed;
    b.velocityY = dy / len * speed;
}

Simple vector stuff:
Get displacement vector
Normalize
Multiply by speed

Programmed in post, may contain errors.
Also somebody correct me if I screwed something up.

xp6ibuI8UuQ

Tutorial

This is for Cocos2D but the code is easy enough to translate.

I’m pretty sure I already answered exact same topic a lot of times already in the past… I remember at least 2 of these topics…

Yeah this is one of the more asked questions, the corresponding inquiries being “rotate arm” and “get angle”.

Shooting towards cursor position I can understand, but shooting toward mouse position seems insoluble to me… :slight_smile:

I do it like this.


	// your game tick
	public void tick() {
		if (Clicked) {
			int targetX2 = mse.x;
			int targetY2 = mse.y;
			double xDistance = mse.x - x;
			double yDistance = mse.y - y;

			int targetX = (mse.x - width / 2);
			int targetY = (mse.y - height / 2);
		}
		int pathX = (int) (game.planet.x + game.planet.width / 2 - width / 2 - x);
		int pathY = (int) (game.planet.y + game.planet.height / 2 - height / 2 - y);

		double distance = Math.sqrt(pathX * pathX + pathY * pathY);
		double directionX = pathX / distance;
		double directionY = pathY / distance;

		double movementX = directionX * 3;
		double movementY = directionY * 3;
		x += (int) movementX;
		y += (int) movementY;

	}

	// MouseListener (something that extends MouseListener)
	public void mousePressed(MouseEvent e) {
		if (e.getButton() == 1) {
			Clicked = true;
		}
	}

	public void mouseReleased(MouseEvent e) {
		if (e.getButton() == 1) {
			Clicked = false;
		}
	}

	// MouseMotionListener (something that extends MouseMotionListener)
	public void mouseDragged(MouseEvent e) {
		mse = new Point(e.getX(), e.getY());
	}

	public void mouseMoved(MouseEvent e) {
		mse = new Point(e.getX(), e.getY());
	}

Did it help?