0

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Hyndrix
  • 4,282
  • 7
  • 41
  • 82

1 Answers1

0

Simple solution:

m_position += m_mouseMovement.x*Vector3D(right.x, 0, right.z);
//m_position += m_mouseMovement.y*Vector3D(up.x, 0, up.z);
m_position += m_mouseMovement.z*Vector3D(direction.x, 0, direction.z);
Hyndrix
  • 4,282
  • 7
  • 41
  • 82