私がしようとしているのは、マウスカーソルの位置に向かう円の位置です。これは、キャンバスを通して見た世界に関連しています。円が動く速度にハンドルを当てるために、私は円より大きな境界を作ることにしました。もしマウスが境界の外にあれば、マウスの "位置"が境界に持ち込まれます。彼らが円の位置からはるか遠くにいなければ、それは狂ったスピードで動かない。私はこれが働いており、これはそれを行うコードです:AからBへのアークコード
dx = Game.controls.mouseX - (this.x - xView); // get the distance between the x coords
dy = Game.controls.mouseY - (this.y - yView); // get the distance between the y coords
radii = this.radius + 1; // +1 because the "radius" of the mouse is 1
if((dx * dx) + (dy * dy) > radii * radii) // is the mouse not over the player?
{
if((dx * dx) + (dy * dy) < 301 * 301)
{
this.x += ((Game.controls.mouseX - (this.x - xView)) * 2/(this.mass)) + step;
this.y += ((Game.controls.mouseY - (this.y - yView)) * 2/(this.mass)) + step;
}
else
{
mx = Game.controls.mouseX;
my = Game.controls.mouseY;
do
{
dx = mx - (this.x - xView);
dy = my - (this.y - yView);
mx += (((this.x - xView) - mx) * 2/(this.mass)) + step;
my += (((this.y - yView) - my) * 2/(this.mass)) + step;
} while((dx * dx) + (dy * dy) > 301 * 301)
this.x += ((mx - (this.x - xView)) * 2/(this.mass)) + step;
this.y += ((my - (this.y - yView)) * 2/(this.mass)) + step;
}
}
「境界の外側」の魔法はdoの中にあります。これは私が思いつくことができる最高の修正であり、私はこれをエレガントで速い解決策と見なすことはできません。
Imアーティストはいませんが、この画像は私が達成しようとしていることを説明するのに役立ちます。黒い点はマウスの位置、黒い円は円、赤い円は私が指定した境界です。私はあなたがそれらの両方のためにあなたの中心点を使用できるように
これは完全にjavascriptで行われています。 – Rhexis
指定された半径の円(中心とマウスの座標を指定)の点座標を見つける必要はありますか? – MBo