[英]SharpDX Camera Rotation is weird
I know SharpDX is no longer maintained, but i already bought a book so i wanted to make use of it.我知道 SharpDX 不再维护,但我已经买了一本书,所以我想利用它。
I want to make a game engine using SharpDX everything is working perfectly!我想使用 SharpDX 制作游戏引擎,一切正常!
Except that one thing i tried to get working over 3 weeks.除了一件事我试图让工作超过 3 周。
There is a weird thing going on with the camera rotation.相机旋转发生了一件奇怪的事情。
why am i able to rotate the camera on the Z axis?为什么我可以在 Z 轴上旋转相机?
and how do i fix it?我该如何解决?
Code:代码:
var Window = new System.Windows.Form();
var viewMatrix = Matrix.LookAtRH(new Vector3(), cameraTarget, cameraUp);
var lastX = 0;
var lastY = 0;
Window.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
lastX = e.X;
lastY = e.Y;
}
};
Window.MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
var yRotate = lastX - e.X;
var xRotate = lastY - e.Y;
lastY = e.Y;
lastX = e.X;
viewMatrix *= Matrix.RotationX(-xRotate * moveFactor);
viewMatrix *= Matrix.RotationY(-yRotate * moveFactor);
updateText();
}
};
Project: https://workupload.com/file/NANE9PbDJ24项目: https://workupload.com/file/NANE9PbDJ24
Help would be greatly appreciated!帮助将不胜感激!
We don't update the view matrix like that for several reasons.我们不会像那样更新视图矩阵有几个原因。
You should keep a rotationX, and rotationY and recalculate your view matrix at each modification.您应该保留一个rotationX 和rotationY 并在每次修改时重新计算您的视图矩阵。
Something like this:像这样的东西:
float rotationX = 0;
float rotationY = 0;
float dist = 1;
Window.MouseMove += (s, e) = >
{
if (e.Button == MouseButtons.Left)
{
var yRotate = lastX - e.X;
var xRotate = lastY - e.Y;
rotationX += xRotate;
rotationY += yRotate;
lastY = e.Y;
lastX = e.X;
float h = cos(rotationX) * dist;
Vector3 cameraTarget = Vector3(cos(rotationY) * h, sin(rotationX) * dist, sin(rotationY) * h);
viewMatrix = Matrix.LookAtRH(new Vector3(), cameraTarget, cameraUp);
updateText();
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.