Module gamejs/pixelcollision

Image mask. Usefull for pixel perfect collision detection:

Example

var unitMask = new Maks(unitSurface, collisionThresholdAlphaValue);
var spearMask = new Maks(unitSurface, collisionThresholdAlphaValue);
var collide = unitMask.overlap(spearMask);

Class Mask

Instance Methods


Mask (surface, threshold)

Creates an image mask from the given Surface. The alpha of each pixel is checked to see if it is greater than the given threshold. If it is greater then that pixel is set as not colliding.

Parameters

Surface surface used for image mask
Number threshold 0 to 255. defaults to: 255, fully transparent

Mask.prototype.getAt (x, y)

Get bit at position.

Parameters

Number x
Number y

Mask.prototype.getSize ()

Returns

Array the dimensions of the map

Mask.prototype.invert ()

Flip the bits in this map.


Mask.prototype.overlap (otherMask, offset)

Parameters

Mask otherMask
Array offset

Returns

True if the otherMask overlaps with this map.

Mask.prototype.overlapArea (otherMask, offset)

Parameters

gamejs.mask.Mask otherMask
Array offset [x,y]

Returns

the number of overlapping pixels

Mask.prototype.overlapMask (otherMask, offset)

Parameters

gamejs.mask.Mask otherMask
Array offset [x,y]

Returns

a mask of the overlapping pixels

Mask.prototype.overlapRect (otherMask, offset)

Parameters

gamejs.mask.Mask otherMask
Array offset [x,y]

Returns

the overlapping rectangle or null if there is no overlap;

Mask.prototype.setAt (x, y)

Set bit at position.

Parameters

Number x
Number y