Show:

File: src/collision/Pair.js

                                /**
                                * The `Matter.Pair` module contains methods for creating and manipulating collision pairs.
                                *
                                * @class Pair
                                */
                                
                                var Pair = {};
                                
                                module.exports = Pair;
                                
                                var Contact = require('./Contact');
                                
                                (function() {
                                    
                                    /**
                                     * Creates a pair.
                                     * @method create
                                     * @param {collision} collision
                                     * @param {number} timestamp
                                     * @return {pair} A new pair
                                     */
                                    Pair.create = function(collision, timestamp) {
                                        var bodyA = collision.bodyA,
                                            bodyB = collision.bodyB;
                                
                                        var pair = {
                                            id: Pair.id(bodyA, bodyB),
                                            bodyA: bodyA,
                                            bodyB: bodyB,
                                            collision: collision,
                                            contacts: [],
                                            activeContacts: [],
                                            separation: 0,
                                            isActive: true,
                                            confirmedActive: true,
                                            isSensor: bodyA.isSensor || bodyB.isSensor,
                                            timeCreated: timestamp,
                                            timeUpdated: timestamp,
                                            inverseMass: 0,
                                            friction: 0,
                                            frictionStatic: 0,
                                            restitution: 0,
                                            slop: 0
                                        };
                                
                                        Pair.update(pair, collision, timestamp);
                                
                                        return pair;
                                    };
                                
                                    /**
                                     * Updates a pair given a collision.
                                     * @method update
                                     * @param {pair} pair
                                     * @param {collision} collision
                                     * @param {number} timestamp
                                     */
                                    Pair.update = function(pair, collision, timestamp) {
                                        var contacts = pair.contacts,
                                            supports = collision.supports,
                                            activeContacts = pair.activeContacts,
                                            parentA = collision.parentA,
                                            parentB = collision.parentB,
                                            parentAVerticesLength = parentA.vertices.length;
                                        
                                        pair.isActive = true;
                                        pair.timeUpdated = timestamp;
                                        pair.collision = collision;
                                        pair.separation = collision.depth;
                                        pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
                                        pair.friction = parentA.friction < parentB.friction ? parentA.friction : parentB.friction;
                                        pair.frictionStatic = parentA.frictionStatic > parentB.frictionStatic ? parentA.frictionStatic : parentB.frictionStatic;
                                        pair.restitution = parentA.restitution > parentB.restitution ? parentA.restitution : parentB.restitution;
                                        pair.slop = parentA.slop > parentB.slop ? parentA.slop : parentB.slop;
                                
                                        collision.pair = pair;
                                        activeContacts.length = 0;
                                        
                                        for (var i = 0; i < supports.length; i++) {
                                            var support = supports[i],
                                                contactId = support.body === parentA ? support.index : parentAVerticesLength + support.index,
                                                contact = contacts[contactId];
                                
                                            if (contact) {
                                                activeContacts.push(contact);
                                            } else {
                                                activeContacts.push(contacts[contactId] = Contact.create(support));
                                            }
                                        }
                                    };
                                    
                                    /**
                                     * Set a pair as active or inactive.
                                     * @method setActive
                                     * @param {pair} pair
                                     * @param {bool} isActive
                                     * @param {number} timestamp
                                     */
                                    Pair.setActive = function(pair, isActive, timestamp) {
                                        if (isActive) {
                                            pair.isActive = true;
                                            pair.timeUpdated = timestamp;
                                        } else {
                                            pair.isActive = false;
                                            pair.activeContacts.length = 0;
                                        }
                                    };
                                
                                    /**
                                     * Get the id for the given pair.
                                     * @method id
                                     * @param {body} bodyA
                                     * @param {body} bodyB
                                     * @return {string} Unique pairId
                                     */
                                    Pair.id = function(bodyA, bodyB) {
                                        if (bodyA.id < bodyB.id) {
                                            return 'A' + bodyA.id + 'B' + bodyB.id;
                                        } else {
                                            return 'A' + bodyB.id + 'B' + bodyA.id;
                                        }
                                    };
                                
                                })();
                                
                                    
0.18.0