Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:56:10+00:00 2026-06-16T01:56:10+00:00

I am trying to implement a game using opengl in qt4. So far I

  • 0

I am trying to implement a game using opengl in qt4. So far I have created the football pitch and I am now trying to implement a camera with which the user can move in the world freely using the arrow keys. My friend used a piece of code he found on NeHe’s tutorials and simply copy pasted it to his code and the camera worked for him. When I tried the same only the escape button works and it just closes the opengl widget. f1 key is supposed to switch to fullscreen but it just makes the mouse cursor invisible without switching to fullscreen mode. The arrow keys don’t work at all. As I’m new to opengl I could not figure out what is wrong with the implementation.

I’m adding the code where I draw the pitch and also the keyboard event handlers.

void metinalifeyyaz::paintGL(){

movePlayer();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

GLfloat xtrans = -xpos;
GLfloat ytrans = -walkbias - 0.50f;
GLfloat ztrans = -zpos;
GLfloat sceneroty = 360.0f - yrot;

glLoadIdentity();
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
glTranslatef(xtrans, ytrans+50, ztrans-130);

glLoadIdentity();
glTranslatef(1.0f,0.0f,-18.0f);
glRotatef(45,1,0,0);
drawScene();

int delay = time.msecsTo(QTime::currentTime());
if (delay == 0)
    delay = 1;
time = QTime::currentTime();
timer->start(qMax(0,10 - delay));

}


void metinalifeyyaz::movePlayer() {

if (keyUp) {
    xpos -= sin(yrot * PI_OVER_180) * 0.5f;
    zpos -= cos(yrot * PI_OVER_180) * 0.5f;
    if (walkbiasangle >= 360.0f)
        walkbiasangle = 0.0f;
    else
        walkbiasangle += 7.0f;
    walkbias = sin(walkbiasangle * PI_OVER_180) / 10.0f;
} else if (keyDown) {
    xpos += sin(yrot * PI_OVER_180)*0.5f;
    zpos += cos(yrot * PI_OVER_180)*0.5f ;
    if (walkbiasangle <= 7.0f)
        walkbiasangle = 360.0f;
    else
        walkbiasangle -= 7.0f;
    walkbias = sin(walkbiasangle * PI_OVER_180) / 10.0f;
}

if (keyLeft)
    yrot += 0.5f;
else if (keyRight)
    yrot -= 0.5f;

if (keyPageUp)
    lookupdown -= 0.5;
else if (keyPageDown)
    lookupdown += 0.5;
}


void metinalifeyyaz::keyPressEvent(QKeyEvent *event) {

switch (event->key()) {
case Qt::Key_Escape:
    close();
    break;
case Qt::Key_F1:
    setWindowState(windowState() ^ Qt::WindowFullScreen);
    break;
default:
    QGLWidget::keyPressEvent(event);
case Qt::Key_PageUp:
    keyPageUp = true;
    break;
case Qt::Key_PageDown:
    keyPageDown = true;
    break;
case Qt::Key_Left:
    keyLeft = true;
    break;
case Qt::Key_Right:
    keyRight = true;
    break;
case Qt::Key_Up:
    keyUp = true;
    break;
case Qt::Key_Down:
    keyDown = true;
    break;

}
}

void metinalifeyyaz::changeEvent(QEvent *event) {

switch (event->type()) {
case QEvent::WindowStateChange:

    if (windowState() == Qt::WindowFullScreen)
        setCursor(Qt::BlankCursor);
    else
        unsetCursor();
    break;
default:
    break;
}
}
void metinalifeyyaz::keyReleaseEvent(QKeyEvent *event) {

switch (event->key()) {
case Qt::Key_PageUp:
    keyPageUp = false;
    break;
case Qt::Key_PageDown:
    keyPageDown = false;
    break;
case Qt::Key_Left:
    keyLeft = false;
    break;
case Qt::Key_Right:
    keyRight = false;
    break;
case Qt::Key_Up:
    keyUp = false;
    break;
case Qt::Key_Down:
    keyDown = false;
    break;
default:
    QGLWidget::keyReleaseEvent(event);
}
}

I know that copy paste is not an efficient method but my friend’s project is not different than mine and it works for him. If you know anything that might cause the same code to work on one project and not the other please point it out. Of course any other comment about the code are much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:56:12+00:00Added an answer on June 16, 2026 at 1:56 am

    Look:

    glLoadIdentity();
    glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
    glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
    glTranslatef(xtrans, ytrans+50, ztrans-130);
    
    glLoadIdentity(); // get rid of this!
    glTranslatef(1.0f,0.0f,-18.0f);
    glRotatef(45,1,0,0);
    drawScene();
    

    “glLoadIdentity” resets the current matrix. In your code, you rotate and translate the matrix, but afterward, you reset the matrix by calling “glLoadIdentity”, so the previous matrix transformations do nothing.

    Basically this is your code:

    • OpenGL, reset the matrix.
    • OpenGL, move the player and camera.
    • OpenGL, reset the matrix, and forget anything I just did.
    • OpenGL, do… whatever that is.
    • OpenGL, draw my scene!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a problem I hit when trying to implement a game using the
I am trying to implement a lighting effect in an HTML5/JavaScript game using tile
I've been trying to implement a simple component-based game object architecture using Objective-C, much
I have been trying to implement a GlassPane for my games' in-game HUD, but
I am trying to implement a game using the iPhone OS 4.0 (iOS4?) SDK.
I was trying to implement the Sorry! board game using C++ such that 4
trying to implement a multiplayer. Using the sample from Game Center - Sending and
I'm writing Snake in C++, using OpenGL and GLFW. I'm trying to implement a
I am trying to implement Accelerometer in my game using Andengine and i also
I'm trying to create a game loop using this tutorial . I have tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.