Hello, I’m new user in this forum but I was reading a lot of threads that helped me but in this case I didn’t get the answer.
My problem is that my game is flickering all the objects (jumping backforward) and I don’t understand why.
I tried to use delta for the movement…, I tried to use sleep…, but nothing works. I don’t know what more to do, and in other tests with delta and “unlimited fps” the code works perfect.
Example of code that works:
[spoiler]http://pastebin.com/vjZ0cgdh[/spoiler]
This is my game loop:
private static void bucleJoc()
{
double passat = 0;
double dividir_entre = 1000.0/60.0;//cada milisegon
ultimTemps = lastFrame = Sys.getTime();
objectes = new ArrayList<ObjecteJoc>();
jugador = new Jugador(0, ALTURA / 2 - Jugador.ALTURA / 2);
enemic = new Enemic(AMPLADA - Enemic.AMPLADA, ALTURA / 2 - Jugador.ALTURA / 2);
pilota = new Pilota(AMPLADA / 2 - Pilota.MIDA / 2, ALTURA / 2 - Pilota.MIDA / 2);
objectes.add(jugador);
objectes.add(enemic);
objectes.add(pilota);
getDelta();
while(!Display.isCloseRequested())
{
double delta = getDelta();
passat += delta;
while(passat >= dividir_entre)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
obtindreTecles();
actualitzar(passat);
passat = 0;
}
renderitzar();
try
{
Thread.sleep(2);
} catch(InterruptedException e)
{
e.printStackTrace();
}
actualitzaFPS();
Display.update();
}
tancarTot();
}
This is my render function:
public void renderitzar()
{
crearRectangle(x,y,amplada,altura,cVermell,cVerd,cBlau,rotacio);
}
private void crearRectangle(float x, float y, int amplada, int altura, int vermell, int verd, int blau, float rotacio)
{
glPushMatrix();
{
glColor3ub((byte) vermell, (byte) verd, (byte) blau);
glTranslatef(x,y,0);
/*glTranslatef(amplada/2,altura/2,0);
glRotatef(rotacio,0,0,1);
glTranslatef(-amplada/2,-altura/2,0);*/
glBegin(GL_QUADS);
glVertex2f(0,0);
glVertex2f(amplada,0);
glVertex2f(amplada,altura);
glVertex2f(0,altura);
glEnd();
}
glPopMatrix();
}
And this my update for the movement:
public void actualitzar(double delta)
{
System.out.println("Y: "+ this.y);
if(Keyboard.isKeyDown(Keyboard.KEY_W) || Keyboard.isKeyDown(Keyboard.KEY_UP))
{
this.y += VEL * delta;
}
if(Keyboard.isKeyDown(Keyboard.KEY_S) || Keyboard.isKeyDown(Keyboard.KEY_DOWN))
{
this.y -= VEL * delta;
}
//System.out.println(this.y + " " + delta);
if(this.y >= Joc.ALTURA - ALTURA / 2)
this.y = Joc.ALTURA - ALTURA / 2;
if(this.y <= 0 - ALTURA / 2)
this.y = -ALTURA / 2;
}
If you want I can post the full code.
Thanks for the help and attention.