-1

私は HTML5 で自分のキャンバスにいくつかの座標関数に取り組んでおり、オブジェクトを度単位で移動できる関数を作成したいと考えています。
私の夢は、次のように機能する関数を作成することです。

box.x=10;
box.y=10;
// now the box has the coordinates (10,10)
moveTheBoxWithThisAmountOfDistance=10;
degreesToMoveTheBox=90;
box.moveByDegrees(moveTheBoxWithThisAmountOfDistance,degreesToMoveTheBox);
// now the box.x would be 20 and box.y wouldn't be changed because
// we only move it to the right (directional 90 degrees)

これが意味をなすことを願っています!
だから私の質問は次のとおり
です。度を座標に変換する必要がある場合、数式はどのように見えますか?

4

2 に答える 2

4

と を使用sincosて、角度と距離を座標に変換します。

function moveByDegrees(distance, angle) {
  var rad = angle * Math.pi / 180;
  this.x += Math.cos(rad) * distance;
  this.y += Math.sin(rad) * distance;
}
于 2012-06-09T16:51:28.267 に答える
1

それだけです: http://en.wikipedia.org/wiki/Rotation_matrix、すべての数学が説明されています:)

また、複数の順次回転が必要な場合 (つまり、連続的なアニメーションを行う場合) は、以前のものだけでなく、最初のものから x' と y' を再計算することをお勧めします。そうしないと、丸め誤差が蓄積され、数千回転後に結果が粗くなりすぎます。

于 2012-06-09T16:50:26.443 に答える