Hi there,
i have a ball:

void ball (void) {
        glColor3f(0.0, 1.0, 0.0); //set ball colour
        glTranslatef(0.0,0.0,1); //moving it toward the screen a bit on creation
        glutSolidSphere (0.6, 7, 7); //create ball.
}

I am trying to get the ball to move in a direction when i press the J key.
I attempt this:

void keyboard_s (int key, int x, int y)
{
   switch (key)
   {
        case 'j': case 'J':
        glPushMatrix(); // to save the current matrix
        glTranslatef(ball.posX += 1, ball.posY, ball.posZ); //move ball +1 on it's x axis

        ball(); // draw our object

        glPopMatrix(); // restore the previous matrix

   }

I get an error:
error C2228: left of '.posX' must have class/struct/union
error C2228: left of '.posY' must have class/struct/union
error C2228: left of '.posZ' must have class/struct/union

I am stuck at this.
I believe i must make the ball a class/struct or union instead of void?
I am unsure on how to go about this.

Any help on the matter?
Thanks in advance.

ps: i've kept out alot of code i didnt think was necessary, so if something is, please do ask :)

That's why people shouldn't jump too early into graphics without good
knowledge of the basics. Ball is a function. It is not a class nor a struct.
Thus you cannot use the dot operator on ball.

A easy way to get the ball to move is by doing something like this :

float ballX = 0.0f;
float ballY = 0.0f;
float ballZ = -1.0f;

void drawBall(void) {
        glColor3f(0.0, 1.0, 0.0); //set ball colour
        glTranslatef(ballX,ballY,ballZ; //moving it toward the screen a bit on creation
        glutSolidSphere (0.6, 7, 7); //create ball.
}
void display(){ 
 glClear(...);
 //more stuff
 drawBall();
 //...
 glutSwapBuffer();
}
void keyboard_s (int key, int x, int y)
{
   switch (key)
   {
      case 'a' : ballX -= 0.05f;
      case 'd' : ballX  += 0.05f;
      case 'w' : ballY += 0.05f;
      case 's' : ballY -= 0.05f;
      case 'r' : ballZ += 0.05f;
      case 'f' : ballZ -= 0.05f;
   }

All you its doing there is updating the position of the ball. The ball
gets redrawn every frame, but ballX, ballY , and ballZ might does not
have to be the same at each frame. Thus changing ballX, ballY, ballZ
would change the position of the ball.

And P.S : I would advice you to learn more about classes before you
go to far with this graphics. Trust me, its gonna pay off later.

Thanks for the friendly reply and advice,

I understand classes for other programming languages such as Python. But i struggle with C++, this is for university so i have to do graphics, it doesnt help that we're not being taught C++ anytime on my course.

I'll give your code a greater look, i find i'm best at learning from others so what you wrote is a great example for me to learn from.

Thankyou firstPerson

It works for glutspehere() function. but we need to move a customization object. like traiangle or polygon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.