How to use mouse as view control of camera?

Started by
5 comments, last by Alundra 8 years, 4 months ago

Hey there, I'm currently making a game using DirectX11 with c++

I've managed to create key inputs to move the camera, forward,backwards,left,right,up,down.

However i'm struggling with making the mouse control. I need to use the mouse to view which direction the camera is facing.

So it doesn't need clicks its just whichever way you move the mouse with the cursor, the camera will follow.

Had no luck so far.

Any pointers or help would be appreciated thanks

Advertisement

You can treat mouse relative movements as changes to camera's pitch/yaw angles. Pitch should be limited (head up/down).

You can treat mouse relative movements as changes to camera's pitch/yaw angles. Pitch should be limited (head up/down).

Where can i find the mouse header files? I'm struggling to understand what calculation needs to be made, could you produce any psuedo code for me to follow?


Where can i find the mouse header files?

There's no separate include files for mouse. Input events is part of subsystem you're using to manage application windows.

Hey there, I'm currently making a game using DirectX11 with c++

I've managed to create key inputs to move the camera, forward,backwards,left,right,up,down.

However i'm struggling with making the mouse control. I need to use the mouse to view which direction the camera is facing.

So it doesn't need clicks its just whichever way you move the mouse with the cursor, the camera will follow.

Had no luck so far.

Any pointers or help would be appreciated thanks

This is not the best way but it is the easiest.

First, call GetCursorPos each frame and store that value. By comparing the result of the current and previous frame, you know how far the mouse moved. Divide this number by some fixed "sensitivity" constant, and apply that as a rotation to your camera matrix. Typically you will want "FPS style" camera control. That means that the mouse X coordinate rotates around the world Y axis, and the mouse Y coordinate rotates around the camera X axis. Then you need to compute the movement vectors in the camera's local coordinates. This is an excellent exercise in beginning linear algebra for games.

As a hint, the variables you need are:

LastFrameCursorPos

CurrentFrameCursorPos

CameraLookVector

CameraUpVector

CameraRightVector

CameraPosition

CameraViewMatrix

MouseSensitivity

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

You could simply rotate the Right (1, 0, 0), Up (0, 1, 0), Look (maybe 0, 0, 1 to look towards Z axis direction) vectors by the X/Y angles taken from cursor movement and build the view matrix from those :)

.:vinterberg:.

Here a code to manage sensitivity and smoothing :


// Compute the real x and y values by accounting for smoothing.
lastX = lastX * m_viewSmoothing + ( x * elapsed ) * ( 1.0f - m_viewSmoothing );
lastY = lastY * m_viewSmoothing + ( y * elapsed ) * ( 1.0f - m_viewSmoothing );

// Adjust the values for sensitivity.
float sensitivity = 1.0f / elapsed * m_viewSensitivity / 100.0f;
if( sensitivity > 4.0f )
  sensitivity = 4.0f;
if( sensitivity < m_viewSensitivity )
  sensitivity = m_viewSensitivity;
sensitivity = sensitivity / 4.0f;
lastX *= sensitivity;
lastY *= sensitivity;

Default values can be :


m_viewSmoothing = 0.5f;
m_viewSensitivity = 0.65f;

This topic is closed to new replies.

Advertisement