What is up with this stretching? Can't get camera right.

Hey guys…I’ve fallen off for a little while but getting back into it now…I’m having trouble getting my camera to work right…I have a 30x30 map or 32x32 tiles. I set the screen size to 960x960 as this would show all tiles…When I resize the window, it stretches or makes everything smaller if I make the window bigger or smaller. I’ve been searching for a while and I know it’s something dumb that i’m missing, but I can’t put my finger on it. I’ve got my main class and my map class


public void create() {		
		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		
		float aspectRatio = h/w;
		
		batch = new SpriteBatch();
		
		camera = new OrthographicCamera();
		//1 is temporary camera x and y
		camera.setToOrtho(false,(w/h)*1,(w/h)*1);
		
		
	
		camera.update();
		
		map = new map(camera,"map");
		map.setRenderView(camera);
			}

map class




import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.*;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class map {
	OrthogonalTiledMapRenderer render;
	TiledMap currentMap;
	MapLayer mLayer1;
	
	float unitScale = 1f/32f;
	
	public map(OrthographicCamera cam, String mapName) {
		
		currentMap = new TmxMapLoader().load("data/test1.tmx");
		render = new OrthogonalTiledMapRenderer(currentMap,unitScale);
		
		
	}

	public void setRenderView(OrthographicCamera cam) {
		render.setView(cam);
	}
	
	public void render(SpriteBatch batch) {
		render.render();
		
	}
	
}

I’ve been messing with the code so much from searching and playing with it that now it seems like it’s rendering 1 HUGE tile to the screen. i’m sure it has something to do with the SetToOrtho call but i’m not exactly sure how to fix it.

I fixed the stretching finally by applying the viewport to the render method. The wiki helped.


public void render() {		
		GL10 gl = Gdx.graphics.getGL10();
		gl.glClearColor(1, 1, 1, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glViewport((int)glViewport.x, (int)glViewport.y, (int)glViewport.width,(int) glViewport.height);
		camera.update();
		camera.apply(gl);
		
		batch.begin();
		map.render(batch);
		batch.end();
	}

It’s frustrating as hell getting back into this.

I lied…I still haven’t gotten it. all i’ve been able to do is move the viewport for the camera around…therefore moving the actual camera and not the level/map…

Here’s what I do to avoid stretching:

public void resize(int width, int height) {
	camera.setToOrtho(false, width, height);
}

In other words: I create the camera anew when the display is resized, somewhat like in this tutorial. However, this does not guarantee that all tiles are inside the screen. Here’s a good answer on StackOverflow that discusses an alternative resize method that maintains aspect ratio and may be of use to you.