shooting is working but ...

Hello,
it’s almost a week now, i’ve been trying to shoot bullets depending on the gun direction, and i think this is the best result i got so far,
am able to shoot and each bullet go to its own direction depending on the gun rotation, the only thing is that the bullet directions are wrong :stuck_out_tongue:
it is supposed to go to the up-right corner but instead :

http://s20.postimg.org/wkc6fgif1/Capture.png

i really don’t know why am facing that much problem with rotation :-\
please can you help

codes :

bullet :

package clickOfGod;

import java.awt.Rectangle;

public class Bullet {

	private int x, y, width, height, s;
	double a;
	private boolean travel;

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

	}

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

	}

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

	public void setA(double a) {
		this.a = a;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public double getA() {
		return a;
	}

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


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


	public void move() {

		x += 2 * Math.cos(a);
		y += 2 * Math.sin(a);

	}

}

Gun :

package clickOfGod;

import java.util.ArrayList;

public class Gun {

	private int x, y, w, h, life;
	private double a;
	static int bx, by;
	private ArrayList bullets;
	static boolean firing, inc, dec;
	static double now;

	public Gun(int x, int y, double a) {

		define();

	}

	public void define() {
		this.x = x;
		this.y = y;
		this.h = 60;
		this.w = 10;
		this.a = a;
		bullets = new ArrayList();
		life = 0;
		firing = false;
		now = 0;
		inc = false;
		dec = false;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public double getA() {
		return a;
	}

	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 setA(double a) {
	
		this.a=a;
	}

	public void fire(int load) {

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

		}

	}

}

Board :

package clickOfGod;

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

import javax.swing.JPanel;

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

	private ArrayList bullets;

	public static boolean shoot;

	public static int mx, my;
	public static double angle;
	public static float yv, xv = 0;

	// 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();
		gun = new Gun(0, 0, 0);
		bullet = new Bullet(0, 0, 0, 0, 0);
		bullets = gun.getBullets();

		angle = 0;

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

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

		shoot = false;
	}

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

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

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

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

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

			g2d.rotate(tb.getA(), tb.getX() + 5, tb.getY() + 60);
			g2d.draw(tb.getBounds());
			g2d.setTransform(old);
		}

		g.dispose();
	}

	// game logic
	public void play() {
		// moving the ship
		ship.move();
		// firing
		if (shoot) {
			gun.fire(10);

		}
		// 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();
			} else {
				bs.remove(i);
			}
		}

		// 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;
		gun.setA(angle);

	}

	// game loop
	public void run() {

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

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

				e.printStackTrace();
			}
		}

	}

}

thank you

Dont use

Math.atan2()

where ???
please am really tired of the angle thing :stuck_out_tongue:
i need a clear example to understand it once for all
can anybody do that please ?

Let me start off by saying that using ata2 is perfectly fine, it may not be the best solution, but there’s certainly nothing wrong with it.

this is your problem:


int x, y;

   public void move() {

      // implicitly, your values are cast back to ints here,
      // leading to a huge loss of accuracy
      x += 2 * Math.cos(a);
      y += 2 * Math.sin(a);

   }

making ‘x’ and ‘y’ floats would solve this problem.

thank you but how can i draw the rectangle with floats x & y ?

PS :
my angles are always something like this 0.7150018600912116
is there a way to make it look normal ? (from 0 to 360 )

thanx

cast the x and y variables to a int in the drawing


g2d.drawRect((int)gun.getX(), (int)gun.getY(), gun.getW(), gun.getH());

…and add 0.5 to round it off to the nearest int

g2d.drawRect((int) (gun.getX() + 0.5f), (int) (gun.getY() + 0.5f), gun.getW(), gun.getH());

i tried that, the result is more strange, bullets always go to the right and slow down when it goes to the top

Eh? Perhaps i misunderstood. Did you get a stranger result when changing x,y to floats? The cast to int was for the drawing only.

yes i change the x and y to float inside the bullet function, and i cast to int in the drawing part

PS :
can you guys please right an example of rotating and moving toward rotation angle ??
please i really need to understand how it works

thank you

you really need to be aware of the difference between radians and degrees if you’re doing anything related to angles.

Radians are measured based off a unit circle with the radius of 1. There are 2*pi radians in a circle, which is equal to 360 degrees, so there are pi radians in a triangle. Use Math.toDegrees(radians) to convert from radians to degrees. Its a much better presentable format. :slight_smile:

guys …
am really stacked here
PLEASE
anyone could explain the whole process of rotating a rectangle and make it move toward the rotation angle
pleaaaaaaaaaase

thank you very much

Isn’t the vertical axis reversed?


public void move() {

    x += 2 * Math.cos(a);
-   y += 2 * Math.sin(a);
+   y += 2 * Math.sin(-a);

}

I think this works now.

thank you guys for your help,
i finally made it ;D
the main problem was that i didn’t know the “head” if my squares ::slight_smile: