collisionmask

Collision Mask Module V1.0
By David Powell
http://www.loadcode.co.uk
Released into the public domain

Table of contents:

Classes:
Interfaces:
Functions:
Interface IOnLoadMaskComplete
Description:Mask Interface for asynchronous loading
Table of contents:Methods:
Method OnLoadMaskComplete:Void(mask:MasterMask, path:String)
Description:The mask has completed loading
Returns:Void
Details: mask : A MasterMask object that was created from the CMSK file
path : The full path of the CMSK file that has completed loading

Function LoadMask:MasterMask(path:String, flags:Int = Mask.DefaultFlags)
Description:Load a CMSK file and return a MasterMask representing it.
Returns:MasterMask
Details: path : The full path of the CMSK file
flags : The mask flags to use
Returns : A MasterMask object representing the loaded CMSK file

Please note that New is called in this function, therefore it should not be used within a main game loop.
Function LoadMaskAsync:Void(path:String, flags:Int = Mask.DefaultFlags, onComplete:IOnLoadMaskComplete)
Description:Begin loading a CMSK file asynchronously
Returns:Void
Details: path : The full path of the CMSK file
flags : The mask flags to use
onComplete : The object to invoke the OnLoadMaskComplete method on when the mask has loaded

As with all asynchronous loading, UpdateAsyncEvents() should be called repeatedly until
the mask has been loaded.

Please note that New is called in this function, therefore it should not be used within a main game loop.
Class Mask
Description:A simple mask, which cannot be scaled or rotated.
Details: Scale and rotate methods on MasterMasks will return this simple Mask.
Table of contents:Methods:
  • CollideCircle ( Method CollideCircle:Bool(maskX:Int, maskY:Int, centreX:Int, centreY:Int, radius:Int) )
  • CollideMask ( Method CollideMask:Bool(maskX:Int, maskY:Int, mask2:Mask, mask2X:Int, mask2Y:Int) )
  • CollidePoint ( Method CollidePoint:Bool(maskX:Int, maskY:Int, pointX:Int, pointY:Int) )
  • CollideRect ( Method CollideRect:Bool(maskX:Int, maskY:Int, rectX:Int, rectY:Int, rectWidth:Int, rectHeight:Int) )
  • DebugDraw ( Method DebugDraw:Void(x:Int, y:Int, alpha:Float = 1.0, red:Float = 255, green:Float = 255, blue:Float = 255) )
  • HandleX ( Method HandleX:Float() Property )
  • HandleY ( Method HandleY:Float() Property )
  • Height ( Method Height:Int() Property )
  • Rotation ( Method Rotation:Float() Property )
  • ScaleX ( Method ScaleX:Float() Property )
  • ScaleY ( Method ScaleY:Float() Property )
  • Threshold ( Method Threshold:Int() Property )
  • Threshold ( Method Threshold:Void(value:Int) Property )
  • Width ( Method Width:Int() Property )
Constants:
Method CollideCircle:Bool(maskX:Int, maskY:Int, centreX:Int, centreY:Int, radius:Int)
Description:Check to see if this mask collides with a circle
Returns:Bool
Details: maskX : The X position of the mask, offset by the masks handle
maskY : The Y position of the mask, offset by the masks handle
centreX : The X position of the centre of the circle
centreY : The Y position of the centre of the circle
radius : The radius of the circle
Returns : True if the mask collides with the circle, False otherwise

Please note that the circle calculation used in this collision check uses a different algorithm to
that used by the Mojo DrawCircle() function. Therefore, this collision will not be pixel perfect against it.
Method CollideMask:Bool(maskX:Int, maskY:Int, mask2:Mask, mask2X:Int, mask2Y:Int)
Description:Check to see if this mask collides with another mask when at the specified locations
Returns:Bool
Details: maskX : The X position of this mask, offset by the masks handle
maskY : The Y position of this mask, offset by the masks handle
mask2 : A second mask to check against this one
mask2X : The X position of the second mask, offset by the masks handle
mask2Y : The Y position of the second mask, offset by the masks handle
Returns : True if the masks collide, False otherwise
Method CollidePoint:Bool(maskX:Int, maskY:Int, pointX:Int, pointY:Int)
Description:Check to see if this mask collides with a point
Returns:Bool
Details: maskX : The X position of the mask, offset by the masks handle
maskY : The Y position of the mask, offset by the masks handle
pointX : The X position of the point
pointY : The Y position of the point
Returns : True if the mask collides with the point, False otherwise
Method CollideRect:Bool(maskX:Int, maskY:Int, rectX:Int, rectY:Int, rectWidth:Int, rectHeight:Int)
Description:Check to see if this mask collides with a rectangle
Returns:Bool
Details: maskX : The X position of the mask, offset by the masks handle
maskY : The Y position of the mask, offset by the masks handle
rectX : The X position of to top-left of the rectangle
rectY : The Y position of to top-left of the rectangle
rectWidth : The width of the rectangle
rectHeight : The height of the rectangle
Returns : True if the mask collides with the rectangle, False otherwise
Method DebugDraw:Void(x:Int, y:Int, alpha:Float = 1.0, red:Float = 255, green:Float = 255, blue:Float = 255)
Description:Draw the mask on the screen for debugging purposes
Returns:Void
Details: x : The X position to draw the mask, offset by the masks handle
y : The Y position to draw the mask, offset by the masks handle
alpha : The alpha level to draw the mask
red : The colour to draw the mask, red component
green : The colour to draw the mask, green component
blue : The colour to draw the mask, blue component

