我目前正在使用带有Viewport3D
摄像头的 wpf 应用程序。我正在创建一个 3D 演练模块。我希望我PerspectiveCamera
在外观方向改变后根据它所面临的方向移动。我正在使用我的键盘来控制移动,例如::
NUMPAD8
向前
NUMPAD2
移动:向后移动
NUMPAD4
:向左
NUMPAD6
旋转:向右旋转
下面这个方法是检测键盘事件:
private void button20_KeyDown(Object sender, KeyEventArgs e)
{
if (e.Key == Key.NumPad6)
{
Rotate(1);
}
else if (e.Key == Key.NumPad4)
{
Rotate(-1);
}
else if (e.Key == Key.NumPad8)
{
Move(-10);
}
else if (e.Key == Key.NumPad2)
{
Move(10);
}
}
这是计算旋转:
public void Rotate(double d)
{
double u = 0.05;
double angleD = u * d;
PerspectiveCamera camera = (PerspectiveCamera)Viewport3D.Camera;
Vector3D lookDirection = camera.LookDirection;
double L = lookDirection.Length;
double D = 2 * L * Math.Sin(angleD / 2);
double m = Math.Sqrt(lookDirection.X * lookDirection.X + lookDirection.Y * lookDirection.Y);
double angleA = 2 * Math.Asin(D / (2 * m));
double x = lookDirection.X;
double y = lookDirection.Y;
double angleB = Math.Atan2(y, x);
double angleG = angleB - angleA;
double newx = m * Math.Cos(angleG);
double newy = m * Math.Sin(angleG);
Vector3D NewlookDirection = new Vector3D(newx, newy, lookDirection.Z);
camera.LookDirection = NewlookDirection;
}
这是计算相机运动:
public void Move(double d)
{
double u = 0.05;
PerspectiveCamera camera = (PerspectiveCamera)Viewport3D.Camera;
Vector3D lookDirection = camera.LookDirection;
Point3D position = camera.Position;
//Point3D NewPosition = new Point3D();
position.Y += d;
//NewPosition.X = position.X + u * lookDirection.X * d;
//NewPosition.Y = position.Y + u * lookDirection.Y * d;
//NewPosition.Z = position.Z + u * lookDirection.Z * d;
//camera.Position = NewPosition;
camera.Position = position;
}
旋转计算工作完美,但移动似乎效果不佳,因为它根据相机位置而不是旋转后的面向方向移动相机。