gpt4 book ai didi

java - 塔防 : bullet math

转载 作者:行者123 更新时间:2023-12-02 11:16:18 28 4
gpt4 key购买 nike

我正在使用 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);
}
}

最佳答案

这里只是一个线性代数问题。您的目标是 P0 点,即您开始射击敌人时所在的位置。但相反,你需要指出子弹到达他们的时间。所以你需要敌人的速度、子弹的速度和射击者的距离来获得正确点的坐标。

但是,由于数学可能会变得棘手(例如,如果目标不沿着一条线移动),您可以尝试让子弹跟随目标,这当然会导致曲线轨迹。

关于java - 塔防 : bullet math,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50255929/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com