1
\$\begingroup\$

I am new in OpenGL, I am making a game in 2D and I want the camera to follow the character.

I started with gluLookAt() but I read that I could just move the matrix with glTranslatef() and It should do the trick.

The problem is the camera only move the first time It is created but if I increment the X position and Y position It doesn't work. What I m doing wrong? I have incorrect concepts?

I tried it changing to GL_PROJECTION,GL_MODELVIEW, AND GL_TEXTURE,

If I do it on GL_PROJECTION all the items are gone ( thanks to the question answer link I understand that I cant take that way)

If I do it on GL_TEXTURE all the textures start to scroll ( but that is not what i want

If I do it on GL_MODELVIEW just nothing happen all the item are in the same site but if I quit the gl.glMatrixMode(GL10.GL_MODELVIEW) line above translatef It move the camera the first iteration and then stop to do it since the last object is on GL10.GL_MODELVIEW

Is at the start at different matrix state?

My render code:

public void onDrawFrame(GL10 gl)
{
    loopStart = System.currentTimeMillis();
    try
    {
        if (loopRunTime < SEngine.GAME_THREAD_FPS_SLEEP)
        {
            Thread.sleep(SEngine.GAME_THREAD_FPS_SLEEP - loopRunTime);
        }
    } catch (InterruptedException e)
    {
    }
    deltaTime = (float) ((System.currentTimeMillis() - aRestar)/1000f);


    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glClearColor((78.0f / 255.0f), (192.0f / 255.0f), (203.0f / 255.0f), 0.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glTranslatef(x, y, 0f);

    perro.draw(gl);
    palmera.draw(gl);

    comprobacionesColision();
    actualizar(deltaTime);


    x +=0.05f;
    y +=0.05f;

    loopEnd = System.currentTimeMillis();
    loopRunTime = ((loopEnd - loopStart));

}
\$\endgroup\$
  • \$\begingroup\$ The code looks right, you are translating your scene each frame to new X/Y, which are +0.5 from previous. \$\endgroup\$ – Kromster Nov 21 '14 at 9:48
  • \$\begingroup\$ Yes but if I put glMatrixMode(GL10.GL_MODELVIEW) it doesnt work \$\endgroup\$ – D4rWiNS Nov 24 '14 at 13:22
  • \$\begingroup\$ IIRC it's recommended to reset the matrix after changing matrix mode. glLoadIdentity \$\endgroup\$ – Kromster Nov 24 '14 at 13:24
  • \$\begingroup\$ If I put glLoadIdentity before translate it , still nothing happen \$\endgroup\$ – D4rWiNS Nov 24 '14 at 13:44
1
\$\begingroup\$

Ok after a lot of tries I made it,Stil I dont know if this is the best way to achieve this, if someone can explain why this is a solution it will be good!

I just change the matrix mode to GL_PROJECTION and translate it

gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0f, 8f, 0f, 4f, -1f, 1f);
    gl.glTranslatef(x, y, 0f);

instead of

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glTranslatef(x, y, 0f);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.