問題のコードは次のとおりです。
public void calculate() {
// Center of circle is at (250, 250).
//THIS ALGORITHM IS NOW PROVEN TO BE WORSE THAN I FEARED...
/* What it does:
* Moves object around in a circle.
* Does not move the object towards the center.
* Object always stays on the rim of the circle.
*
* Algorithm I used. (DOES NOT WORK):
* N is normalized vector.
* R = -2*(V dot N)*N + V
*/
vx += Accelero.X * 0.1;
vy += Accelero.Y * 0.1;
double nx = x - 250;
double ny = y - 250;
double nd = Math.hypot(nx, ny);
if (nd == 0)
nd = 1;
nx /= nd;
ny /= nd;
double dotProduct = vx * nx + vy * ny;
vx += (float) (-2 * dotProduct * nx);
vy += (float) (-2 * dotProduct * ny);
x -= vx * 2;
y -= vy * 2;
vx *= 0.99;
vy *= 0.99;
}
そして、これが起こることです。
表示されている黒い線は、紫色のオブジェクト (ボックス) が移動する場所です。たまたま で描いた円の線上にあっただけCanvas.drawCircle()
です。
リフレクションが機能しなかった理由がわかりません。オブジェクトが円形の壁に衝突する場合、オブジェクトの速度の方向を反映するべきではありませんか?これは、アルゴリズムが意図したものです? または、間違ったアルゴリズムを使用しましたか?
どんな助けでも大歓迎です。前もって感謝します。