Sign in to follow this  

Model positioning problems

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I am designing a 3d game and I have majorly hit a brick wall. I have created methods to load in my models fine from a raw file, and all of my models were in rows and moved together like in space invaders. But now that i have to make a seperate class for the models, using a constructor etc, when i draw the models 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

Share this post


Link to post
Share on other sites
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.glTranslatef(xAlien,yAlien,zAlien); //move forward
renderAlien();
}
}

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[(y*6)+x] = new Alien((float)x*4,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

Share this post


Link to post
Share on other sites
You probably just want:
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.

Enigma

Share this post


Link to post
Share on other sites
Cheers for the reply Enigma, i know what you mean.
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?
thanks

Share this post


Link to post
Share on other sites
If I place trace writes in my constructor i get the following:
xAlien = 0.0, yAlien = 0.0, zAlien = 0.0
xAlien = 0.0, yAlien = 0.0, zAlien = 3.0
xAlien = 0.0, yAlien = 0.0, zAlien = 6.0
xAlien = 4.0, yAlien = 0.0, zAlien = 0.0
xAlien = 4.0, yAlien = 0.0, zAlien = 3.0
xAlien = 4.0, yAlien = 0.0, zAlien = 6.0
xAlien = 8.0, yAlien = 0.0, zAlien = 0.0
xAlien = 8.0, yAlien = 0.0, zAlien = 3.0
xAlien = 8.0, yAlien = 0.0, zAlien = 6.0
xAlien = 12.0, yAlien = 0.0, zAlien = 0.0
xAlien = 12.0, yAlien = 0.0, zAlien = 3.0
xAlien = 12.0, yAlien = 0.0, zAlien = 6.0
xAlien = 16.0, yAlien = 0.0, zAlien = 0.0
xAlien = 16.0, yAlien = 0.0, zAlien = 3.0
xAlien = 16.0, yAlien = 0.0, zAlien = 6.0
xAlien = 20.0, yAlien = 0.0, zAlien = 0.0
xAlien = 20.0, yAlien = 0.0, zAlien = 3.0
xAlien = 20.0, yAlien = 0.0, zAlien = 6.0

but i only see one alien, can anyone see why?
thanks

Share this post


Link to post
Share on other sites

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this