[solved]Shooting towards the mouse

I am having trouble shooting a bullet towards the mouse. The bullet shoots fine and moves towards the mouse however clicking to shoot again will adjust the old bullets rotation aswell. For example i click on the left, the bullet will fly off to the left as expected you then click to the right and another bullet will appear and fly off to the right as expect however the first bullet fired will follow the second bullet fired because the mouses position as been updated. My question to you guys is how do i make it so that once a shot has been fired its velocity doesnt change after?

Bullet Class

public Bullet(int x, int y, int type) {
		super(x, y);
		this.type = type;
		Image();
	}
	
	public void update() {
		y += velY;
		x += velX;
		y += secvelY;
		x += secvelX;
		
		if (type == 0) { currentImage = lead_Bullet;}
		if (type == 1) { currentImage = laser_Bullet;}
	}
	
	public void draw(Graphics2D g2d) {
		g2d.drawImage(currentImage, x, y, null);
		if (Game.debugMode) { g2d.draw(getBounds());  }
	}

	public static double velX;
	public static double velY;
	public static int secvelX;
	public static int secvelY;
	public int type=0;
	public Image currentImage;
	
	// Characters
	private Image lead_Bullet;
	private Image laser_Bullet;
	
	
	public void Image() {
		ImageIcon q1 = new ImageIcon(this.getClass().getResource("/misc/lead bullet.png"));
		lead_Bullet = q1.getImage();
		ImageIcon q2 = new ImageIcon(this.getClass().getResource("/mobs/player old 1/player_right_old1.png"));
		laser_Bullet = q2.getImage();
	}
	 public Rectangle getBounds() {
		return new Rectangle (x,y,9,9);
	 }
}

Shooting code:

public static void shoot() {shot = true;}
	public void BulletUpdate() {
		if (shot==true && Lead_Bullets > 0) {
			shotTimer++;
			Bullet tempBullet = new Bullet(Game.player.x, Game.player.y, 0);
			Bullets.add(tempBullet);
			dx = Object_Control.msx - x;
			dy = Object_Control.msy - y;
			
			double theta = Math.atan2(Object_Control.msy - y, Object_Control.msx - x);
			dx = (int) ((Environment.speed*bulletspeed)*Math.cos(theta));
			dy = (int) ((Environment.speed*bulletspeed)*Math.sin(theta));

			Bullet.velX = dx;
			Bullet.velY = dy;
			Lead_Bullets-=1;
		}
		if (shotTimer > 0) {
			shot = false;
			shotTimer = 0;
		}
	}

ALL help is appriecated :slight_smile:

Why do you think making the same topic three times will yield different answers? If you want help, ask in your two other topics that are similar to this one.

Wait, didn’t you help someone out with this same problem? :point:

Anyway, the short answer is, you have to let the bullets keep track of where they are firing. In other words, don’t let it shoot towards the mouse, but have it store the location of the mouse and shoot in that direction. Bullets should always keep track of their own direction and velocity.

I am noticing that you ARE storing the movement data for the bullet with the Bullet class. The problem is that the information is static. Thus, whatever you last put into those variables will be shared by all instances of that class.

Without having looked at your code too deeply, it seems to me that something as simple as losing the static keywords for the variables and creating individual bullets instead of calling Bullet statically could solve your problem. You might have to write some additional code to manage the bullets and their lifetimes as a collection.

I’d add mousex and mousey integers to my bullet class then set the velocity of the bullet with it’s own mouse position field.

I posted a question before to find out how to get the bullet to shoot towards the mouse. I then repeated the thread accidentally forgetting that this was the site i had originally posted on so for that i apologise. However after getting my answer on one or the threads i had noticed that it had worked at first glance however after some testing i realised there was a problem with the code because the velocity would change for every bullet when i clicked. So here is the thread trying to fix this

Each bullet needs its own velocity. Why in the world you made that static I do not know.

Also, DO NOT LOAD A NEW IMAGE FOR EACH BULLET. Sorry the caps was rude. You can load an image once and then set the reference to other objects.


Image a b c;
void loadImages()
{
a = load blah blah blah; 
b = load blah blah blah; 
c = load blah blah blah; 
}

public class Bullet
{
//stuff.
BufferedImage image;

public Bullet(your args)
{
 image = a b c or what ever.
}

}

Sorry for bad formatting. I usually have a static class that has a reference to all images and then grab the reference when an object needs the image.

Image a = ImageResources.get(“imagename”);

This way you do not have a thousands of images loaded but only one.

Yeah the static was the problem. As silly as it sounds I have never really fully understood what static means I have just used it if ive gotten an error but now I know, Thankyou!

…google what static means in java. It is something very basic and you should really know. Don’t worry about not currently knowing static but also use the google more when you do not know something.