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 2 of 2

Thread: Boucing Ball on key press

  1. #1
    Newbie Newbie
    Join Date
    Sep 2014
    Posts
    1

    Boucing Ball on key press

    I have to develop a small application in which a ball moves from left to right.
    1. The application should run only for 3 minutes.And the speed of the ball should be increased with every minute.
    2. On key press like up button the ball should jump
    I learnt that for time glutTimer function has to be used and for keypress jump i have to use keypressed event and just have to play with coordinates of ball.
    But i don't know how to write the code for above 2 things?

    Here is the following code which i have written:

    #include <GL/glut.h>
    #include <GL/gl.h>
    #include <GL/glu.h>



    #define ESCAPE 27


    int window;
    static int flag=1;

    float rtri = 0.10f;
    void InitGL(int Width, int Height)
    {

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);


    glMatrixMode(GL_MODELVIEW);
    }


    void ReSizeGLScene(int Width, int Height)
    {
    if (Height==0)
    Height=1;

    glViewport(0, 0, Width, Height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    }

    float ballX = -0.8f;
    float ballY = -0.3f;
    float ballZ = -1.2f;

    void drawBall(void) {

    glColor3f(0.0, 1.0, 0.0);
    glTranslatef(ballX,ballY,ballZ);
    glutSolidSphere (0.3, 20, 20);
    glTranslatef(ballX+1.5,ballY,ballZ);
    }



    void DrawGLScene()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(rtri,0.0f,-6.0f);
    drawBall();
    rtri+=0.01f;
    if(rtri>3)
    rtri=-3.0f;
    glutSwapBuffers();
    }


    void keyPressed(unsigned char key, int x, int y)
    {

    }



    int main(int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(0, 0);
    window = glutCreateWindow("Ball Game");
    glutDisplayFunc(&DrawGLScene);
    glutIdleFunc(&DrawGLScene);
    glutReshapeFunc(&ReSizeGLScene);
    glutKeyboardFunc(&keyPressed);
    InitGL(640, 480);
    // glutTimerFunc(25, update, 0);
    glutMainLoop();

    return 1;
    }

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    392
    That's a big ask. Couple of things. It's not recommended to use your display function as your idle function. What happens when U run the code U posted? As an intermediate step I'd forget the animation part of the assignment. Try to simply move the ball in response to pushing a key on the keyboard. Once you have that working you can move on to idle functions and animation.

    Good luck.

Posting Permissions

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