problem with keyboard driven movement

Member
Posts: 185
Joined: 2005.02
Post: #1
ok with my "space ship" program, when I press the space bar the "flame" turns on but the ship doesn't move. Why is that?
Code:
void ProcessKeyboardInput(unsigned char key, int x, int y)
{
    if (key == 32)
    {
        curenTime = glutGet(GLUT_ELAPSED_TIME);
        inMotion = 1;
    }    
}    

void KeyUp(unsigned char key, int x, int y)
{
    if (key == 32)
    {
        inMotion = 0;
        prevTime = 0;
    }
}
    
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    if (inMotion)
    {
        if (prevTime == 0)
            prevTime = curenTime - 1;
        timeDif = curenTime - prevTime;
        timeDifer = timeDif / 1000.0f;
        coordy =+ timeDifer * 300;
        curenTime = prevTime;
        if (coordy >  480)
            coordy = 0;        
    }
    
    glLoadIdentity();
    glPushMatrix();
        glTranslatef(320.0 + coordx, 50.0 + coordy, 0.0);
        glColor3f(0.0, 0.0, 1.0);
        drawOBJ(spaceship);
        if (inMotion)
        {
            glColor3f(1.0, 0.0, 0.0);
            drawOBJ(flame);
        }
    glPopMatrix();            
    glutSwapBuffers();
}

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    
     ...Blah blah blah lots of OBJ loading stuff  

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    
    glutCreateWindow("Space Ship");
    
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    
    glutKeyboardFunc(ProcessKeyboardInput);
    glutKeyboardUpFunc(KeyUp);
    
    glutMainLoop();
    return EXIT_SUCCESS;
}
grr... and I got so far without asking for help. Sad
Quote this message in a reply
Member
Posts: 353
Joined: 2002.04
Post: #2
Could this be your problem?

coordy =+ timeDifer * 300;

Should be:

coordy += timeDifer * 300;
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #3
ARG!!! why is it always the stupid stuff!

Thanks for your help.
Quote this message in a reply
Member
Posts: 353
Joined: 2002.04
Post: #4
When you've been looking at code for ages you stop seeing the little things because you think there's something wrong with your process, not your syntax. It's amazing what a different pair of eyes can see. Smile
Quote this message in a reply
Member
Posts: 148
Joined: 2003.03
Post: #5
Someone should write a program that scans source code for that type of thing.
Things like:

=+ instead of +=
= instead of == in an if() statement

There's probably many more little things like this.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #6
MacFiend Wrote:= instead of == in an if() statement

Would be detected and reported with -Wall. =+ instead of += wouldn't tough.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Moderator
Posts: 771
Joined: 2003.04
Post: #8
Just saw a link in OSNews to this new ADC article:
http://developer.apple.com/tools/xcode/s...lysis.html
Quote this message in a reply
Post Reply