This method should only be used for debugging as it is very slow.
Const DefaultFlags:Int = 0
Description:Default mask flags (no flags set)
Method HandleX:Float() Property
Description:Get the X position of the handle.
Returns:Float
Method HandleY:Float() Property
Description:Get the Y position of the handle.
Returns:Float
Method Height:Int() Property
Description:Get theight of the mask.
Returns:Int
Const MidHandle:Int = 1
Description:Set the mask handle to the middle of the mask
Method Rotation:Float() Property
Description:Get the rotation angle.
Returns:Float
Method ScaleX:Float() Property
Description:Get the X scale.
Returns:Float
Method ScaleY:Float() Property
Description:Get the Y scale.
Returns:Float
Method Threshold:Int() Property
Description:Get the collision threshold.
Returns:Int
Method Threshold:Void(value:Int) Property
Description:Set the collision threshold.
Returns:Void
Method Width:Int() Property
Description:Get the width of the mask.
Returns:Int

Class MasterMask Extends Mask Implements IOnLoadDataComplete
Description:A master mask, which allows scaling and rotation.
Table of contents:Methods:
  • HandleX ( Method HandleX:Void(value:Float) Property )
  • HandleY ( Method HandleY:Void(value:Float) Property )
  • PreAllocateMask ( Method PreAllocateMask:Mask(maxScaleX:Float = 1.0, maxScaleY:Float = 1.0, populate:Bool = False) )
  • Scale ( Method Scale:Mask(scaleX:Float, scaleY:Float, destMask:Mask = Null) )
  • ScaleAndRotate ( Method ScaleAndRotate:Mask(scaleX:Float, scaleY:Float, rotation:Float, destMask:Mask = Null) )
  • SetHandle ( Method SetHandle:Void(tx:Float, ty:Float) )
Method HandleX:Void(value:Float) Property
Description:Set the X position of the handle.
Returns:Void
Method HandleY:Void(value:Float) Property
Description:Set the Y position of the handle.
Returns:Void
Method PreAllocateMask:Mask(maxScaleX:Float = 1.0, maxScaleY:Float = 1.0, populate:Bool = False)
Description:Create a new mask with enough space to store a rotated and scaled mask with the supplied scaling factors
Returns:Mask
Details: If the populate flag is set to True then the MasterMask data is copied to the new Mask

Please note that New is called in this method, therefore it should not be used within a main game loop.
Method Scale:Mask(scaleX:Float, scaleY:Float, destMask:Mask = Null)
Description:Scale the mask by the specified X and Y amounts
Returns:Mask
Details: scaleX : The amount to scale by horizontally
scaleY : The amount to scale by vertically
destMask : The pre-allocated mask to hold the scaled version
Returns : destMask if not Null, otherwise a new Mask

If destMask is supplied then the scaled version is written to it.

If destMask is Null then then a new mask containing the scaled version is returned.
Please note that New is called if a destMask is not supplied, therefore a pre-allocated
mask should always be supplied if this method is used within a main game loop.
Method ScaleAndRotate:Mask(scaleX:Float, scaleY:Float, rotation:Float, destMask:Mask = Null)
Description:Scale the mask by the specified X and Y amounts and rotate by the specified angle
Returns:Mask
Details: scaleX : The amount to scale by horizontally
scaleY : The amount to scale by vertically
rotation : The amount to rotate by (in degrees)
destMask : The pre-allocated mask to hold the scaled version
Returns : destMask if not Null, otherwise a new Mask

If destMask is supplied then the scaled and rotated version is written to it.

If destMask is Null then then a new mask containing the scaled and rotated version is returned.
Please note that New is called if a destMask is not supplied, therefore a pre-allocated
mask should always be supplied if this method is used within a main game loop.
Method SetHandle:Void(tx:Float, ty:Float)
Description:Set the X and Y position of the handle.
Returns:Void
Details: tx : The X position of the handle
ty : The Y position of the handle

This document was generated using the Jungle IDE document generator version 14.07.10-A