I'm creating a 2d game and I wanted to implement shooting. However, bullets for some reason shoot only at a -45° angle. Provided below is my code. The if statement is also important and needs to know when the bullet goes out of range. I've looked up answers for the issue and none of them seem to work.
Also, .getEntity returns the JLabel of the entity.
public class Bullet extends Entity
{
private double speed, dmg, dist, vX, vY, xDest, yDest;
private double traveledDist = 0;
private JLabel bullet;
private String img;
public Bullet(double nSpeed, double nDmg, double xDest, double yDest, double nDistance, String img, double vX,
double vY)
{
super(80, 80, "res/img/bullet.png");
speed = nSpeed;
dmg = nDmg;
dist = nDistance;
this.xDest = xDest;
this.yDest = yDest;
this.img = img;
this.vX = vX;
this.vY = vY;
Image image = new ImageIcon("res/img/bullet.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT);
ImageIcon imageicon = new ImageIcon(image);
JLabel bullet = new JLabel(imageicon);
bullet.setSize(80, 80);
bullet.setVisible(true);
}
public String toString()
{
return ("[Speed: " + speed + ", Damage: " + dmg + ", xDest: " + xDest + ", yDest: " + yDest + ", vX:" + vX
+ ", vY" + vY + ", Distance: " + dist + ", Traveled Distance: " + traveledDist + ", Image Path: " + img
+ "]");
}
public double getxDest()
{
return xDest;
}
public double getyDest()
{
return yDest;
}
public double getvX()
{
return vX;
}
public double getvY()
{
return vY;
}
public JLabel newBullet()
{
return bullet;
}
public double getSpeed()
{
return speed;
}
public double getDmg()
{
return dmg;
}
public double getDist()
{
return dist;
}
public String getImg()
{
return img;
}
public double getTraveledDist()
{
return traveledDist;
}
public void setTraveledDist(double nDist)
{
traveledDist = nDist;
}
}
private void fireBullet()
{
System.out.println("bullet");
double originX = lblCross.getX() - lblChar.getX();
double originY = lblCross.getY() - lblChar.getY();
double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
double bulletVelocity = 1.0;
//mouseX/Y = current x/y location of the mouse
//originX/Y = x/y location of where the bullet is being shot from
double angle = Math.atan2(mouseX - originX, mouseY - originY);
double xVelocity = (bulletVelocity) * Math.cos(angle);
double yVelocity = (bulletVelocity) * Math.sin(angle);
Bullet tempBullet = new Bullet(((ProjectileWeapon) sWeapon).getProjectileSpeed(), sWeapon.getDamage(), 0, 0,
sWeapon.getRange() * 100, ("res/img/bullet.png"), xVelocity, yVelocity);
tempBullet.getEntity().setVisible(true);
room.add(tempBullet.getEntity());
room.repaint();
tempBullet.getEntity().setLocation(lblChar.getX(), lblChar.getY());
bullets.add(tempBullet);
}
private void updateBullet()
{
for (int i = 0; i < bullets.size(); i++)
{
bullets.get(i).getEntity().getY());
if (true)
{
System.out.println("updating" + bullets.get(i));
Bullet b = bullets.get(i);
b.getEntity().setLocation((int) (b.getEntity().getLocation().getX() + (b.getvX() * 10)),
(int) (b.getEntity().getLocation().getY() + (b.getvY() * 10)));
b.setTraveledDist(b.getTraveledDist() + 1);
room.repaint();
}
else
{
room.remove(bullets.get(i).getEntity());
bullets.remove(i);
room.repaint();
}
}
}
Your help is much appreciated.