0

オブジェクトを常にプレーヤーに向けようとしていますが、その背後にある数学に苦労しています。これまでのところ、オブジェクトはプレーヤーが移動しても回転しますが、一貫性がありません。

これは、オブジェクトを回転させるために使用するものです。

var targetX = player.x - this.width/2;
var targetY = player.y - this.height/2;

this.rotation = Math.atan2(targetY, targetX);
ctx.transform(1, 0, 0, 1, this.x, this.y);

//ART WORK
ctx.save()
    ctx.translate(15, 15);
    ctx.rotate(this.rotation);

    ctx.fillStyle = "866c4a";
    ctx.fillRect(-15, -15, this.width, this.height);
ctx.restore();

私が言ったように、それは正しく機能しません。それは私の数学に関係していると思います。

ありがとう

4

1 に答える 1

1

上手!!

私はすぐに問題が何であるかを知りました。私はオブジェクトの位置を考慮していませんでした:

var targetX = player.x - this.x;
var targetY = player.y - this.y;

this.rotation = Math.atan2(targetY, targetX);
ctx.transform(1, 0, 0, 1, this.x, this.y);

//ART WORK
ctx.save()
    ctx.translate(15, 15);
    ctx.rotate(this.rotation);

    ctx.fillStyle = "866c4a";
    ctx.fillRect(-15, -15, this.width, this.height);
ctx.restore();

他の誰かがこれに遭遇した場合に備えて、このQnAはそのままにしておきます。:)

于 2013-02-14T02:25:00.280 に答える