开发者

opengl array of pool balls

i am drawing an array of pool balls in opengl using c++ the problem i am facing is the array draws in a straight line. when i use gltranslate the balls still only translate along the line when i edit the z and y axis what i want to do is set the balls up in a triangle shape like the breaking of a pool match how do i use the array code to set the balls up like this? any help would be much appreciated

 balls[7];
    for (int x = ball-start; x<ball-end;x++)
    {
开发者_开发技巧       glTranslatef(0,0,0.5);
       glColor3f(1,0,0);
       ball[x].drawball();
    }


assuming:

struct Ball {
    double x,y,z;
    void drawball(void);
    /* ... */
    } ball[7];

try:

for(int i=0; i<7 ;i++)
    {
    glPushMatrix();
        glTranslated(ball[i].x,ball[i].y,ball[i].z);
        glColor3f(1,0,0);
        ball[i].drawball();
    glPopMatrix();
    }

details probably vary, but hopefully you get the idea.


Do something like this:

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

   private:
      float 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 *ball= new Ball*[8];
ball[0] = new Ball(x0,y0);
ball[1] = new Ball(x1,y1);
ball[2] = new Ball(x2,y2);
ball[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 ball[i];

delete [] ball;

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

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

All you have to do is come up with the correct (x0,y0),(x1,y1),(x2,y2)... to form a triangle! Does this make sense/answer your question?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