is this the right way to rotate and shooting bullets ??

Hello,
I’ve made a little changes to the game am working on, and now it’s like this ;

-move with arrow keys
-gun rotate toward mouse
-shoot bullet

now the problem is the bullets doesn’t go to the correct target, plus, they all rotate together, even after the bullet is shot it still change the x and y direction

my board class :

package clickOfGod;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Board extends JPanel implements Runnable {
	// declaration
	private Ship ship;
	private Bullet bullet;
	private Gun gun;
	private Control mouse;

	private Thread loop;

	private ArrayList bullets;

	public static int mx, my;

	public static double angle;

	// constructor
	public Board() {
		init();
		setBackground(Color.lightGray);
		setDoubleBuffered(true);
		setFocusable(true);
		addMouseListener(mouse);
		addMouseMotionListener(mouse);
		this.addKeyListener(mouse);

	}

	// initialisation
	private void init() {
		ship = new Ship();
		bullet = new Bullet(0, 0, 0, 0);
		gun = new Gun(0, 0);
		bullets = gun.getBullets();

		angle = 0;

		mouse = new Control();
		mx = 0;
		my = 0;

		loop = new Thread(this);
		loop.start();

	}

	// painting
	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		// drawing the ship
		g2d.draw(ship.getBounds());

		// rotating and drawing the gun
		g2d.rotate(angle, gun.getX() + 5, gun.getY() + 60);
		g2d.drawRect(gun.getX(), gun.getY(), gun.getW(), gun.getH());


		// creating the bullets arrayList
				ArrayList bs = gun.getBullets();
				for (int i = 0; i < bs.size(); i++) {
					Bullet tb = (Bullet) bs.get(i);
					g2d.draw(tb.getBounds());

				}
		

		g.dispose();
	}

	// game logic
	public void play() {

		ship.move(); // moving the ship
		gun.fire(50);// firing

		// placing the gun in the middle of the ship
		gun.setX(ship.getX() + ship.getW() / 3);
		gun.setY(ship.getY() - ship.getH() / 2);
		// calculating the angle rotation of the gun based on mouse coorindates
		// (my,mx)
		angle = Math.atan2(gun.getY() - my, gun.getX() - mx) - Math.PI / 2;

		//moving or removing the bullets 
		ArrayList bs = gun.getBullets();
		for (int i = 0; i < bs.size(); i++) {
			Bullet tb = (Bullet) bs.get(i);
			
			if (tb.getTravel()) {
				tb.move(mx,my);
			} else {
				bs.remove(i);
			}
		}

	}

	// game loop
	public void run() {

		while (true) {
			repaint();
			play();

			try {
				loop.sleep(5);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
		}

	}

}

my bullet class :

package clickOfGod;

import java.awt.Rectangle;

public class Bullet {

	private int x, y, width, height;
	private boolean travel;

	public Bullet(int x, int y, int w, int h) {
		this.x = x;
		this.y = y;
		this.width = w;
		this.height = h;

		travel = true;
	}

	public void setX(int x) {
		this.x = x;
		;
	}

	public void setY(int y) {
		this.y = y;
		;
	}

	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	
	public Rectangle getBounds() {
		return new Rectangle(x, y, width, height);
	}

	public boolean getTravel() {
		return travel;
	}

	public void setTravel(boolean travel) {
		this.travel = travel;
	}

	public void move(float tx, float ty) {

	if (x != tx || y != ty) {
			float dx = tx - x;
			float dy = ty - y;

			float len = (float) Math.sqrt(dx * dx + dy * dy);
			if (len != 0) {
				dx /= len;
				dy /= len;
			}

			float speed = 2f;
			dx *= speed;
			dy *= speed;

			x += dx;
			y += dy;

			travel = true;

		} else {
			travel = false;
		}

		
		
		
		
		
		
		
		
		
	}

}

my gun class

package clickOfGod;

import java.util.ArrayList;

public class Gun {

	private int x, y, w, h, life;

	private ArrayList bullets;

	public Gun(int x, int h) {

		define();

	}

	public void define() {
		this.x = x;
		this.y = y;
		this.h = 60;
		this.w = 10;
		bullets = new ArrayList();
		life = 0;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getW() {
		return w;

	}

	public int getH() {
		return h;
	}

	public ArrayList getBullets() {
		return bullets;
	}

	public void fire(int load) {

		life += 1;
		if (life == load) {
			bullets.add(new Bullet(x, y, 10, 10));
			life = 0;

		}

	}

}

i think those are the only classes you need to understand my problem

thank you


ArrayList bs = gun.getBullets();
      for (int i = 0; i < bs.size(); i++) {
         Bullet tb = (Bullet) bs.get(i);
         
         if (tb.getTravel()) {
@@            tb.move(mx,my);
         } else {
            bs.remove(i);
         }
      }

The problem is this.

You are setting the target each frame for bullets that have already been fired.
The velocity should be calculated once and saved.

any specific solution please ?

I’d suggest asking that after thinking for longer than 510 seconds.

xD
sooorryy :stuck_out_tongue: