Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4546938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:01:32+00:00 2026-05-21T16:01:32+00:00

I have been trying to draw multiple balls for a game I am making,

  • 0

I have been trying to draw multiple balls for a game I am making, I have tried to get this working but there are problems in my init class and display method class, I have commented it where the errors are.

Ball.h:

#pragma once
#include "Vector2f.h"
#include "Vector3f.h"

class Ball
{
private:
    Vector3f position;
    Vector3f velocity;

public:
    Ball(void);
    ~Ball(void);

    void Draw();
    void SetPos(Vector3f New_position);
    void SetVel(Vector3f New_velocity);
    Vector3f GetPos();
};

Ball.cpp

#include "Ball.h"
#include "Vector2f.h"
#include "Vector3f.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"


Ball::Ball(void)
{
    Vector3f Temp_position;
    position = Temp_position;
    Vector3f Temp_velocity;
    velocity = Temp_velocity;
}


Ball::~Ball(void)
{
}

void Ball::SetPos(Vector3f New_position)
{
    position = New_position;
}

void Ball::Draw()
{
    glPushMatrix();
    glTranslatef(position.X(), position.Y(), position.Z());
    glColor3d(1, 0, 0);
    glutSolidSphere(0.3, 50, 50);
    glPopMatrix();
}

void Ball::SetVel(Vector3f New_velocity)
{
    velocity = New_velocity;
}

Vector3f Ball::GetPos()
{
    Vector3f temp;
    temp = position;
    return temp;
}

I want the be able to draw an array of the balls in Main.cpp

Main.cpp 

#include "Display.h"
#include "Vector3f.h"
#include "Ball.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include <math.h>



static float TableWidth = 4;  // Z axis normal = 4
float Display::eyeX = -7.5; //-7.5
float Display::eyeY = 3; //3
float Display::eyeZ = 5; //5
float Display::Position[4] = { 1.0f, 0.0f, -3.5, 1.0f };
float Display::translateZ = -3.5;
float Display::translateX = 0.0;
//Timer Display::m_Timer = Timer();
float Display::lightX = 5.0; //5 2.5
float Display::lightY = 5.0;
float Display::lightZ = 2.5;


float m_TableX = -5.0f;
float m_TableZ = -2.5f;
float m_TableWidth = 2.5f;
float m_TableLength = 5.0f;

float ballx = 0.7;
float bally = 0.1;
float ballz = -0.7;

Ball Redball;
float BALL_RED_START = 0;
float RADIUS_OF_BALL = 0.3;
float BALL_RED_END = 8;
float m_ball;




void Display::Init(int argc, char ** argv)
{
    glutInit(&argc, argv); // initializes glut
    // sets display mode. These parameter set RGB colour model
    // and double buffering.
    glutInitWindowSize(500,500);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("Pool Version 1.0");


    // Set glut callback functions
    glutDisplayFunc(Display::DisplayScene);
    glutIdleFunc(Display::Idle);
    glutReshapeFunc(Display::Resize);
    glutKeyboardFunc(Display::KeyboardInput);
    //m_Timer.getSeconds();
    glEnable(GL_DEPTH_TEST);
    glPointSize(5);

        glEnable(GL_NORMALIZE);
        glEnable(GL_LIGHTING);
        glClearColor(0,0,0,1);
        glEnable(GL_COLOR_MATERIAL);


float white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);

glEnable(GL_LIGHT0);
Redball.SetPos(Vector3f(0.0,0.3,0.0));

for(int i = BALL_RED_START; i < BALL_RED_START; i++)
{
  glColor3f(1,0,0);
  Redball[i]->SetPos(Vector3f (i+128,RADIUS_OF_BALL,45));  //I tried this but it doesn't work Error C2227
}



    // Begin glut main loop
    glutMainLoop();
}




void BallMovement()
{
    //Vector3f position(0.0,0.3,0.0);
    /*Redball.SetPos(Vector3f(0.0,0.3,0.0));*/
    Vector3f New_velocity(0.01,0,0);
    Redball.SetVel(New_velocity);

    Vector3f New_position;
    Vector3f Old_position;

    Old_position = Redball.GetPos();

    //New_position = Old_position + New_velocity;

    New_position.SetX(Old_position.X() + New_velocity.X());
    New_position.SetY(Old_position.Y() + New_velocity.Y());
    New_position.SetZ(Old_position.Z() + New_velocity.Z());


    Redball.SetPos(New_position);
}





void Display::DisplayScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the back buffer


    glPushMatrix();
    glLoadIdentity();
    glNormal3f(0,1,0);
    Vector3f didums = Redball.GetPos();
    gluLookAt(eyeX, eyeY, eyeZ,     // eye position
            0, 0, 0,        // what I'm looking at
            0.0, 1.0, 0); // Up direction

    float Position[] = {lightX, lightY, lightZ, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, Position);
DrawLight(0, Position);



    /* Rendering code goes here */

for (int i = BALL_RED_START; i<BALL_RED_END;i++)
{
glColor3f(1,0,0);
Redball[i]->Draw();     //I tried this but it doesn't work Error C2227
}




drawTable();
drawTableLegFrontLeft();
drawTableLegFrontRight();
drawTableLegBackLeft();
drawTableLegBackRight();
drawCushions();
//drawCircle();
//drawHCircle();
Table(-2,-4.5,2,4.5); // Draws the table top in trianglestrip       -4.5, 0.5, -0.5, 9.5



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

void Display::Resize(int w, int h)
{
    /* Resize is called when window is resized */
    glMatrixMode(GL_PROJECTION); // set matrix mode to profection
    // this dictates how the 3d scene is "squashed" onto the 2d screen
    glLoadIdentity();
    glViewport(0, 0, w, h);  // Set the part of the window to use.
    gluPerspective(45,              // field of view
                (float)w/(float)h,  // ration of window
                1,                  // front clipping plane
                1000                // back clipping plane
                );          // set the area in the 3d scene to draw

    glMatrixMode(GL_MODELVIEW); // Setthe matrix mode to model view
        // the matrix specifies how the 3d scene is viewed
    /*glLoadIdentity();
    gluLookAt(-3.5, 2, eyeZ,        // eye position
            1, 1, 0,        // what I'm looking at
            0.0, 1.0, 0); // Up direction*/
}




void Display::Idle()
{
    /* When nothing else is happening, idle is called.
     * Simulation should be done here and then
     * the display method should be called
     */

    BallMovement();

    glutPostRedisplay();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:01:33+00:00Added an answer on May 21, 2026 at 4:01 pm

    I see you have declared Ball as Ball Redball; which will create a single Ball on the stack.

    Then you attempt to treat it as a collection of Balls with Redball[i]->SetPos(...) and Redball[i]->Draw(). It appears you are attempting to work with 8 of them.

    What you want to do is create an array of Balls, with a max size of 8 (according to BALL_RED_END). For simplicity, you could do

    Ball RedBall[8];
    for( //some conditions here )
    {
        RedBall[i].Draw();
    }
    

    as your declaration and usage.

    Remember that anytime you use Redball.SetPos(...) will no longer be valid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get around this error for a day now and
I have been trying this for so long, but was not able to implement
I am very new to android development and have been trying to draw a
I have been trying to draw an annulus (ring with thickness) with a transparent
I have been trying to register 3 hotkeys. I followed this example (or this
I have been trying to define multiple combo boxes in R using the tcltk
I have been trying to get the unique values in each column of a
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to determine a best case solution for registering a COM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.