JAVA 绘制从角色发射到鼠标位置的子弹的有效方法?

JAVA Efficient way to draw bullets firing from character to mouse position?

我正在创建一个 2d 游戏,我想实现射击。但是,出于某种原因,子弹只能以 -45° 角发射。下面提供的是我的代码。 if 语句也很重要,需要知道项目符号何时超出范围。我查找了该问题的答案,其中 none 似乎有效。

此外,.getEntity returns 实体的 JLabel。

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();
                    }
    }
}

非常感谢您的帮助。

这是我最终使用的代码:

double theta = Math.atan2(destination.getY() - origin.getY(), destination.getX() - 
origin.getX());
    xVelocity = Math.cos(-theta);
    yVelocity = -Math.sin(-theta);