どうやら、私は微積分 3 を経験したにもかかわらず、私の三角関数はひどく失敗しています (おそらく、この三角関数を呼び出すことさえできません。それは単なる基本的な数学です)。基本的に、回転する画像がたくさんありますが、それを瞬時にしたくありません。回転を遅くする方法はわかりましたが、どのように回転するかを計算する方法がわかりません。
target
回転したい角度が 179 度未満の場合は、一方向に回転します。180 度を超えている場合は、反対方向に回してください。その言い方が正しいかどうかはわかりませんが、基本的には最短距離で回転させたいだけです。ホーミングミサイルを思い浮かべてください。
角度を見つけるには 2 つの方法があります。1 つの方法は、ここに示すようにベクトルを使用することです。
hero = new Vector2f(this.x, this.y);
target = new Vector2f(mouseX, mouseY);
target.sub(hero);
// Calculates the rotation
finalRotation = (int) target.getTheta();
もう 1 つの方法は次を使用することですatan2
(これはより単純に見え、1 つの return ステートメントに短縮できることに気付きました):
direction = Math.atan2(player.x - this.x, player.y - this.y);
return (int) Math.toDegrees(direction);
最短の回転を見つける方法は、両方で機能すると思います。私はしばらくの間、いくつかの試行錯誤を試みてきましたが、ただ困惑しています。私の価値のないプログラミングスキルを助けてください!どんな助けでも大歓迎です!
編集: 私が実際に言及するのを忘れていたのは、私がマウスを指しているということです. したがって、マウスが「画像」 (プレーヤー) から 200 度の角度にあり、プレーヤーが現在 19 を向いている場合、200 に正になるか、0 に下がるかをどのように判断し、360 に転送してから200。
二重編集:
私が望んでいたものの作業バージョンがありますが、何らかの理由で最終回転と現在の回転が 180 ずれており、キャラクターのけいれんが止まりません。明日はもっと頑張らないといけない。currentRotation
スプライトが元々回転しているため、最後にから 90 を引く必要があります。
トリプル編集: さて、インクリメントステートメントとデクリメントステートメントを逆にしました。したがって、180を超えてから方向を逆にしようとしていました。私はそれを完成させました!
mouseX = input.getMouseX();
mouseY = input.getMouseY();
hero = new Vector2f(this.x, this.y);
target = new Vector2f(mouseX, mouseY);
target.sub(hero);
// Calculates the rotation
finalRotation = (int) target.getTheta();
if(!(currentRotation <= finalRotation + 1 && currentRotation >= finalRotation - 1)) {
double tempVal;
if (currentRotation < finalRotation) {
tempVal = finalRotation - currentRotation;
if (tempVal > 180) {
currentRotation -= .3 * delta;
} else {
currentRotation += .3 * delta;
}
} else if (currentRotation > finalRotation) {
tempVal = currentRotation - finalRotation;
if (tempVal < 180) {
currentRotation -= .3 * delta;
} else {
currentRotation += .3 * delta;
}
}
}
if (currentRotation < 0)
currentRotation = 359;
if (currentRotation > 360)
currentRotation = 1;
this.setAngle((int) (currentRotation+90));
入力内容とベクトルは Slick2D からのものです。