Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 塔防:子弹数学_Java_Math_Slick2d - Fatal编程技术网

Java 塔防:子弹数学

Java 塔防:子弹数学,java,math,slick2d,Java,Math,Slick2d,我正在使用Slick2D库用Java制作一个塔防游戏。 我的数学有个问题。现在,子弹将在敌人当前的X,Y坐标后射击-但由于当子弹到达X,Y坐标时敌人已经移动,它将缺少后面。除了加速子弹,你知道怎么解决这个问题吗?子弹数学在子弹类的底部 public class Bullets implements GameObject { private ArrayList<Bullet> bulletList = new ArrayList<Bullet>(); Enemy enemy

我正在使用Slick2D库用Java制作一个塔防游戏。 我的数学有个问题。现在,子弹将在敌人当前的X,Y坐标后射击-但由于当子弹到达X,Y坐标时敌人已经移动,它将缺少后面。除了加速子弹,你知道怎么解决这个问题吗?子弹数学在子弹类的底部

public class Bullets implements GameObject {
private ArrayList<Bullet> bulletList = new ArrayList<Bullet>();
Enemy enemy = new Enemy();
BasicTower basicTower = new BasicTower();
public Shape bulletCircle = null;
public boolean collides = false;
public int bulletCount;

public Bullets() throws SlickException {
}


@Override
public Vector2f getPosition() {
    return null;
}

@Override
public void init(GameContainer gc, StateBasedGame stateBasedGame) throws SlickException {
}

@Override
public void render(GameContainer gc, StateBasedGame stateBasedGame, Graphics g) throws SlickException {
    for (int i = 0; i < bulletList.size(); i++) {
        Bullet bullet = bulletList.get(i);
        bulletCircle = new Circle(bullet.location.getX(),bullet.location.getY(),10);
        if (bulletCircle.intersects( enemy.playerRectangle )){
            bulletCount++;
            bulletList.remove( i );
            collides = true;
        }else{
            collides = false;
        }
        g.setColor( red );
        g.fill(bulletCircle);
    }


}

@Override
public void update(GameContainer gc, StateBasedGame stateBasedGame, int delta) throws SlickException {
    //Update the bullet's position.
    Input input = gc.getInput();
    //enemy.update(gc, stateBasedGame, delta);
    for (int i = 0; i < bulletList.size(); i++) {
        Bullet bullet = bulletList.get(i);
        bullet.move();
    }
}

public void addNewBullet2(float x1, float y1, float x2, float y2) {
    bulletList.add(new Bullet(x1*64+48,y1*64+48, x2, y2));
}


class Bullet {
    float startX = 0;
    float startY = 0;
    float destX = 0;
    float destY = 0;
    Point location = new Point(0, 0);
    float speed; //how fast this moves.
    float dx;
    float dy;


    public Bullet(float startX, float startY, float destX, float destY) {
        this.startX = startX;
        this.startY = startY;
        location.setLocation(startX, startY);
        this.destX = destX;
        this.destY = destY;
        recalculateVector(destX, destY);
    }

    public void recalculateVector(float destX, float destY) {
        float rad = (float) (Math.atan2(destX - startX, startY - destY));
        //Can set different speeds here, if you wanted.
        speed = 5;
        this.dx = (float) Math.sin(rad) * speed;
        this.dy = -(float) Math.cos(rad) * speed;
    }

    public void move() {
        float x = location.getX();
        float y = location.getY();
        x += dx;
        y += dy;
        location.setLocation(x, y);
    }
}

你可能需要考虑每个敌人的速度。因此,如果你有更快或更慢的敌人,你可以使用敌人的速度来调整子弹的发射位置。你可以根据敌人的速度将子弹添加到敌人的X&Y中,而不仅仅是将子弹发送到敌人的X&Y中。这将使子弹在敌人前方更远一点,以补偿敌人的速度

这只是一个线性阿尔贝格拉问题。当你开始射击敌人时,你瞄准的是P0点。但是,你需要指出当子弹击中他们时他们会在什么时候。 所以你需要敌人的速度,子弹的速度,和射手的距离来获得正确点的坐标


但是,由于数学计算可能会变得很复杂,例如,如果目标没有沿着直线移动,你可以尝试使子弹沿着目标移动,这当然会导致曲线轨迹。

我的猜测是,每次射击时,你都必须重新计算位置,因此它会沿着目标移动。玩家只能单击敌人进行射击,或者他们有能力领导他们的投篮?