Making a working "camera" in a 2D orthogonal environment

Started by CodeBunny, October 17, 2010, 14:39:27

Previous topic - Next topic

CodeBunny

This is what I came up with:

        Camera c = DataManager.getCurrentCamera();
       
        GL11.glScaled(c.getZoom(), c.getZoom(), 1);
        double w = Display.getDisplayMode().getWidth()/2;
        double h = Display.getDisplayMode().getHeight()/2;
        GL11.glTranslated(-c.getX() + w / c.getZoom(), -c.getY() + h / c.getZoom(), 0);


I put it right after I draw the background of the scene. It was the simplest I could get it.

Wolftein

Here is an example of my camera
    private void renderCamera(Camera camera) {
        // Size And Position
        Vector3f pos = camera.getPosition();
        Vector3f size = camera.getViewport();
        Vector3f center = camera.getCenter();
        // Do we need to zoom the camera?
        float zoomFactor = camera.getZoom();
        if (zoomFactor != 1.0f) {
            GL11.glViewport(0, 0, (int) size.x, (int) size.y);
            GL11.glOrtho(0, size.x / zoomFactor, size.y / zoomFactor, 0, -100, 100);
        }
        // Move the matrix with teh camerca
        GL11.glTranslatef(-pos.x + center.x, -pos.y + center.y, pos.z);
        GL11.glRotatef(camera.getRotation(), 0.0f, 0.0f, 1.0f);
        GL11.glTranslatef(-center.x, -center.y, -pos.z);
    }

CodeBunny

I see. Very nice.

Thanks for all the help, everyone.