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 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 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 point 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 point 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.
Thanks!
Yheeky