me.collision

A singleton for managing collision detection (and projection-based collision response) of 2D shapes.
Based on the Separating Axis Theorem and supports detecting collisions between simple Axis-Aligned Boxes, convex polygons and circles based shapes.

Members

static bounds :me.Rect

bounds of the physic world.

static maxChildren :Number

The maximum number of children that a quadtree node can contain before it is split into sub-nodes.

Default Value:
  • 8
See:

static maxDepth :Number

The maximum number of levels that the quadtree will create.

Default Value:
  • 4
See:

static quadTree :me.QuadTree

the world quadtree used for the collision broadphase

static response :me.collision.ResponseObject

a global instance of a response object used for collision detection
this object will be reused amongst collision detection call if not user-defined response is specified

static ResponseObject :Object

An object representing the result of an intersection.

Properties:
Name Type Description
a me.Renderable

The first object participating in the intersection

b me.Renderable

The second object participating in the intersection

overlap Number

Magnitude of the overlap on the shortest colliding axis

overlapV me.Vector2d

The overlap vector (i.e. overlapN.scale(overlap, overlap)). If this vector is subtracted from the position of a, a and b will no longer be colliding

overlapN me.Vector2d

The shortest colliding axis (unit-vector)

aInB Boolean

Whether the first object is entirely inside the second

bInA Boolean

Whether the second object is entirely inside the first

indexShapeA Number

The index of the colliding shape for the object a body

indexShapeB Number

The index of the colliding shape for the object b body

See:

static, readonly types :Number

Enum for collision type values.

Properties:
Name Type Description
NO_OBJECT

to disable collision check

PLAYER_OBJECT
NPC_OBJECT
ENEMY_OBJECT
COLLECTABLE_OBJECT
ACTION_OBJECT

e.g. doors

PROJECTILE_OBJECT

e.g. missiles

WORLD_SHAPE

e.g. walls; for map collision shapes

USER

user-defined collision types (see example)

ALL_OBJECT

all of the above (including user-defined types)

See:
  • me.body.setCollisionMask
  • me.body.collisionType
Example
// set the entity body collision type
myEntity.body.collisionType = me.collision.types.PLAYER_OBJECT;

// filter collision detection with collision shapes, enemies and collectables
myEntity.body.setCollisionMask(
    me.collision.types.WORLD_SHAPE |
    me.collision.types.ENEMY_OBJECT |
    me.collision.types.COLLECTABLE_OBJECT
);

// User-defined collision types are defined using BITWISE LEFT-SHIFT:
game.collisionTypes = {
    LOCKED_DOOR : me.collision.types.USER << 0,
    OPEN_DOOR   : me.collision.types.USER << 1,
    LOOT        : me.collision.types.USER << 2,
};

// Set collision type for a door entity
myDoorEntity.body.collisionType = game.collisionTypes.LOCKED_DOOR;

// Set collision mask for the player entity, so it collides with locked doors and loot
myPlayerEntity.body.setCollisionMask(
    me.collision.types.ENEMY_OBJECT |
    me.collision.types.WORLD_SHAPE |
    game.collisionTypes.LOCKED_DOOR |
    game.collisionTypes.LOOT
);

Methods

static check(obj, respObjopt) → {Boolean}

Checks if the specified entity collides with others entities

Parameters:
Name Type Attributes Default Description
obj me.Renderable

entity to be tested for collision

respObj me.collision.ResponseObject <optional>
me.collision.response

a user defined response object that will be populated if they intersect.

Returns:
Boolean -

in case of collision, false otherwise

Example
update : function (dt) {
   // ...

   // handle collisions against other shapes
   me.collision.check(this);

   // ...
},

// colision handler
onCollision : function (response) {
    if (response.b.body.collisionType === me.collision.types.ENEMY_OBJECT) {
        // makes the other entity solid, by substracting the overlap vector to the current position
        this.pos.sub(response.overlapV);
        this.hurt();
        // not solid
        return false;
    }
    // Make the object solid
    return true;
},

static rayCast(line, resultopt) → {Array.<me.Renderable>}

Checks for object colliding with the given line

Parameters:
Name Type Attributes Description
line me.Line

line to be tested for collision

result Array.<me.Renderable> <optional>

a user defined array that will be populated with intersecting physic objects.

Returns:
Array.<me.Renderable> -

an array of intersecting physic objects

Example
// define a line accross the viewport
   var ray = new me.Line(
       // absolute position of the line
       0, 0, [
       // starting point relative to the initial position
       new me.Vector2d(0, 0),
       // ending point
       new me.Vector2d(me.game.viewport.width, me.game.viewport.height)
   ]);

   // check for collition
   result = me.collision.rayCast(ray);

   if (result.length > 0) {
       // ...
   }

static shouldCollide(a, b) → {Boolean}

a callback used to determine if two objects should collide (based on both respective objects collision mask and type).
you can redefine this function if you need any specific rules over what should collide with what.

Parameters:
Name Type Description
a me.Renderable

a reference to the object A.

b me.Renderable

a reference to the object B.

Returns:
Boolean -

true if they should collide, false otherwise