Show:

File: src/collision/Pairs.js

                                /**
                                * The `Matter.Pairs` module contains methods for creating and manipulating collision pair sets.
                                *
                                * @class Pairs
                                */
                                
                                var Pairs = {};
                                
                                module.exports = Pairs;
                                
                                var Pair = require('./Pair');
                                var Common = require('../core/Common');
                                
                                (function() {
                                
                                    /**
                                     * Creates a new pairs structure.
                                     * @method create
                                     * @param {object} options
                                     * @return {pairs} A new pairs structure
                                     */
                                    Pairs.create = function(options) {
                                        return Common.extend({ 
                                            table: {},
                                            list: [],
                                            collisionStart: [],
                                            collisionActive: [],
                                            collisionEnd: []
                                        }, options);
                                    };
                                
                                    /**
                                     * Updates pairs given a list of collisions.
                                     * @method update
                                     * @param {object} pairs
                                     * @param {collision[]} collisions
                                     * @param {number} timestamp
                                     */
                                    Pairs.update = function(pairs, collisions, timestamp) {
                                        var pairsList = pairs.list,
                                            pairsListLength = pairsList.length,
                                            pairsTable = pairs.table,
                                            collisionsLength = collisions.length,
                                            collisionStart = pairs.collisionStart,
                                            collisionEnd = pairs.collisionEnd,
                                            collisionActive = pairs.collisionActive,
                                            collision,
                                            pairIndex,
                                            pair,
                                            i;
                                
                                        // clear collision state arrays, but maintain old reference
                                        collisionStart.length = 0;
                                        collisionEnd.length = 0;
                                        collisionActive.length = 0;
                                
                                        for (i = 0; i < pairsListLength; i++) {
                                            pairsList[i].confirmedActive = false;
                                        }
                                
                                        for (i = 0; i < collisionsLength; i++) {
                                            collision = collisions[i];
                                            pair = collision.pair;
                                
                                            if (pair) {
                                                // pair already exists (but may or may not be active)
                                                if (pair.isActive) {
                                                    // pair exists and is active
                                                    collisionActive.push(pair);
                                                } else {
                                                    // pair exists but was inactive, so a collision has just started again
                                                    collisionStart.push(pair);
                                                }
                                
                                                // update the pair
                                                Pair.update(pair, collision, timestamp);
                                                pair.confirmedActive = true;
                                            } else {
                                                // pair did not exist, create a new pair
                                                pair = Pair.create(collision, timestamp);
                                                pairsTable[pair.id] = pair;
                                
                                                // push the new pair
                                                collisionStart.push(pair);
                                                pairsList.push(pair);
                                            }
                                        }
                                
                                        // find pairs that are no longer active
                                        var removePairIndex = [];
                                        pairsListLength = pairsList.length;
                                
                                        for (i = 0; i < pairsListLength; i++) {
                                            pair = pairsList[i];
                                            
                                            if (!pair.confirmedActive) {
                                                Pair.setActive(pair, false, timestamp);
                                                collisionEnd.push(pair);
                                
                                                if (!pair.collision.bodyA.isSleeping && !pair.collision.bodyB.isSleeping) {
                                                    removePairIndex.push(i);
                                                }
                                            }
                                        }
                                
                                        // remove inactive pairs
                                        for (i = 0; i < removePairIndex.length; i++) {
                                            pairIndex = removePairIndex[i] - i;
                                            pair = pairsList[pairIndex];
                                            pairsList.splice(pairIndex, 1);
                                            delete pairsTable[pair.id];
                                        }
                                    };
                                
                                    /**
                                     * Clears the given pairs structure.
                                     * @method clear
                                     * @param {pairs} pairs
                                     * @return {pairs} pairs
                                     */
                                    Pairs.clear = function(pairs) {
                                        pairs.table = {};
                                        pairs.list.length = 0;
                                        pairs.collisionStart.length = 0;
                                        pairs.collisionActive.length = 0;
                                        pairs.collisionEnd.length = 0;
                                        return pairs;
                                    };
                                
                                })();
                                
                                    
0.18.0