Ich weiß, dass diese Frage auf verschiedene Arten beantwortet wurde, und meine aktuelle Lösung basiert auf einer früheren Antwort, aber es funktioniert nicht ganz für mich und ich kann nicht herausfinden Warum.Machen Sie Projektil zum Ziel im Spiel von Grund auf
float targetX = bullet.getTarget().x;
float targetY = bullet.getTarget().y;
float bulletX = bullet.getLocation().x;
float bulletY = bullet.getLocation().y;
// Version of setX(getX() + (speed * Math.cos(direction)));
float deltaX = targetX - bulletX;
float deltaY = targetY - bulletY;
float direction = (float) Math.atan2(deltaY, deltaX);
float x = bulletX + BULLET_SPEED * (float) Math.cos(direction);
float y = bulletY + BULLET_SPEED * (float) Math.sin(direction);
bullet.setLocation(x, y);
Hier die Ergebnisse in Aktion:
Animated Gif Of The Bullet Going The (Almost Right) Direction
Gibt es ein Problem mit meiner Mathe?