olsn / Collision-Detection-for-EaselJS

Pixel Perfect and BoundingBox Collision Detector for EaselJS Bitmaps and BitmapAnimations
170 stars 53 forks source link

Detection fails on scaleX = -1 #4

Open Cosmitar opened 10 years ago

Cosmitar commented 10 years ago

Hi, im testing this great library playing with some sprites and I found that no collision is detected when I reflect the sprite with scaleX = -1 To solve and check this, I save and restore scaleX when it is = -1

var scaleX = mySprite.scaleX; //save
if( mySprite.scaleX == -1 ){ mySprite.scaleX = 1; } //fix
collision = ndgmr.checkPixelCollision( someSprite, mySprite );
mySprite.scaleX = scaleX; //restore

I guess this is a bug. Hope this help!

olsn commented 10 years ago

Hi, I cannot say currently when I will have the time to look into this, but I have noted it for now and will take a look at it as soon as possible.

martialc commented 10 years ago

It also fails on scaleY = -1.

Which becomes quite a bigger problem with the quick fix above (collisions are detected as if the object was not vertically scaled, which is the point of the patch, but gives wrong collision coordinates).

Another quickfix, with better results :

In _collisionDistancePrecheck = function(bitmap1,bitmap2) :

Change the return value to:

return(Math.abs(b2.x - b1.x) < ir2.width  * Math.abs(bitmap2.scaleX) + ir1.width  * Math.abs(bitmap1.scaleX)
    && Math.abs(b2.y - b1.y) < ir2.height * Math.abs(bitmap2.scaleY) + ir1.height * Math.abs(bitmap2.scaleY))