2011-04-18 55 views
0

編輯:定位對象的數組在OpenGL

我仍然沒有得到正確的位置,所有的球被在原點繪製的,我想我可以把東西在錯誤的地方...

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(); 

結果是DRAWIN g原點上的數組所有的球都在同一個地方!

+0

任何你不能在你的數組中放置當前x,y,z位置(平移)的原因? – holtavolt 2011-04-18 22:48:06

+0

這就是我想要的!我只是不確定如何! – 2011-04-18 23:03:36

+1

首先,將三行添加到您的Ball類中以存儲x,y和z int值。然後,您可以在程序中將這些值設置爲某個初始值。在上面的循環中,在glTranslate調用中使用這3個值(例如glTranslate(Yellowball [i] .x,Yellowball [i] .y,Yellowball [i] .z);) – holtavolt 2011-04-19 01:46:18

回答

2

做這樣的事情:

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

    private: 
     int xpos, ypos; 
    //... 
}; 

然後,當你構建球的陣列,而不是僅僅做8個球,你會想在堆上分配內存,使其將持續貫穿整個遊戲。所以這樣做:

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); 
// ... 

確保當你的遊戲結束後,你自己清理。

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

然後在你的球:: DrawYellow()做這樣的事情:

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 
} 

它可能會採取從這裏調整的一點點,但是這是否有意義/回答你的問題?

+0

我認爲添加一個glPushMatrix/glPopMatrix對並不是什麼壞主意。 – datenwolf 2011-04-19 06:43:20

+0

是的,這是有道理的!我會給它一個去看看發生了什麼感謝! – 2011-04-19 16:25:35

+0

正確的仍然有一些問題我編輯我的帖子與我有:) – 2011-04-21 16:04:50