简体   繁体   中英

LibGDX Orthographic Camera: Center the image so borders are symmetrical (java)

I am making a game in LibGDX. I'm using the orthographic camera class to scale the image and it works great. My only issue is with the black border. Since opengl draws the image at 0,0 which is the bottom left corner, the black border when the screen is scaled improperly is either on the top or the right. Ex:

Border on right:

在此处输入图片说明

Border on top:

在此处输入图片说明

What I want to do is make them like this:

Border used to be on right, now it's on right and left, perfectly symmetrical:

在此处输入图片说明

Same for this one. It used to just be on top but now it's evenly on top and bottom:

在此处输入图片说明

Anyone know how to do this? I think it would look muuuch nicer and more professional. Thanks in advance.

EDIT: This is what I tried so far:

Here is what I did so far:

double ratio = 1280 / 720;
double newRatio = width / height;
System.out.println("" + newRatio + "    " + ratio);
System.out.println("" + width + "   " + height);
if(newRatio < ratio){
    System.out.println("herro");
    double x = width / ratio;
    double dif = height - x;
    cam.setPosition(0f, (float)(dif / 2));
}

you will have to center the image at first it would look something like:

image_x = screen_width/2 - image_width/2;
image_y = screen_height/2 - image_height/2;

then in the resize methode provided by LibGDX you will have to change the viewport, it would look like:

 public void resize(int newWidth, int newHeight) {
    Gdx.GL30.glViewport(0, 0, newWidth, newHeight);
 }

the version of the GL depends on your config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM