Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: 3D Game - When any arrow key is pressed the screen goes smaller then dissapears

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    12

    Exclamation 3D Game - When any arrow key is pressed the screen goes smaller then dissapears

    I have made a 3d game when any arrow buttons are pressed the ball is meant to travel that way it was working until i put the aim targets on the wall after that it decided to stop working, instead the screen goes small first time a arrow button is pressed then after second press screen goes all black why is this

    Click image for larger version. 

Name:	Screen Shot 2012-12-23 at 6.12.52 PM.jpg 
Views:	93 
Size:	20.4 KB 
ID:	929Click image for larger version. 

Name:	Screen Shot 2012-12-23 at 6.13.04 PM.jpg 
Views:	96 
Size:	10.1 KB 
ID:	930Click image for larger version. 

Name:	Screen Shot 2012-12-23 at 6.13.14 PM.jpg 
Views:	88 
Size:	5.7 KB 
ID:	931


    Heres some code that could help

    GLfloat ballSpin = 0.0f;

    GLfloat ballXpos = 40.0f;
    GLfloat ballYpos = -30.0;
    GLfloat ballZpos = 180.0;

    GLfloat ballZspeed = 8.0f;
    GLfloat ballXspeed = 2.0f;

    GLfloat tempXpos = 40.0f;
    GLfloat tempYpos = -30.0;
    GLfloat tempZpos = 180.0;


    void specialKeys(int key, int x, int y)
    {
    if(key == GLUT_KEY_UP){
    ballZpos = ballZpos - ballZspeed;
    ballSpin -= 90.0f;
    glutPostRedisplay();
    }

    if(key == GLUT_KEY_DOWN){
    ballZpos = ballZpos + ballZspeed;
    ballSpin += 90.0f;
    glutPostRedisplay();
    }


    if(key == GLUT_KEY_LEFT){
    ballXpos = ballXpos - ballXspeed;
    ballSpin += 45.0f;
    glutPostRedisplay();
    }

    if(key == GLUT_KEY_RIGHT){
    ballXpos = ballXpos + ballXspeed;
    ballSpin -= 45.0f;
    glutPostRedisplay();
    }
    }


    void RenderScene(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    sky(skylight);
    frontWall(bricks);
    leftWall(bricks);
    rightWall(bricks);
    floor(grass);

    eightPoints();
    sixPoints();
    fivePoints();

    glPushMatrix();
    glTranslatef(ballXpos, ballYpos, ballZpos);
    glRotatef(ballSpin, 1.0, 1.0, 1.0);
    ball(football);
    glPopMatrix();
    glFlush();

    glutSwapBuffers();

    }





    Also can anybody tell me how I would code this for the when i press the z key it should shoot the ball toward the z axis, and if i use any of the arrows it will aim towards that angle.
    Last edited by donmitz; 12-23-2012 at 11:25 AM. Reason: keys, shoot, angles, trouble, view

  2. #2
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    187
    Try adding a couple of lines to your RenderScene function - as shown below ...
    Code :
    void RenderScene(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();
     
        sky      (skylight);
        frontWall (bricks);
        leftWall   (bricks);
        rightWall (bricks);
        floor       (grass);
    .
    .
    .

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    12
    Yes that worked thanks, but i also found out another way.

    I was playing around with it and i put:
    eightPoints();
    sixPoints();
    fivePoints();

    between
    glPushMatrix();
    glTranslatef(ballXpos, ballYpos, ballZpos);

    and it also worked, do you know if there is any difference between the way i done it compared to your metod ??

    Thanks again

  4. #4
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    187
    Quote Originally Posted by donmitz View Post
    Yes that worked thanks, but i also found out another way.

    I was playing around with it and i put:
    eightPoints();
    sixPoints();
    fivePoints();

    between
    glPushMatrix();
    glTranslatef(ballXpos, ballYpos, ballZpos);

    and it also worked, do you know if there is any difference between the way i done it compared to your metod ??

    Thanks again
    What you are doing is different from what I'm doing.
    You are sort of lucky that your way works.
    It's a good idea to initialize the matrix stack each time you redraw a scene.
    If you don't, modeling transformations can compound on each redraw.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •