0

I'm created a grid to represent ground to walk on.

I've got my arrow keys linked up to rotate/move the view:

switch(currentKey)
        {
        case sf::Key::Up:
            z_trans+=1;
            break;

        case sf::Key::Down:
            z_trans-=1;
            break;

        case sf::Key::Left:
            y_angle-=0.5;
            break;

        case sf::Key::Right:
            y_angle+=0.5;
            break;
        }

With z_trans being the translation in z, y_angle being the rotation in the y axis in glRotatef().

I can get my 'character' to walk forward and turn left and right, and then continue walking straight in the direction he is facing. The only problem is that as I get away from the origin (0,0,0), the rotations occur AROUND the origin, not the camera. So in if I'm 100 units away from the origin, my character is basically following the path of a 100 radius circle.

WITHOUT using gluLookAt, does anyone have any ideas how to implement this? Completely stumped, been sat here a good 2 hours trying to figure it out.

The full code:

float elapsedTime=Clock.GetElapsedTime();
    if(elapsedTime>REFRESH_RATE){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity(); 
        glTranslatef(x_trans, y_trans, z_trans); // y_trans == -25.f to keep us above the plane
        glRotatef(x_angle,1,0,0);
        glRotatef(y_angle,0,1,0);
        glRotatef(z_angle,0,0,1);

        currentKey=sf::Key::Num9;

        if (Event.Type == sf::Event::KeyPressed) currentKey = Event.Key.Code;
        switch(currentKey)
        {
        case sf::Key::Up:
            z_trans+=1;
            break;

        case sf::Key::Down:
            z_trans-=1;
            break;

        case sf::Key::Left:
            y_angle-=0.5;
            break;

        case sf::Key::Right:
            y_angle+=0.5;
            break;
        }
        drawGrid();

        Clock.Reset();
    }
Dezachu
  • 153
  • 1
  • 2
  • 10

1 Answers1

0

Rotate fist, then translate second?

edit: I just checked some of my existing code.

glLoadIdentity();
// Rotate around origin first
glRotatef(RotY,-1.0f ,0.0f, 0.0f);
glRotatef(RotX, 0.0f, 1.0f, 0.0f);
// Then move the camera
glTranslatef(-Player.x, -Player.y, -Player.z);
JohnD
  • 1,129
  • 10
  • 19
  • I get the issue then that the axis don't rotate. As in, I can be facing 90 degrees to my right, I'll press Up to move 'forward' and I'll move to my left. – Dezachu Dec 05 '12 at 13:25
  • If I adjust RotY, RotX and Player variables.. I get the correct results. So that is the correct order of operations. – JohnD Dec 06 '12 at 01:31
  • Hi JohnD, i am looking for panning functionality in android like moving the GLSurfaceview, not object.[i need to move the GLSurfaceview using touch event]. if you have any idea now , pls give any sample code. it would be helpful for me to complete my task.i am struggle here. my sample code here.http://www.mediafire.com/?09f9q51xnqhq2l7.which – harikrishnan Jul 06 '13 at 18:18