x


World space to pixel space accuracy

I'm working on a 2D game, and looking through an orthographic camera. However, given the setup I have thus far, it seems that my final output is not as pixel perfect as the numbers I have entered. Here is an example. I have a grid, where cubes are 1.1 units apart from each other. Every few cubes, there is a larger space between them, that is undesired. Obviously since we have to shoot though the camera at objects existing in space, there is an eventual decision made by unity to round to the next pixel.

My question is, has anyone found a good setup for your camera to pixel ratio for iOS4s res of 960x640? so that a unit in unity is more mathematically equivalent to the pixels on screen?

My apologies if this didn't come out clearly, and thanks in advance!

more ▼

asked Dec 04 '10 at 07:27 PM

Matt 15 gravatar image

Matt 15
149 18 19 26

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You could just set up your orthographic camera to have the desired height by editing Size in the Inspector. For an orthographic camera, Size is half of the height of the camera in world units. If you wanted your camera 640 "pixels" tall, you'd set Size to be 320, and make sure all your objects have sizes specified in pixels.

I find that solution to be cumbersome, so what I do is just calculate the pixel ratio:

float pixelRatio = (camera.orthographicSize * 2) / camera.pixelHeight;

Now you can use this ratio to convert pixels to world units to properly size your objects:

someObject.width = pixelRatio * 64; // 64 pixels wide on screen

more ▼

answered Jan 02 '11 at 07:39 AM

d3coy gravatar image

d3coy
252 1 1 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2000
x1037
x381
x83
x28

asked: Dec 04 '10 at 07:27 PM

Seen: 4635 times

Last Updated: Dec 04 '10 at 07:27 PM