Rotating bullet bullet shot[Solved]

Hey Everyone,

So i’m having this problem that i can’t seem to figure out and i can’t find it on here. Maybe i’m just wording it wrong. basically what i want to do is where ever i press my mouse on the game i want a bullet from the player to shoot towards where ever i press. So my game is a top down game and i just can’t seem to figure it out. I know how to rotate the bullet manually but i don’t know how to do it with the mouse. I appreciate any help. thank you.

Something like this should do the trick

bulletDirection = mousePosition - gunPosition
normalize the vector bulletDirection
multiply bulletDirection with the desired speed

^ is the best way you can find. You don’t want to get into the angle stuff, but if you want to rotate the bullet sprite so it shoots in the correct direction, you’ll have to.

Yeah thats what i want to do.

this article i wrote may help you more on understanding moving/rotation
but in general i think you should add this method into your player class

this method will create a bullet in the current player coordinates (x,y and angle)

public void fire(int load, int number, int spread,int life) {

		if (tmpLoad == 0) {
			for (int i = 0; i < number; i++) {
				
				Board.bullet.setX(x + w);
				Board.bullet.setY(y + h / 2);
				Board.bullet.setA(a + ((spread * (i - 1)) / 2));
				Board.bullet.setW(5);
				Board.bullet.setH(5);

				bullets.add(new Bullet(Board.bullet.getX(),
						Board.bullet.getY(), Board.bullet.getA(), Board.bullet
								.getW(), Board.bullet.getH()));
			}
			
			tmpLoad = load;
		}else{
			tmpLoad -= 1;
			life--;
		}

	}

and based on this cooridnates, you make your bullet move forward

public void moveForward(int speed) {
		x += Math.cos(a)*speed;
		y += Math.sin(a)*speed;
	}

now all you have to do is painting your bullet array in your paint method and updating in your game loop

Vector solution:


//Vector2f is any vector implementation with 2 floats.
//position, velocity and target are both Vector2f.
//Position and target are already defined

velocity = target.subtract(position);
velocity.normalize();

Non-Vector solution (still uses vector math):


//An x in the variable name is the xcoord. y is ycoord.
//all variables are floats/doubles
//p is position, d is velocity, t is target. Position and target are already defined.

dx = tx-px;
dy = ty-py;

float magnitude = Math.sqrt(dx*dx+dy*dy);

dx /= magnitude;
dy /= magnitude;


I don’t understand your code…you’re creating new bullets by using static values of the bullet class? We don’t know what the board class/bullet class does either (if you’re going to copy+paste code from your project here, try to generalize it or put it into pseudocode).

@Jimmt
okay ::slight_smile:

My point was not to use magic numbers or non-standard classes in example code unless you explain what they are and what they do. It’s far more helpful to give pseudocode/a forumula or one-file example code than to have them read an article that doesn’t teach them anything new.

i got that, it will not happened again ;D
and i was going to write the article tomorrow morning (it’s 2:20AM here :3 ) but because what you said was right (my codes are un-readable :stuck_out_tongue: here) i decided to make the article now so the one who post this topic will find his answer, he want to know how to shoot bullets and that’s the main goal of my article (i already made one on movement) i know those are very simple and basic but people still asking about how they do it because they didn’t found an article or a tutorial about it on our forum, i asked the same thing 2 weeks ago so why not creating an article about all the basics i know, it can be useful for someone else, and as you know…
sharing is caring ;D

Ok guys i figured it out. Thanks for the help though. if you are wondering how i did it. this is how :


//Constructor of my bullet class. with the angle, x position and y position
public Bullet(double angle,int x, int y){
		this.x = x;
		this.y = y;
		
             // this is what makes the bullet move towards the mouse cursor. and getX() and getY() are the positions of the bullet.
		rad = Math.atan2(getY() - Game.mouseY, getX() - Game.mouseX) - Math.PI;
             // this is how i change the angles
		dx = Math.cos(rad) * speed;
		dy = Math.sin(rad) * speed;
}

When i shoot the bullet i the angle is the “rad” variable. I hope this helps.
-GlennBrann