我仍然沒有得到正確的位置,所有的球被在原點繪製的,我想我可以把東西在錯誤的地方...
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原點上的數組所有的球都在同一個地方!
任何你不能在你的數組中放置當前x,y,z位置(平移)的原因? – holtavolt 2011-04-18 22:48:06
這就是我想要的!我只是不確定如何! – 2011-04-18 23:03:36
首先,將三行添加到您的Ball類中以存儲x,y和z int值。然後,您可以在程序中將這些值設置爲某個初始值。在上面的循環中,在glTranslate調用中使用這3個值(例如glTranslate(Yellowball [i] .x,Yellowball [i] .y,Yellowball [i] .z);) – holtavolt 2011-04-19 01:46:18