Moving an object using keyboard controls

In this topic we'll be looking at is 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…

  1. Open up mainwindow.h and declare two floating point numbers called moveX and moveZ:
    private:
      QOpenGLContext* context;
      QOpenGLFunctions* openGLFunctions;
    
      float rotation;
      GLuint texID[1];
    
      float moveX;
      float moveZ;
    
  2. After that, declare the keyPressEvent() function, like so:
    public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
    
      void keyPressEvent(QKeyEvent *event);
    
  3. Then, open up mainwindow.cpp and set the default values for the two variables we just declared:
    MainWindow::MainWindow(QWidget *parent)
    {
      setSurfaceType(QWindow::OpenGLSurface);
    
      QSurfaceFormat format;
      format.setProfile(QSurfaceFormat::CompatibilityProfile);
      format.setVersion(2, 1); // OpenGL 2.1
      setFormat(format);
    
      context = new QOpenGLContext;
      context->setFormat(format);
      context->create();
      context->makeCurrent(this);
    
      openGLFunctions = context->functions();
    
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(updateAnimation()));
      timer->start(100);
    
      rotation = 0;
    
      moveX = 0;
      moveZ = 0;
    }
  4. Next, we will implement the keyPressEvent() function:
    void MainWindow::keyPressEvent(QKeyEvent *event)
    {
      if (event->key() == Qt::Key_W)
      {
        moveZ -= 0.2;
      }
    
      if (event->key() == Qt::Key_S)
      {
        moveZ += 0.2;
      }
    
      if (event->key() == Qt::Key_A)
      {
        moveX -= 0.2;
      }
    
      if (event->key() == Qt::Key_D)
      {
        moveX += 0.2;
      }
    }
  5. After that, we call glTranslatef() before drawing the 3D cube and putting both moveX and moveZ into the function. Also, we disabled the rotation so that it's easier to see the movement:
    // Transformations
    glTranslatef(0.0, 0.0, -3.0);
    glRotatef(rotation, 1.0, 1.0, 1.0);
    glTranslatef(moveX, 0.0, moveZ);
    
    // Texture mapping
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texID[0]);
    
    glEnable(GL_LIGHTING);
  6. If you compile and run the program now, you should be able to move the cube around by pressing W, A, S and D:
    How to do it…

How it works...

Basically, what we did here was add or subtract the moveX and moveZ values when a key is pressed. In keyPressEvent(), we checked whether the keyboard button pressed was W, A, S, or D. Then, we add or subtract 0.2 from the variables accordingly. To get the full list of key names used by Qt, visit http://doc.qt.io/qt-5/qt.html#Key-enum.

When we hold down the same key and don't release it, Qt will repeat the key press event after an interval. The keyboard input interval varies between different operating systems. You can set the interval by calling QApplication::setKeyboardInterval(), but this may not work in every operating system. We called glTranslatef(moveX, 0.0, moveZ) before drawing the cube, which moves the cube around when we press W, A, S, or D.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86