Hi guys, I have majorly hit a brick wall with this space invaders of mine. I had my models loaded fine, and all the aliens were in rows and moved together. But now that i have to make a seperate class for the aliens, using a constructor etc, when i draw the aliens to the screen they are not placed in rows anymore.
I’m hoping that one of you guys/gals would be kind enough to have a look at my code for me and see if you can fix it so that they are placed in rows, like space invaders should be.
I have spent days on this and I just cant spot the problem, but one of you would probably be able to fix it in 5 minutes.
So if any of you have five minutes and are kind enough to have a look at my code for me, mail me and I will send it to you.
Thanks everyone
Danny
found out that maybe i should have the following included thanks to Enigma off another forum:
private void renderAliens()
{
if (alienIsAlive) {
GL11.glPushMatrix();
GL11.glTranslatef(xAlien,yAlien,zAlien); //move forward
renderAlien();
GL11.glPopMatrix();
}
}
Since translating moves the origin and not just the alien, so if you make multiple translation calls they add up. By pushing and popping the matrix you restore the old matrix after drawing each alien so the translations are independent.
unfortunately all i am seeing when i run it now is one alien.
i know that all of the aliens are created though and that their xyz
is correct as i have put trace write’s in the alien constructor and each
xyz seems correct.
any ideas?
Here is the relevant code from the classes incase some of you could spot the problem at a glance:
the alien class:
public Alien(float tempXCoord,float tempYCoord,float tempZCoord,boolean tempIsAlive)
{
xAlien = tempXCoord;
yAlien = tempYCoord;
zAlien = tempZCoord;
alienIsAlive = tempIsAlive;
}
public void renderTheAliens()
{
renderAliens();
}
/**
- Render a collection of aliens to the screen using the GL commands
- and the buffer we’ve read from the RAW file.
*/
private void renderAliens()
{
if (alienIsAlive) {
//GL11.glLoadIdentity();
GL11.glPushMatrix();
GL11.glTranslatef(xAlien,yAlien,zAlien); //move forward
renderAlien();
GL11.glPopMatrix();
}
}
public void update(int direction) {
//if System.timieMillis() - lasUpdateTime > 300 {
if(direction==0)
{
xAlien -=0.01f;
}
else if(direction==1)
{
xAlien +=0.01f;
}
}
/**
- Render a single alien to the screen
*/
private void renderAlien() {
//System.out.println("drawing aliens: ");
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glVertexPointer(3, 0, alienData);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, coords.size() / 3);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
}
and here is the code from the main class:
private boolean render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
GLU.gluLookAt(0,20,80, 0,0,0, 0,1,0);
//GL11.glTranslatef(moveOnX,0.0f,0.0f); //Move the aliens left/right
//GL11.glTranslatef(0.0f,0.0f,moveOnZ); //Move the aliens toward/away
for (int x = 0;x<aliens.length;x++) {
aliens[x].renderTheAliens();
}
return true;
}
private void init() throws Exception {
createWindow();
Keyboard.create();
initGL();
Alien.loadTheRawFile();
createAliens();
}
private void createAliens()
{
aliens = new Alien[18];
for (int x = 0;x<6;x++)
{
for (int y = 0;y<3;y++)
{
aliens[(y6)+x] = new Alien((float)x4,0.0f, (float)y*3, IS_ALIVE);
}
}
}
private Alien aliens[];
I am a newbie and I hope no-one minds me asking but i’ve been stuck on this for days and it would be nice to carry on with the movement of the models
thanks
[quote]aliens[(y6)+x] = new Alien((float)x4,0.0f, (float)y*3, IS_ALIVE);
[/quote]
Try multiplying with 40 and 30 instead of 4 and 3. Maybe all the aliens are rendered on top of eachother. Also try switching y and z, incase z is in/out instead of up/down.
thanks for the reply mate. changed the x value and it moved further right, changed the y and it moved higher, changed the z and it came closer so i think they are correct and unfortunately multiplying by higher numbers didn’t have an effect 
any other idea will be greatly appreciated
thanks
If your alienX, alienY and alienZ attibutes are marked as static they will be shared across all instances of you alien, therefore all your aliens will be draw on top of each other, make sure they’re instance variables by removing any static declarations.
Andy.
It works, thanks so much!!! 