我正在写一个我正在制作的3D游戏的敌人类,并且正在努力让敌人跟随玩家。我希望敌人基本上在玩家的方向上每帧稍微旋转一下,然后每帧向前移动一下。我尝试使用Lerping来完成此操作,如下面的代码所示,但我似乎无法使其工作。在玩时,敌人甚至不会出现在我的视野中,也不会出现在我的视野中。下面是我的敌人级别下面的代码。C#和XNA - 如何使3D模型跟随使用lerp的另一个3D模型
注意:p是对我追逐的玩家对象的引用,world是敌方物体的世界矩阵,quaternion是这个敌方物体的四元数。
我目前的策略是找到我的敌人的前向矢量和玩家的位置矢量3之间的方向矢量,然后用由速度变量确定的数量来确定方向矢量。然后,我尝试找到由敌人的前向矢量确定的平面的垂直矢量,并且将新的矢量称为midVector。然后,我更新四元数以使玩家围绕该垂直矢量旋转。这是下面的代码:
//here I get the direction vector in between where my enemy is pointing and where the player is located at
Vector3 midVector = Vector3.Lerp(Vector3.Normalize(world.Forward), Vector3.Normalize(Vector3.Subtract(p.position,this.position)), velocity);
//here I get the vector perpendicular to this middle vector and my forward vector
Vector3 perp=Vector3.Normalize(Vector3.Cross(midVector, Vector3.Normalize(world.Forward)));
//here I am looking at the enemy's quaternion and I am trying to rotate it about the axis (my perp vector) with an angle that I determine which is in between where the enemy object is facing and the midVector
quaternion = Quaternion.CreateFromAxisAngle(perp, (float)Math.Acos(Vector3.Dot(world.Forward,midVector)));
//here I am simply scaling the enemy's world matrix, implementing the enemy's quaternion, and translating it to the enemy's position
world = Matrix.CreateScale(scale) * Matrix.CreateFromQuaternion(quaternion) * Matrix.CreateTranslation(position);
//here i move the enemy forward in the direciton that it is facing
MoveForward(ref position, quaternion, velocity);
}
private void MoveForward(ref Vector3 position, Quaternion rotationQuat, float speed)
{
Vector3 addVector = Vector3.Transform(new Vector3(0, 0, -1), rotationQuat);
position += addVector * speed;
}
我的问题是两个,有什么问题我目前的策略/实现,是有更简单的办法,我做到这一点(第一部分是更重要的)?
这是有道理的,并帮助了。我的点积使用仍然存在一些问题,因为有时它会导致对象抖动并感到困惑。我认为它与反余弦域有关,但不确定。 – computerscience32