Thread: OpenGL animation problem

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    OpenGL animation problem

    Hi, there Im new to this forum and I came here to look for some answers regarding my code for a school project. I'm new to opengl programming so I dont know where the problem might be even after about half an hour of searching.

    So here is my problem: There are six platform in this game and there is two elevators which are controlled by two players the key 'q' and 'a' moves the left one up and down, and 'o', 'l' keys do the same for the right one.

    I've managed to draw the objects of the world and now im trying to make the elevators move.

    To do this Ive created a simulateWorld function in which I split the elapsed time since the last simulation and do the following the number of time intervals i get: change the coordinates of the elevators based on the input and their velocity then call the glutPostRedisplay function.

    The problem is: if i call the Move() function of the elevators the program doesnt draw them. If I dont call them, the elevators are drawn(, but they dont move of course). Ive checked the variables' value during debug, they should be alright, they dont ascend through the ceiling.

    Any help would be appreciated. Thank you in advance.

    Here is the code:
    Code:
    //================================================== ===========================================
    // Szamitogepes grafika hazi feladat keret. Ervenyes 2011-tol.
    // A //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // sorokon beluli reszben celszeru garazdalkodni, mert a tobbit ugyis toroljuk.
    // A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat.
    // Tilos:
    // - mast "beincludolni", illetve mas konyvtarat hasznalni
    // - faljmuveleteket vegezni (printf is fajlmuvelet!)
    // - new operatort hivni
    // - felesleges programsorokat a beadott programban hagyni
    // - tovabbi kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
    // ---------------------------------------------------------------------------------------------
    // A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
    // es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
    // a hazibeado portal ad egy osszefoglalot.
    // ---------------------------------------------------------------------------------------------
    // A feladatmegoldasokban csak olyan gl/glu/glut fuggvenyek hasznalhatok, amelyek
    // 1. Az oran a feladatkiadasig elhangzottak ES (logikai AND muvelet)
    // 2. Az alabbi listaban szerepelnek:
    // Rendering pass: glBegin, glVertex[2|3]f, glColor3f, glNormal3f, glTexCoord2f, glEnd, glDrawPixels
    // Transzformaciok: glViewport, glMatrixMode, glLoadIdentity, glMultMatrixf, gluOrtho2D,
    // glTranslatef, glRotatef, glScalef, gluLookAt, gluPerspective, glPushMatrix, glPopMatrix,
    // Illuminacio: glMaterialfv, glMaterialfv, glMaterialf, glLightfv
    // Texturazas: glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glTexEnvi,
    // Pipeline vezerles: glShadeModel, glEnable/Disable a kovetkezokre:
    // GL_LIGHTING, GL_NORMALIZE, GL_DEPTH_TEST, GL_CULL_FACE, GL_TEXTURE_2D, GL_BLEND, GL_LIGHT[0..7]
    //
    // NYILATKOZAT
    // ---------------------------------------------------------------------------------------------
    // Nev : <VEZETEKNEV(EK)> <KERESZTNEV(EK)>
    // Neptun : <NEPTUN KOD>
    // ---------------------------------------------------------------------------------------------
    // ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
    // mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
    // A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
    // grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
    // informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
    // azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
    // a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
    // Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
    // negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
    //================================================== ===========================================
    
    #include <math.h>
    #include <stdlib.h>
    
    #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
    // MsWindows-on ez is kell
    #include <windows.h>
    #endif // Win32 platform
    
    #include <GL/gl.h>
    #include <GL/glu.h>
    // A GLUT-ot le kell tolteni: http://www.opengl.org/resources/libraries/glut/
    #include <GL/glut.h>
    
    #define new new_nelkul_is_meg_lehet_csinalni
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
    // Innentol modosithatod...
    
    #define g 0.1f
    #define epsilon 0.005f
    #define NoOfObstacles 12
    #define PI 3.14f
    #define elevacc 0.2f
    #define dt 500
    
    bool floatIsEqual(float a, float b){
    if(fabs(a-b)<epsilon)
    return true;
    else
    return false;
    }
    
    class coord{
    public:
    float x, y;
    coord(float x=0.0f, float y=0.0f):x(x), y(y){}
    ~coord(){}
    };
    
    class Snake;
    
    class Platform{
    public:
    coord a, b;
    
    Platform(coord a, coord b): a(a), b(b){}
    bool carry(){
    
    }
    bool bottom(){
    
    }
    void collision(){
    
    }
    ~Platform(){}
    };
    
    class Block: public Platform{
    public:
    coord c, d;
    
    Block(coord a, coord b, coord c, coord d): Platform(a, b), c(c), d(d) {}
    ~Block(){}
    };
    
    class Elevator: public Block{
    public:
    float v;
    bool up;
    
    Elevator(coord a, coord b, coord c, coord d, float v=0.0f, bool up=true): Block(a, b, c, d), v(v), up(up){}
    void Move(){
    if(up){
    if(a.y+v>1.0f || floatIsEqual(a.y+v, 1.0f)){
    a.y=1.0f;
    b.y=1.0f;
    c.y=0.99f;
    d.y=0.99f;
    v=0.0f;
    }
    else{
    a.y+=v;
    b.y+=v;
    c.y+=v;
    d.y+=v;
    v+=elevacc;
    }
    }
    else{
    if(a.y+v<-1.0f || floatIsEqual(a.y+v, -1.0f)){
    a.y=-0.99f;
    b.y=-0.99f;
    c.y=-1.0f;
    d.y=-1.0f;
    v=0.0f;
    }
    else{
    a.y+=v;
    b.y+=v;
    c.y+=v;
    d.y+=v;
    v+=elevacc;
    }
    }
    }
    ~Elevator(){}
    };
    
    class Obstacles{
    public:
    Platform *t[NoOfObstacles];
    
    Obstacles();
    ~Obstacles();
    };
    
    class Snake{
    public:
    float vx, vy, ay, A, x;
    coord a, b, c, d;
    
    Snake(coord a, coord b, coord c, coord d, float vx, float vy, float ay, float A, float x): a(a), b(b), c(c), d(d), vx(vx), vy(vy), ay(ay), A(A), x(x) {}
    bool death(){
    
    }
    void move(){
    
    }
    ~Snake(){}
    };
    
    Platform upperleft(coord(-1.0f, 0.33f), coord(-0.6f, 0.33f)),
    uppermid(coord(-0.2, 0.33f), coord(0.2f, 0.33f)),
    upperright(coord(0.6f, 0.33f), coord(1.0f, 0.33f)),
    lowerleft(coord(-1.0f, -0.33f), coord(-0.6f, -0.33f)),
    lowermid(coord(-0.2, -0.33f), coord(0.2f, -0.33f)),
    lowerright(coord(0.6f, -0.33f), coord(1.0f, -0.33f));
    
    Block top(coord(-1.0f, 1.0f), coord(1.0f, 1.0f), coord(1.0f, 0.99f), coord(-1.0f, 0.99f)),
    bottom(coord(-1.0f, -0.99f), coord(1.0f, -0.99f), coord(1.0f, -1.0f), coord(-1.0f, -1.0f)),
    left(coord(-1.0f, 1.0f), coord(-0.99f, 1.0f), coord(-0.99f, -1.0f), coord(-1.0f, -1.0f)),
    right(coord(0.99f, 1.0f), coord(1.0f, 1.0f), coord(1.0f, -1.0f), coord(0.99f, -1.0f));
    
    Elevator e[2]={Elevator(coord(-0.59f, 0.33f), coord(-0.21f, 0.33f), coord(-0.21f, 0.32f), coord(-0.59f, 0.32f), 0.0f, true),
    Elevator(coord(0.21f, 0.33f), coord(0.59f, 0.33f), coord(0.59f, 0.32f), coord(0.21f, 0.32f), 0.0f, true)};
    
    Snake s[2]={Snake(coord(-0.9f, -0.87f), coord(-0.6f, -0.87f), coord(-0.6f, -0.99f), coord(-0.9f, -0.99f), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f),
    Snake(coord(0.6f, 0.45f), coord(0.9f, 0.45f), coord(0.9f, 0.33f), coord(0.6f, 0.33f), -1.0f, 0.0f, 0.0f, 0.0f, 0.0f)};
    
    long oldtime=0;
    
    void simulateWorld(long tstart, long tend){
    for(long ts=tstart; ts<tend; ts+=dt){
    float te=min(tend, ts+dt);
    //e[0].Move();
    //e[1].Move();
    glutPostRedisplay();
    }
    }
    
    // Inicializacio, a program futasanak kezdeten, az OpenGL kontextus letrehozasa utan hivodik meg (ld. main() fv.)
    void onInitialization( ) {
    
    
    }
    
    // Rajzolas, ha az alkalmazas ablak ervenytelenne valik, akkor ez a fuggveny hivodik meg
    void onDisplay( ) {
    glClearColor(0.1f, 0.2f, 0.3f, 1.0f); // torlesi szin beallitasa
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // kepernyo torles
    glColor3f(1.0f, 1.0f, 1.0f);
    glBegin(GL_LINES);
    glVertex2f(upperleft.a.x, upperleft.a.y);
    glVertex2f(upperleft.b.x, upperleft.b.y);
    glVertex2f(uppermid.a.x, uppermid.a.y);
    glVertex2f(uppermid.b.x, uppermid.b.y);
    glVertex2f(upperright.a.x, upperright.a.y);
    glVertex2f(upperright.b.x, upperright.b.y);
    glVertex2f(lowerleft.a.x, lowerleft.a.y);
    glVertex2f(lowerleft.b.x, lowerleft.b.y);
    glVertex2f(lowermid.a.x, lowermid.a.y);
    glVertex2f(lowermid.b.x, lowermid.b.y);
    glVertex2f(lowerright.a.x, lowerright.a.y);
    glVertex2f(lowerright.b.x, lowerright.b.y);
    glEnd();
    glBegin(GL_QUADS);
    glVertex2f(top.a.x, top.a.y);
    glVertex2f(top.b.x, top.b.y);
    glVertex2f(top.c.x, top.c.y);
    glVertex2f(top.d.x, top.d.y);
    glVertex2f(bottom.a.x, bottom.a.y);
    glVertex2f(bottom.b.x, bottom.b.y);
    glVertex2f(bottom.c.x, bottom.c.y);
    glVertex2f(bottom.d.x, bottom.d.y);
    glVertex2f(left.a.x, left.a.y);
    glVertex2f(left.b.x, left.b.y);
    glVertex2f(left.c.x, left.c.y);
    glVertex2f(left.d.x, left.d.y);
    glVertex2f(right.a.x, right.a.y);
    glVertex2f(right.b.x, right.b.y);
    glVertex2f(right.c.x, right.c.y);
    glVertex2f(right.d.x, right.d.y);
    glVertex2f(e[0].a.x, e[0].a.y);
    glVertex2f(e[0].b.x, e[0].b.y);
    glVertex2f(e[0].c.x, e[0].c.y);
    glVertex2f(e[0].d.x, e[0].d.y);
    glVertex2f(e[1].a.x, e[1].a.y);
    glVertex2f(e[1].b.x, e[1].b.y);
    glVertex2f(e[1].c.x, e[1].c.y);
    glVertex2f(e[1].d.x, e[1].d.y);
    for(int i=0; i<2; i++){
    if(i==0)
    glColor3f(0.0f, 1.0f, 0.0f);
    else
    glColor3f(1.0f, 0.0f, 0.0f);
    if(s[i].vx<0.0f){
    glVertex2f(s[i].a.x, (s[i].a.y+s[i].d.y)/2);
    glVertex2f(s[i].a.x+0.03f, (s[i].a.y+s[i].d.y)/2+0.02f);
    glVertex2f(s[i].a.x+0.06f, (s[i].a.y+s[i].d.y)/2);
    glVertex2f(s[i].a.x+0.03f, (s[i].a.y+s[i].d.y)/2-0.02f);
    }
    else{
    glVertex2f(s[i].b.x-0.06f, (s[i].a.y+s[i].d.y)/2);
    glVertex2f(s[i].b.x-0.03f, (s[i].a.y+s[i].d.y)/2+0.02f);
    glVertex2f(s[i].b.x, (s[i].a.y+s[i].d.y)/2);
    glVertex2f(s[i].b.x-0.03f, (s[i].a.y+s[i].d.y)/2-0.02f);
    }
    }
    glEnd();
    for(int i=0; i<2; i++){
    if(i==0)
    glColor3f(0.0f, 1.0f, 0.0f);
    else
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);
    if(s[i].vx<0.0f){
    for(float j=0.0f; j<=0.24f; j+=0.005f)
    glVertex2f(s[i].a.x+0.06f+j, ((s[i].a.y+s[i].d.y)/2)+0.06f*sin(4.0f*PI*(j/0.24f)));
    }
    else{
    for(float j=0.0f; j<=0.24f; j+=0.005f)
    glVertex2f(s[i].b.x-0.06f-j, ((s[i].a.y+s[i].d.y)/2)+0.06f*sin(4.0f*PI*(j/0.24f)));
    }
    glEnd();
    }
    glutSwapBuffers();
    
    // ...
    
    //glutSwapBuffers(); // Buffercsere: rajzolas vege
    
    }
    
    // Billentyuzet esemenyeket lekezelo fuggveny
    void onKeyboard(unsigned char key, int x, int y) {
    if(key=='q')
    e[0].up=true;
    if(key=='a')
    e[0].up=false;
    if(key=='o')
    e[1].up=true;
    if(key=='l')
    e[1].up=false;
    glutPostRedisplay();
    }
    
    // Eger esemenyeket lekezelo fuggveny
    void onMouse(int button, int state, int x, int y) {
    
    }
    
    // `Idle' esemenykezelo, jelzi, hogy az ido telik, az Idle esemenyek frekvenciajara csak a 0 a garantalt minimalis ertek
    void onIdle( ) {
    long time = glutGet(GLUT_ELAPSED_TIME); // program inditasa ota eltelt ido
    simulateWorld(oldtime, time);
    oldtime=time;
    }
    
    // ...Idaig modosithatod
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
    
    // A C++ program belepesi pontja, a main fuggvenyt mar nem szabad bantani
    int main(int argc, char **argv) {
    glutInit(&argc, argv); // GLUT inicializalasa
    glutInitWindowSize(600, 600); // Alkalmazas ablak kezdeti merete 600x600 pixel
    glutInitWindowPosition(100, 100); // Az elozo alkalmazas ablakhoz kepest hol tunik fel
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // 8 bites R,G,B,A + dupla buffer + melyseg buffer
    
    glutCreateWindow("Grafika hazi feladat"); // Alkalmazas ablak megszuletik es megjelenik a kepernyon
    
    glMatrixMode(GL_MODELVIEW); // A MODELVIEW transzformaciot egysegmatrixra inicializaljuk
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION); // A PROJECTION transzformaciot egysegmatrixra inicializaljuk
    glLoadIdentity();
    
    onInitialization(); // Az altalad irt inicializalast lefuttatjuk
    
    glutDisplayFunc(onDisplay); // Esemenykezelok regisztralasa
    glutMouseFunc(onMouse);
    glutIdleFunc(onIdle);
    glutKeyboardFunc(onKeyboard);
    
    glutMainLoop(); // Esemenykezelo hurok
    
    return 0;
    }
    Last edited by Salem; 09-20-2011 at 06:43 AM. Reason: Restore original and lock

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Having parameter names the same as member names presents a scope/shadowing problem.
    You're almost certainly not updating what you think you are.

    Code:
    $ g++ -c -Wall -Wshadow bar.cpp
    bar.cpp: In constructor ‘coord::coord(float, float)’:
    bar.cpp:36: warning: declaration of ‘y’ shadows a member of 'this'
    bar.cpp:36: warning: declaration of ‘x’ shadows a member of 'this'
    bar.cpp: In constructor ‘Platform::Platform(coord, coord)’:
    bar.cpp:46: warning: declaration of ‘b’ shadows a member of 'this'
    bar.cpp:46: warning: declaration of ‘a’ shadows a member of 'this'
    bar.cpp: In member function ‘bool Platform::carry()’:
    bar.cpp:49: warning: no return statement in function returning non-void
    bar.cpp: In member function ‘bool Platform::bottom()’:
    bar.cpp:52: warning: no return statement in function returning non-void
    bar.cpp: In constructor ‘Block::Block(coord, coord, coord, coord)’:
    bar.cpp:63: warning: declaration of ‘d’ shadows a member of 'this'
    bar.cpp:63: warning: declaration of ‘c’ shadows a member of 'this'
    bar.cpp:63: warning: declaration of ‘b’ shadows a member of 'this'
    bar.cpp:63: warning: declaration of ‘a’ shadows a member of 'this'
    bar.cpp: In constructor ‘Elevator::Elevator(coord, coord, coord, coord, float, bool)’:
    bar.cpp:72: warning: declaration of ‘up’ shadows a member of 'this'
    bar.cpp:72: warning: declaration of ‘v’ shadows a member of 'this'
    bar.cpp:72: warning: declaration of ‘d’ shadows a member of 'this'
    bar.cpp:72: warning: declaration of ‘c’ shadows a member of 'this'
    bar.cpp:72: warning: declaration of ‘b’ shadows a member of 'this'
    bar.cpp:72: warning: declaration of ‘a’ shadows a member of 'this'
    bar.cpp: In constructor ‘Snake::Snake(coord, coord, coord, coord, float, float, float, float, float)’:
    bar.cpp:123: warning: declaration of ‘x’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘A’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘ay’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘vy’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘vx’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘d’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘c’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘b’ shadows a member of 'this'
    bar.cpp:123: warning: declaration of ‘a’ shadows a member of 'this'
    bar.cpp:121: warning: ‘Snake::d’ will be initialized after
    bar.cpp:120: warning:   ‘float Snake::vx’
    bar.cpp:123: warning:   when initialized here
    bar.cpp: In member function ‘bool Snake::death()’:
    bar.cpp:126: warning: no return statement in function returning non-void
    bar.cpp: In function ‘void simulateWorld(long int, long int)’:
    bar.cpp:156: warning: unused variable ‘te’
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    I use VS and it didnt give me all this warning lol

    I was taught that in a constructor I can have paramaters with the same names as the members. And when i do this I have to refer to the members as this->variable_name and refer to the parameters by their name(variable_name). And it worked for me. Using initialization lists shouldnt mess with them either.

    I also checked the members value during debug, so I think they should be ok.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Ok, so I just had the idea of removing the ceiling and the floor to check if the elevators were still there, and yes, they are there:/ So the problem is with the animation not being smooth. (well, going instantly to the top and bottom certainly isnt smooth lol). oh and the acceleration was added wrongly to the velocity when they were going down, so i edited the code in my first post.

    EDIT: ive managed to make it smooth by changing the acceleration of elevators to 0.000001f. Are there any better alternatives? like making the program to make the changes and animate at a set period(e.g: every quarter seconds)?
    Last edited by TiElite; 09-18-2011 at 03:40 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    ok, i think ive solved it myself.
    Last edited by TiElite; 09-19-2011 at 05:53 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    @OP - please don't trash the whole thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ OpenGL bird animation
    By Scythed in forum Game Programming
    Replies: 9
    Last Post: 11-10-2010, 11:00 PM
  2. C++ animation through bitmap and OpenGL
    By egor in forum C++ Programming
    Replies: 15
    Last Post: 12-09-2009, 01:17 AM
  3. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  4. OpenGL: glutIdleFunc and animation
    By gazsux in forum Game Programming
    Replies: 1
    Last Post: 03-21-2003, 07:24 AM
  5. OpenGL 100% CPU problem
    By tWiZtEr in forum Game Programming
    Replies: 6
    Last Post: 12-23-2002, 05:49 AM