# Screen to World coordinates in Box2D

In this blog entry I'm going to talk about how to manage the coordinates system in your Box2D world. The problem arises because normally in games we want to use two sets of coordinates - one for the Box2D world and one for the screen output. Time and again I come across people using sub-optimal methods for managing the two sets of coordinates.

### Mistake 1: Pixel coordinates

When people start out with Box2D normally they create a Box2D world with the same dimensions as the screen on which their world will be displayed i.e. pixel dimensions. If the game runs at 1000px x 800px then the Box2D world has dimensions 1000m x 800m. This is a simple and easy solution but can cause all sorts of problems.

Firstly, Box2D is optimised to run using real world dimensions i.e. objects ranging in size from 0.1m to 10m. Having 100m meter objects flying around will reduce performance and could lead to bugs and glitches.

Secondly, this method is horribly inflexible. What if you want to modify your game to run at 500px x 400px? The short answer is that you can't!

### Mistake 2: SCREEN_TO_WORLD

The second method I see is to use a global parameter SCREEN_TO_WORLD which contains the ratio of Box2D units to screen pixels. This method allows you to run your Box2D world with sensible dimensions but still doesn't offer much flexibility.

What if you want to zoom in on a section of your world? What if you want to pan across your world like in Angry Birds? What if you want the coordinates from your world to go from -50 to +50 but your screen needs to go from 0 to +500? The answer to all these questions is simply "you can't!".

This method is also hacky! Your code ends up littered with references to SCREEN_TO_WORLD and WORLD_TO_SCREEN which isn't object orientated or flexible. It makes the code hard to read which in turn makes it much easier to make mistakes.

### Solution: Camera class

So what's the solution? The answer is to use a Camera class to manage the conversion from screen to world. A camera class emulates the behavior of a camera moving over the Box2D world. The output of the camera is completely independent of what it's recording. The camera can pan, rotate and zoom without affecting the Box2D world in any way. You create your whole game using world coordinates and then at the end the camera converts everything to screen coordinates. Simple and clean.

I'm not going to go into the maths to work this out - it took me an evening of scribbling diagrams on scraps of paper to work it out but if you're interested then you can read more about Affine Transforms.

Below is my camera class:

