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.hand declare two floating-point numbers calledmoveXandmoveZ. Then, declare aQVector3Dvariable 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...