i’m doing a very simple 2D battleship game. I want to make the ship moves forward so i use glTranslatef(10,0,0) and the enemies move towards the battleship using glTranslatef(-20,0,0). However, the battleship and enemies move together to the left. I tried many ways to move each of them separately but still cannot do it… Can someone please help me?
my code:
void drawBattleShip()
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2i((75+X), (248+Y));
glVertex2i((85+X), (225+Y));
glVertex2i((115+X), (225+Y));
glVertex2i((125+X), (248+Y));
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f);
for(float angle1=0; angle1<PI; angle1+=STEP)
glVertex2f((100.0+X) + 55.0 * cos(angle1), (275+Y) + 35.0 * sin(angle1));
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
for(float angle2=0; angle2<PI; angle2+=STEP)
glVertex2f((100.0+X) + 55.0 * cos(angle2), (275+Y) + 15.0 * sin(angle2));
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f);
for(float angle3=0; angle3<PI; angle3+=STEP)
glVertex2f((48.0+X) + 15.0 * cos(angle3), (275+Y) + 5.0 * sin(angle3));
glEnd();
}
void drawEnemies()
{
glColor3f(r1, g1, b1);
glBegin(GL_POLYGON);
glVertex2i(720, 520);
glVertex2i(720, 560);
glVertex2i(760, 560);
glVertex2i(760, 520);
glEnd();
glColor3f(r2, g2, b2);
glBegin(GL_POLYGON);
glVertex2i(800, 420);
glVertex2i(800, 460);
glVertex2i(840, 460);
glVertex2i(840, 420);
glEnd();
}
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT);
// glLoadIdentity();
glPushMatrix();
glTranslatef(10.0, 0.0, 0.0);
drawBattleShip();
glPopMatrix();
glPushMatrix();
glTranslatef(-10.0, 0.0, 0.0);
drawEnemies();
glPopMatrix();
glFlush();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (0.0, 900.0, 0.0, 700.0, 0.0, 1.0);
}