user1578454
user1578454

Reputation: 11

Problems with moving camera in openGL

Ok, I'd like to start off by saying that I know I'm not actually moving the camera, but it's easier to explain that way.

My problem is that I'm trying to move the camera with my character in a top down 2d rpg, and I can't find the correct way to do it. I know about glTranslate() but then I can only use a speed instead of an x and y coordinate. I'm not sure how to move the camera keeping the delta in mind. I don't even know if glTranslate() is even the method I should be using.

In case I'm not making any sense (which is very likely), here's some of my code.

My test while loop:

while(!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)&&!Display.isCloseRequested())
    {
        glClear(GL11.GL_COLOR_BUFFER_BIT);

        delta=getDelta();
        update(delta);
        glTranslatef(speedx, speedy, 0);

        level1.checkCurrent(x, y);
        level1.draw();
        Display.update();
        Display.sync(60);
    }

Here is where I set the speed:

    if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
    {
        y+=0.5*delta;
        screenY+=0.5*delta;
        speedy=(int) (-0.5*delta);
        direction=2;
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_UP))
    {
        y-=0.5*delta;
        screenY-=0.5*delta;
        speedy=(int) (0.5*delta);
        direction=8;
    }
    else
        speedy=0;

Upvotes: 1

Views: 306

Answers (1)

datenwolf
datenwolf

Reputation: 162367

Right now you're treating OpenGL as if it were a scene graph. However OpenGL is only meant to draw things on the screen. Whatever you do, you should always think about your problem in a way, as if all the rest of the infrastructure wasn't there.

You want to accelerate an object? Well, then you need to increment some speed variable over time and that speed variable multiplied by time adds to the position. In essence Newton's laws of motion:

a = dv/dt => v = a*t + v_0
v = dr/dt => r = v*t + r_0 = a*t² + v_0*t + r_0

This you evaluate for each of your objects. Then when drawing the animation, you use the state to place the object geometry accordingly.

Upvotes: 1

Related Questions