Ball not showing OpenGL C++

85 Views Asked by At

I'm making pong, and my Ball object isn't being drawn on the screen. Is there something I'm not seeing?

My guess is that it's something within the main method, or just something I'm overlooking.

#include <GL/glut.h>
#include <cmath>

class GameObject
{
public:
    float xpos;
    float ypos;

    void draw() {}
    void update() {}
};

class Paddle : public GameObject
{
public:
    float xlen;
    float ylen;

    float xvel;
    float yvel;

    Paddle() {}

    Paddle(float x, float y, float xl, float yl, float xv, float yv)
    {
        xpos = x;
        ypos = y;
        xlen = xl;
        ylen = yl;
        xvel = xv;
        yvel = yv;
    }

    void draw()
    {
        glPushMatrix();
        glTranslatef(xpos, ypos, 0.0f);
        glColor3f(1.0f, 1.0f, 1.0f);
        glBegin(GL_QUADS);
        glVertex2f(-xlen, -ylen);
        glVertex2f(-xlen, ylen);
        glVertex2f(xlen, ylen);
        glVertex2f(xlen, -ylen);
        glEnd();
        glPopMatrix();
    }

    void update()
    {
        xpos += xvel;
        ypos += yvel;

        if (xpos > 1.0f || xpos < -1.0f)
        {
            xvel *= -1.0f;
        }

        if (ypos > 1.0f || ypos < -1.0f)
        {
            yvel *= -1.0f;
        }
    }
};

class Ball : public GameObject
{
public:
    float rad;

    float xvel;
    float yvel;

    Ball() {}

    Ball(float x, float y, float rad, float xv, float yv)
    {
        xpos = x;
        ypos = y;
        rad = rad;
        xvel = xv;
        yvel = yv;
    }

    void draw()
    {
        glPushMatrix();
        glTranslatef(xpos, ypos, 0.0f);
        glColor3f(1.0f, 1.0f, 1.0f);
        glBegin(GL_TRIANGLE_FAN);
        for (int i = 0; i < 360; i++)
        {
            float theta = i * 3.14159 / 180;
            glVertex2f(rad * cos(theta), rad * sin(theta));
        }
        glEnd();
        glPopMatrix();
    }

    void update()
    {
        xpos += xvel;
        ypos += yvel;

        // Check for collision with the edges of the window
        if (xpos > 1.0f || xpos < -1.0f)
        {
            xvel *= -1.0f;
        }

        if (ypos > 1.0f || ypos < -1.0f)
        {
            yvel *= -1.0f;
        }
    }
};

class Game
{
public:
    Paddle paddle1;
    Paddle paddle2;
    Ball ball;
    Game()
        : paddle1(-0.85f, 0.0f, 0.05f, 0.3f, 0.0f, 0.1f),
          paddle2(0.85f, 0.0f, 0.05f, 0.3f, 0.0f, -0.1f),
          ball(0.0f, 0.0f, 0.3f, 0.0f, 0.0f)
    {
    }
};

Game game = Game(); // global game object

void display()
{
    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw the paddles
    game.paddle1.draw();
    game.paddle2.draw();

    // Draw the ball
    game.ball.draw();

    // Swap the front and back buffers (double buffering)
    glutSwapBuffers();
}

void update(int value)
{
    // Update the object's position
    game.paddle1.update();
    game.paddle2.update();

    game.ball.update();

    // Redraw the scene
    glutPostRedisplay();

    // Call update() again after 16 milliseconds (60 frames per second)
    glutTimerFunc(16, update, 0);
}

int main(int argc, char **argv)
{
    // Initialize GLUT and create the window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(720, 720);
    glutCreateWindow("Pong");

    // Set up the display and update callbacks
    glutDisplayFunc(display);
    glutTimerFunc(0, update, 0);

    // Start the main loop
    glutMainLoop();

    return 0;
}

I've tried messing around with it, even giving it the same code to draw as the paddles, but it doesn't work.

0

There are 0 best solutions below