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
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