Jake Collier
Jake Collier

Reputation: 21

OpenGL checking screen boundaries

I am working on a project for my openGL class that is supposed to emulate a Yo-Yo, I am making progress but I have seem to hit a roadblock when it comes to checking the bounds of the screen. I am trying to get it so that once the ball reaches the bottom of the screen it starts to go up and vice verse, and help would be much appreciated!

I have three static floats declared above that are to be incremented by the function, I am trying to check the deltay in order to limit the ball's movement.

Below is my function that translates the ball, the ball moves but will continue to move past the boundaries I have set

void moveYoYo() {

glClear(GL_COLOR_BUFFER_BIT);

//going up
if(flag == 1)
{
    deltay -= .05;

    if(deltay <= -.002)
    {
        flag = 0;

    }

}
//going down
if(flag == 0)
{
    deltay += .05;

    if(deltay >= .002)
    {
        flag = 1;

    }

}

glPushMatrix();
    glTranslatef(deltax,deltay,deltaz);
    display();
glPopMatrix();
glFlush();
glutSwapBuffers();

}

Upvotes: 2

Views: 337

Answers (1)

Maroš Beťko
Maroš Beťko

Reputation: 2329

If you want that ball to bounce between the bottom and the top of the screen, the lower bound where it changes the direction should be <= -0.002 and the upper bound >= 1.002 if you want to use those strange numbers. Anyway much better would be to use less random numbers, like f.e.:

if (flag == 1) {
    // Going down
    deltay -= 0.05f;
    if (deltay == 0)
        flag = 0;
} else if (flag == 0) {
    // Going up
    deltay += 0.05f;
    if (deltay == 1)
        flag = 1;
}

Also if the flag in only used as 0 or 1 why not use a bool?

Upvotes: 1

Related Questions