Namespace: collision

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.
Source:
collision.js, line 187

Members

<static> bounds :me.Rect

bounds of the physic world.
Type:
Source:
collision.js, line 231

<static> maxChildren :boolean

The maximum number of children that a quadtree node can contain before it is split into sub-nodes. Default is 8.
Type:
  • boolean
Source:
collision.js, line 221
See:

<static> maxDepth :number

The maximum number of levels that the quadtree will create. Default is 4.
Type:
  • number
Source:
collision.js, line 210
See:

<static> quadTree :me.QuadTree

the world quadtree used for the collision broadphase
Type:
Source:
collision.js, line 201

<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
Type:
Source:
collision.js, line 368

<static> ResponseObject :Object

An object representing the result of an intersection.
Type:
Properties:
Name Type Description
a me.Entity The first object participating in the intersection
b me.Entity 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
Source:
collision.js, line 320
See:

<static, readonly> types :Number

Enum for collision type values.
Type:
  • Number
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)
Source:
collision.js, line 240
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, respObj) → {Boolean}

Checks if the specified entity collides with others entities
Parameters:
Name Type Argument Default Description
obj me.Entity 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.
Source:
collision.js, line 397
Returns:
in case of collision, false otherwise
Type
Boolean
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> 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.Entity a reference to the object A.
b me.Entity a reference to the object B.
Source:
collision.js, line 378
Returns:
true if they should collide, false otherwise
Type
Boolean
Copyright © 2011 - 2017, Olivier Biot, Jason Oster, Aaron McLeod
Documentation generated by JSDoc 3.4.0 on 2017-02-28T11:37:40+08:00 using the DocStrap template modified by codename-.