Answers:
只需将运动向量投影到法线平面上,然后从运动向量中减去结果即可。
Vector undesiredMotion = normal * (dotProduct(input, normal));
Vector desiredMotion = input - undesiredMotion;
反正就是这样。尽管在您的可爱图中,输入似乎远离墙壁,所以我有些困惑。
desiredMotion
结果确实很奇怪。你有没有做这个工作?
虽然直接删除法线方向上的运动分量很简单,但它可能适合您的游戏玩法来旋转运动矢量。例如,在第三人称动作游戏中,可能容易挂在墙壁和其他边界上,因此您可以对玩家的意图做出一些猜测。
// 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);
}
}