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

  • Home
  • SEARCH
  • 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 4329788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:52:17+00:00 2026-05-21T09:52:17+00:00

Edit: I’m still not getting the correct position, all the balls are being drawn

  • 0

Edit:

I’m still not getting the correct position, all the balls are being drawn at the origin, I think I may be putting things in the wrong places…

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::ArrayPosition()
{

Ball *Yellowball = new Ball[8];
Yellowball[0].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[1].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[2].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[3].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[4].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[5].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[6].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[7].SetPos(Vector3f (position.X(), position.Y(), position.Z()));

}

void Ball::DrawYellow()
{

glPushMatrix();
glColor3f(2,1,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;
}

Vector3f Ball::GetVel()
{
    Vector3f temp;
    temp = velocity;
    return temp;
}

Main.cpp

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

Ball Yellowball; 
float BALL_YELLOW_START = 0;

float BALL_YELLOW_END = 8;

init()

Ball *Yellowball = new Ball[8];

for(int i = BALL_YELLOW_START; i < BALL_YELLOW_START; i++)
{

Yellowball[0].SetPos(Vector3f (1,0,0));
Yellowball[1].SetPos(Vector3f (0,0,0));
Yellowball[2].SetPos(Vector3f (0,0,0));
Yellowball[3].SetPos(Vector3f (0,0,0));
Yellowball[4].SetPos(Vector3f (0,0,0));
Yellowball[5].SetPos(Vector3f (0,0,0));
Yellowball[6].SetPos(Vector3f (0,0,0));
Yellowball[7].SetPos(Vector3f (0,0,0));
}

Draw method:

Ball Yellowball[8];

for (int i = BALL_YELLOW_START; i<BALL_YELLOW_END;i++)
{
glColor3f(2,1,0);

Yellowball[i].DrawYellow();

}
glPopMatrix();

The result is that is drawing the array at the origin all the balls are in the same place!

  • 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-21T09:52:17+00:00Added an answer on May 21, 2026 at 9:52 am

    Do something like this:

    // first of all, include the x,y position (assuming 2D, since pool) in the Ball object:
    class Ball
    {
       //...
    
       private:
          int xpos, ypos;
       //...
    };
    

    Then when you construct the array of balls, rather than just making 8 balls, you’re going to want to allocate the memory on the heap so that it will last throughout your entire game. So do this:

    Ball *YellowBall[8];
    YellowBall[0] = new Ball(x0,y0);
    YellowBall[1] = new Ball(x1,y1);
    YellowBall[2] = new Ball(x2,y2);
    YellowBall[3] = new Ball(x3,y3);
    // ...
    

    Make sure that when your game is over, you clean up after yourself.

    for (int i = 0; i < 8; i++)
       delete YellowBall[i];
    

    Then in your Ball::DrawYellow() do something like this:

    Ball::DrawYellow() 
    {
       glColor3f(/*yellow*/); // Set the color to yellow 
       glTranslatef(-x, -y, 0); // Move to the position of the ball
       // Draw the ball
       glTranslatef(x, y, 0); // Move back to the default position
    }
    

    It will probably take a tiny bit of tweaking from here, but does this make sense/answer your question?

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

Sidebar

Related Questions

No related questions found

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.