# How to pick zoomed objects in an isometric game?

I´m currently working on an isometric game engine and right now I'm looking for help concerning my zoom function.

On my tilemap there are several objects, some of them are selectable. When a house (texture size 128 x 256) is placed on the map I create an array containing all pixels (= 32768 pixels). Therefore each pixel has an alpha value; I check if the alpha value is bigger than 200 so it seems to be a pixel which belongs to the building. So if the mouse cursor is on this pixel the building will be selected -> PixelCollision.

Now I've already implemented my zooming function which works quite well. I use a scale variable which will change my calculation on drawing all map items.

What I'm looking for right now is a precise way to find out if a zoomed out/in house is selected. My formula works for values like 0.5 (zoomed out) or 2 (zoomed in) but not for values in between.

Here is the code I use for the pixel index:

var pixelIndex = (int)(((yPos / (Scale * Scale)) * width) + (xPos / Scale) + 1);


Example:

Let´s assume my mouse is over pixel coordinate (38, 222) on the original house texture. Using the code above we get the following pixel index:

var pixelIndex = ((222 / (1 * 1)) * 128) + (38 / 1) + 1;
= (222 * 128) + 39
= 28416 + 39
= 28455


If we now zoom out to scale 0.5, the texture size will change to 64 x 128 and the amount of pixels will decrease from 32768 to 8192. Of course also our mouse coordinate changes by the scale to (19, 111). The formula makes it easy to calculate the original pixelIndex using our new coordinates:

var pixelIndex = ((111 / (0.5 * 0.5)) * 64) + (19 / 0.5) + 1;
= (444 * 64) + 39
= 28416 + 39
= 28455


But now comes the problem. If I zoom out just to scale 0.75 it does not work any more. The pixel amount changes from 32768 to 18432 pixels since texture size is 96 x 192. Mouse coordinate is transformed to point (28, 166). The formula gives me a wrong pixelIndex.

var pixelIndex = ((166 / (0.75 * 0.75)) * 96) + (28 / 0.75) + 1;
= (295.11 * 96) + 38.33
= 28330.66 + 38.33
= 28369


Does anyone have a clue what's wrong in my code? Must be the first part (28330.66) which causes the calculation problem.

• There is a reason people use camera matrices. The scaling and transition between coordinate systems would be trivial. May 28, 2014 at 21:55
• I´ve heard about that but I don't know how this helps me having a good pixel collision. Or is it easy to handle? May 29, 2014 at 10:34
• I find it best to think of Matrix as box that alters the coordinate system. To start out with you have your normal system with a nice 1 to 1 ratio of texture to screen size with no translation or rotation. When you set the matrix in the spritebatch it automatically pushes all of your coordinates though that box and they come out scaled,rotated,ect... on the screen. The box works both ways though. So when you click on something (your mouse is in the drawn coordinate system) you push it the other way though the matrix and it is now in the correct place in the normal coordinate system. May 29, 2014 at 15:50
• Cool. Just tried your idea in a new project and it works perfect! Thanks! :) May 29, 2014 at 20:48
• No problem best of luck with your project. May 30, 2014 at 15:09