法線の方向にあるモーションのコンポーネントを削除するのは簡単ですが、代わりにゲームプレイに合わせてモーションベクトルを回転させることもできます。たとえば、3人称のアクションゲームでは、壁やその他の境界に少し引っ掛かるのが簡単な場合があるため、プレイヤーの意図を推測できます。
// assume that 'normal' is unit length
Vector GetDesired(Vector input, Vector normal) {
// tune this to get where you stop when you're running into the wall,
// vs. bouncing off of it
static float k_runningIntoWallThreshold = cos(DEG2RAD(45));
// tune this to "fudge" the "push away" from the wall
static float k_bounceFudge = 1.1f;
// normalize input, but keep track of original size
float inputLength = input.GetLength();
input.Scale(1.0f / inputLength);
float dot = DotProduct(input, normal);
if (dot < 0)
{
// we're not running into the wall
return input;
}
else if (dot < k_runningIntoWallThreshold)
{
Vector intoWall = normal.Scale(dot);
intoWall.Scale(k_bounceFudge);
Vector alongWall = (input - intoWall).Normalize();
alongWall.Scale(inputLength);
return alongWall;
}
else
{
// we ran "straight into the wall"
return Vector(0, 0, 0);
}
}