package library.physics; import org.jbox2d.common.Vec2; public class Camera { public Vec2 rotationOrigin = new Vec2(); public float rotation = 0; public boolean updated = false; public Vec2 scaleWorldToScreen = new Vec2(); public Vec2 scaleScreenToWorld = new Vec2(); // Coordinate of bottom left hand corner of screen public Vec2 screenMin; // Coordinate of top right hand corner of screen public Vec2 screenMax; // Coordinate of bottom left hand corner of world public Vec2 worldMin; // Coordinate of bottom top right corner of world public Vec2 worldMax; /* * Screen max/min in world - i.e. if the world extends from 0 - 100 but you only want to display * from 50 - 100 then screenMinInWorld = (50, 50) screenMaxInWorld(100, 100) */ public Vec2 screenMinInWorld; public Vec2 screenMaxInWorld; public Camera(Vec2 worldMin, Vec2 worldMax, Vec2 screenMin, Vec2 screenMax, Vec2 screenMinInWorld, Vec2 screenMaxInWorld) { setCamera(worldMin, worldMax, screenMin, screenMax, screenMinInWorld, screenMaxInWorld); } public Camera( Vec2 worldMax, Vec2 screenMax, Vec2 screenMinInWorld, Vec2 screenMaxInWorld) { setCamera(new Vec2(), worldMax, new Vec2(), screenMax, screenMinInWorld, screenMaxInWorld); } public Camera( Vec2 worldMax, Vec2 screenMax) { setCamera(new Vec2(), worldMax, new Vec2(), screenMax, new Vec2(), worldMax); } public void setCamera(Vec2 worldMin, Vec2 worldMax, Vec2 screenMin, Vec2 screenMax, Vec2 screenMinInWorld, Vec2 screenMaxInWorld) { scaleWorldToScreen.x = (screenMax.x-screenMin.x)/(screenMaxInWorld.x-screenMinInWorld.x); scaleWorldToScreen.y = (screenMax.y-screenMin.y)/(screenMaxInWorld.y-screenMinInWorld.y); scaleScreenToWorld.x = 1/scaleWorldToScreen.x; scaleScreenToWorld.y = 1/scaleWorldToScreen.y; this.screenMin = screenMin; this.screenMax = screenMax; this.worldMin = worldMin; this.worldMax = worldMax; this.screenMinInWorld = screenMinInWorld; this.screenMaxInWorld = screenMaxInWorld; } public Vec2 worldToScreen(float x, float y) { return worldToScreen(new Vec2(x,y)); } public Vec2 worldToScreen(Vec2 world) { Vec2 screen = new Vec2(); screen.x = screenMin.x + scaleWorldToScreen.x * (world.x - worldMin.x - screenMinInWorld.x); screen.y = screenMin.y + scaleWorldToScreen.y * (world.y - worldMin.y - screenMinInWorld.y); return screen; } public Vec2 screenToWorld(float x, float y) { return screenToWorld(new Vec2(x,y)); } public Vec2 screenToWorld(Vec2 screen) { Vec2 world = new Vec2(); world.x = (screen.x - screenMin.x)*scaleScreenToWorld.x + worldMin.x + screenMinInWorld.x; world.y = (screen.y - screenMin.y)*scaleScreenToWorld.y + worldMin.y + screenMinInWorld.y; return world; } public float worldWidth() { return (worldMax.x - worldMin.x); } public float worldHeight() { return (worldMax.y - worldMin.y); } public float screenWidth() { return (screenMax.x - screenMin.x); } public float screenHeight() { return (screenMax.y - screenMin.y); } s += screenMin.toString()+" -> "+screenToWorld(screenMin).toString()+"\n"; s += screenMax.toString()+" -> "+screenToWorld(screenMax).toString(); return s; } // Used if the screen dimensions change i.e. if a user adjusts the // dimensions of the window public void reshape(float x1, float y1, float x2, float y2 ) { this.screenMin.set(x1, y1); this.screenMax.set(x2, y2); setCamera(worldMin, worldMax, screenMin, screenMax, screenMinInWorld, screenMaxInWorld); } // Pan by a vector public void pan (Vec2 pan) { screenMinInWorld = screenMinInWorld.add(pan); screenMaxInWorld = screenMaxInWorld.add(pan); updated = true; } // Zoom by a vector public void zoom (Vec2 zoom) { screenMinInWorld = screenMinInWorld.add(zoom); screenMaxInWorld = screenMaxInWorld.sub(zoom); updated = true; } // rotate by a certain angle public void rotate(float amount) { rotation += amount; updated = true; } }

So there we have it. Just define this class at the start of your project then use screenToWorld and worldToScreen to perform coordinate conversions.

### Using the Camera class with OpenGL

If you are using OpenGL with an orthographic camera you can make your life even easier by including the following in your initialisation code:

gl.glOrtho(camera.screenMinInWorld.x, camera.screenMaxInWorld.x, camera.screenMinInWorld.y, camera.screenMaxInWorld.y, -1.0, 1.0);

Then the following in your OpenGL display method:

public void display(GL2 gl) { // Move the camera position if(camera.updated == true) { gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(camera.screenMinInWorld.x, camera.screenMaxInWorld.x, camera.screenMinInWorld.y, camera.screenMaxInWorld.y, -1.0, 1.0); gl.glTranslatef(camera.worldWidth()/2, camera.worldHeight()/2, 0); gl.glRotatef(-camera.rotation, 0, 0, 1); gl.glTranslatef(-camera.worldWidth()/2, -camera.worldHeight()/2, 0); } //...your display code... }

Finally if you want to support screen reshapes include the following in your OpenGL reshape method:

public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int arg3, int arg4) { camera.reshape(arg1, arg2, arg3, arg4); }

## Add new comment