Model positioning problems
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
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
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
You probably just want:
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
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
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
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
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement