Wrong coordinates in hitbox checks

All functions with hitbox checks (in Bitmap.hx, Tilemap.hx, Video.hx) currently use this logic:

if (px > 0 && py > 0 && px <= __bitmapData.width && py <= __bitmapData.height)

It is 1 pixel wrong for both axes. For example, Mouse.ROLL_OVER event (which is based on hitbox checks) will not trigger when the cursor is 1px over an object from top / left, but will trigger when it is 1px away from bottom / right.

hitbox

To fix this the code should be changed to:

if (px >= 0 && py >= 0 && px < __bitmapData.width && py < __bitmapData.height)
1 Like

Have you had an opportunity to compare this to the Flash target? If this is how Flash handles it then I would suggest offsetting your cursor graphic by (-1, -1)

Let me know if you have a chance to check, thanks :smiley:

Yes, in Flash it’s fairly odd:

if (px > 0 && py >= 0 && px <= __bitmapData.width && py < __bitmapData.height)

I don’t know why they treat X and Y differently, but this has always looked like a bug to me.

Interesting – is there a way to verify this, like a .hitTest() call rather than needing to use a mouse or touch?

I don’t know, I was testing with a mouse. I also don’t know if all hit tests in Flash share one logic (and Mouse.ROLL_OVER is not based on it’s own rules, for example).

Strange so if you have an object at (0, 0) and you move your mouse to (0, 0) in Flash it does not register as a hit? I think that is why we use the logic we have now

Atm OpenFL logic is wrong and not Flash-like.

Does hitTestPoint also work like if (px > 0 && py >= 0 && px <= __bitmapData.width && py < __bitmapData.height)?