使用单位向量描述方向和使用点描述位置是有意义的。然后当汽车向前移动时
currentPosition = currentPosition + distance * directionvector;
//(伪代码)
改变方向时,最好使用矩阵来(旋转)变换方向向量。
(我对 XNA 不熟悉)
写了一些虚拟代码来说明:
[Test]
public void MoveForwardTest()
{
var position = new Point(0, 0);
var direction = new Vector(1, 0);
double distance = 5;
//Update position moving forward distance along direction
position = position + distance * direction;
Assert.AreEqual(5.0, position.X, 1e-3);
Assert.AreEqual(0, position.Y, 1e-3);
}
[Test]
public void RotateThenMoveTest()
{
var position = new Point(0, 0);
var direction = new Vector(1, 0);
double distance = 5;
//Create the rotation matrix
var rotateTransform = new RotateTransform(90);
//Apply the rotation to the direction
direction = Vector.Multiply(direction, rotateTransform.Value);
//Update position moving forward distance along direction
position = position + distance * direction;
Assert.AreEqual(0, position.X, 1e-3);
Assert.AreEqual(5.0, position.Y, 1e-3);
}
[Test]
public void CheckIfOtherCarIsInfrontTest()
{
var position = new Point(0, 0);
var direction = new Vector(1, 0);
var otherCarPosition = new Point(1, 0);
//Create a vector from current car to other car
Vector vectorTo = Point.Subtract(otherCarPosition, position);
//If the dotproduct is > 0 the other car is in front
Assert.IsTrue(Vector.Multiply(direction, vectorTo) > 0);
}
[Test]
public void AngleToNortTest()
{
var direction = new Vector(1, 0);
var northDirection = new Vector(0, 1);
Assert.AreEqual(90, Vector.AngleBetween(direction, northDirection), 1e-3);
}