我目前正在使用具有Viewport3D
相机的wpf应用程序。
我正在创建一个3D演练模块。
我希望我的PerspectiveCamera
根据Look Directions更改后面向的方向移动。
我正在使用键盘控制机芯,例如:
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;
}
旋转计算工作得很完美,但运动似乎不能正常工作,因为它会在旋转后根据摄像机位置而不是面向方向移动摄像机。
答案 0 :(得分:1)
注释掉的代码看起来或多或少......你没有说出它的错误。你试过规范化lookDirection向量吗?归一化矢量使其幅度(长度)为1。
public void Move(double d)
{
double u = 0.05;
PerspectiveCamera camera = (PerspectiveCamera)Viewport3D.Camera;
Vector3D lookDirection = camera.LookDirection;
Point3D position = camera.Position;
lookDirection.Normalize();
position = position + u * lookDirection * d;
camera.Position = position;
}
更新:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.NumPad6)
{
Rotate(10);
}
else if (e.Key == Key.NumPad4)
{
Rotate(-10);
}
else if (e.Key == Key.NumPad8)
{
Move(-10);
}
else if (e.Key == Key.NumPad2)
{
Move(10);
}
else if (e.Key == Key.PageUp)
{
RotateVertical(10);
}
else if (e.Key == Key.PageDown)
{
RotateVertical(-10);
}
}
public void Rotate(double d)
{
double u = 0.05;
double angleD = u * d;
PerspectiveCamera camera = (PerspectiveCamera)Viewport3D.Camera;
Vector3D lookDirection = camera.LookDirection;
var m = new Matrix3D();
m.Rotate(new Quaternion(camera.UpDirection, -angleD)); // Rotate about the camera's up direction to look left/right
camera.LookDirection = m.Transform(camera.LookDirection);
}
public void RotateVertical(double d)
{
double u = 0.05;
double angleD = u * d;
PerspectiveCamera camera = (PerspectiveCamera)Viewport3D.Camera;
Vector3D lookDirection = camera.LookDirection;
// Cross Product gets a vector that is perpendicular to the passed in vectors (order does matter, reverse the order and the vector will point in the reverse direction)
var cp = Vector3D.CrossProduct(camera.UpDirection, lookDirection);
cp.Normalize();
var m = new Matrix3D();
m.Rotate(new Quaternion(cp, -angleD)); // Rotate about the vector from the cross product
camera.LookDirection = m.Transform(camera.LookDirection);
}