Skip to content

Instantly share code, notes, and snippets.

@worldoptimizer
Created September 12, 2020 12:55
Show Gist options
  • Save worldoptimizer/82e2bd5ad57918f14552672f0a210598 to your computer and use it in GitHub Desktop.
Save worldoptimizer/82e2bd5ad57918f14552672f0a210598 to your computer and use it in GitHub Desktop.
hypeDocument.queryIntersections
/**
* Query intersections between elements using Matter.js
*
* @param {Node} sourceElm The source element used in each check
* @param {Nodelist} targetElms The target elements to query against
* @return {Nodelist} Returns a list of targetElms that intersect with sourceElm
*/
hypeDocument.queryIntersections = function(sourceElm, targetElms) {
var sourceBody = hypeDocument.getElementProperty(sourceElm, 'physics-body');
if (sourceBody) {
var targetBodies = [];
targetElms.forEach(function(elm){
var elmBody = hypeDocument.getElementProperty(elm, 'physics-body');
if (elmBody) {
targetBodies.push(elmBody);
} else {
console.log('target '+elm.id+' must have static Physics enabled');
}
});
var collisions = Matter.Query.collides(sourceBody, targetBodies);
if (collisions.length) {
return collisions.map(function(collision){
var isB = sourceElm.id == collision.bodyB.elementId;
return document.getElementById((isB? collision.bodyA : collision.bodyB).elementId);
});
}
} else {
console.log('sourceElm must have static Physics enabled');
}
return [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment