Moving an object using keyboard controls
In this section, we will be looking at how to move an object in OpenGL using keyboard controls. Qt provides an easy way to detect keyboard events using virtual functions, namely keyPressEvent()
and keyReleaseEvent()
. We will be using the previous example and adding to it.
How to do it…
To move an object using keyboard controls, follow these steps:
- Open up
renderwindow.h
and declare two floating-point numbers calledmoveX
andmoveZ
. Then, declare aQVector3D
variable calledmovement
:QElapsedTimer* time; int currentTime = 0; int oldTime = 0; float deltaTime = 0; float rotation = 0; float moveX = 0; float moveZ = 0; QVector3D movement = QVector3D(0, 0, 0);
- We will also declare two functions called
keyPressEvent()
andkeyReleaseEvent...