相机多次旋转
我正在尝试在 WebGL 应用程序中进行一些简单的相机旋转(使用学习 WebGL 中的第 10 课) ),但我肯定做错了什么。我的意思是相机的水平移动似乎不错,WASD 的移动似乎也可以,但是当我添加垂直移动时,在地图的某些点上,出现了问题并且地图开始倾斜。我的错误在哪里? (演示位于此处)
我正在做的是:
function handleMouseMove(event) {
var canvas = document.getElementById("quak-canvas");
var newX = event.clientX;
var newY = event.clientY;
var newRotationMatrix = mat4.create();
mat4.identity(newRotationMatrix);
var deltaY = newY - lastMouseY;
mat4.rotate(newRotationMatrix, degToRad(deltaY / 40), [1, 0, 0]);
var deltaX = newX - lastMouseX;
horizontalAngle = (event.pageX/(canvas.width/2))*180;
mat4.rotate(newRotationMatrix, degToRad(deltaX / 3.75), [0, 1, 0]);
mat4.multiply(newRotationMatrix, moonRotationMatrix, moonRotationMatrix);
lastMouseX = newX
lastMouseY = newY;
window.moveBy(10, 10);
}
我认为缺少一些翻译或类似的东西,但我尝试了一些组合,但没有成功..
非常感谢
谢尔希。
I'm trying to do some simple camera rotation in WebGL app(using lesson 10 from Learning WebGL), but I'm definetly doing something wrong.. I mean the horizontal movement of camera seems good,movement with WASD seems also OK, but when I'm adding the vertical movement, in some points of the map, something goes wrong and the map starts to incline. Where is my mistake? (demo is here)
what I'm doing is:
function handleMouseMove(event) {
var canvas = document.getElementById("quak-canvas");
var newX = event.clientX;
var newY = event.clientY;
var newRotationMatrix = mat4.create();
mat4.identity(newRotationMatrix);
var deltaY = newY - lastMouseY;
mat4.rotate(newRotationMatrix, degToRad(deltaY / 40), [1, 0, 0]);
var deltaX = newX - lastMouseX;
horizontalAngle = (event.pageX/(canvas.width/2))*180;
mat4.rotate(newRotationMatrix, degToRad(deltaX / 3.75), [0, 1, 0]);
mat4.multiply(newRotationMatrix, moonRotationMatrix, moonRotationMatrix);
lastMouseX = newX
lastMouseY = newY;
window.moveBy(10, 10);
}
I think there is some translate is missing or something like, but I have tried some combinations but it wasn't successfull..
Thanks a lot
Serhiy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我要说的是,你的演示实际上对我来说看起来还不错。也许这是垂直旋转减弱的副作用,但我没有看到任何看起来太糟糕的东西。您的代码在大多数情况下看起来也不错。我认为你问题的核心可能是最后的矩阵乘法。事实上,您总是在前一帧的结果的基础上进行构建,这可能会导致一些复杂的情况。
在我的 FPS 运动代码中,我重新计算每一帧的视图矩阵,如下所示:
当按下 WASD 时计算位置,如下所示:
希望这可以帮助您为自己的代码获得有效的解决方案!
First off, let me say that you're demo actually looks okay to me. Maybe it's a side effect of the dampened vertical rotation, but I don't see anything that looks too terribly off. Your code looks okay too for the most part. I think the core of your problem may be the matrix multiply at the end. The fact that you're always building off the previous frame's results can lead to some complications.
In my FPS movement code I re-calculate the view matrix every frame like so:
The position is calculated when WASD are pressed like so:
Hopefully that helps you get a working solution for your own code!