如何让相机在WebGL中沿着视图方向移动?

时间:2016-10-15 20:24:02

标签: javascript graphics webgl

我试图在WebGL中使用键盘事件处理程序编写飞行模拟器。在各种旋转之后,相机应始终沿着视图方向移动。

我试图通过在旋转整个mvMatrix后调用此向量来保持速度向量并调用

mat4.translate(mvMatrix, mvMatrix, velocity);

让它以恒定速度移动并且确实有效。

然而,在我滚动和倾斜之后,它仍然沿着发起的方向移动。

有没有办法解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

相机的视图方向应为Z轴。 (如果你坚持使用gl-matrix惯例)。

// on init
// -------
var camMatrix = mat4.create()// the camera matrix

// zAxis : third row (z) of cam matrix;
var zAxis    = new Float32Array( camMatrix.buffer, 32, 3 );
// position : fourth row
var position = new Float32Array( camMatrix.buffer, 48, 3 );

// on update
// ---------
var velocity = 3.0 // scalar value

// move the cam along zaxis
vec3.scaleAndAdd( position, position, zAxis,  velocity );