I want to move around left/right, forward/backward in a terrain (delta x, delta y in m_mouseMovement
) with respect to the current viewing direction. The following code works with zooming (mouse wheel). But movement works only as long as the viewing direction is exactly along the x-axis:
Vector3D m_position;
Vector2D m_mouseMovement; // delta x,y amount the mouse was moved on the screen
int m_mouseWheelSteps;
QVector3D direction(...);
QVector3D right(...);
QVector3D up = QVector3D::crossProduct(right, direction);
m_position += m_mouseWheelSteps * direction; // zoom in and out; ok
// m_position += Vector3D(m_mouseMovement.x*right.x, 0, m_mouseMovement.z*right.z); // does not work properly; it always moves along the x/z axis
QMatrix4x4 modelMatrix;
//modelMatrix.scale, translate, etc. the terrain
QMatrix4x4 viewMatrix;
viewMatrix.lookAt(m_position, m_position+direction, up);
QMatrix4x4 mvMatrix = viewMatrix * modelMatrix;
QMatrix4x4 projectionMatrix;
projectionMatrix.perspective(...);
QMatrix4x4 mvpMatrix = projectionMatrix*mvMatrix;
I cannot get my head around how to get this working so that the movement is independent of the viewing direction?