0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-27 13:59:01 -05:00

added Query module, added Query.ray

This commit is contained in:
liabru 2014-04-24 12:21:14 +01:00
parent 5e2a1e5df9
commit a3a475fce7
2 changed files with 44 additions and 0 deletions

43
src/collision/Query.js Normal file
View file

@ -0,0 +1,43 @@
/**
* Functions for performing collision queries
*
* @class Query
*/
var Query = {};
(function() {
/**
* Casts a ray segment against a set of bodies and returns all collisions, ray width is optional. Intersection points are not provided.
* @method ray
* @param {body[]} bodies
* @param {vector} startPoint
* @param {vector} endPoint
* @return {object[]} Collisions
*/
Query.ray = function(bodies, startPoint, endPoint, rayWidth) {
var rayAngle = Vector.angle(startPoint, endPoint),
rayWidth = rayWidth || Number.MIN_VALUE,
rayLength = Vector.magnitude(Vector.sub(startPoint, endPoint)),
rayX = (endPoint.x + startPoint.x) * 0.5,
rayY = (endPoint.y + startPoint.y) * 0.5,
ray = Bodies.rectangle(rayX, rayY, rayLength, rayWidth, { angle: rayAngle }),
collisions = [];
for (var i = 0; i < bodies.length; i++) {
var bodyA = bodies[i];
if (Bounds.overlaps(bodyA.bounds, ray.bounds)) {
var collision = SAT.collides(bodyA, ray);
if (collision.collided) {
collision.body = collision.bodyA = collision.bodyB = bodyA;
collisions.push(collision);
}
}
}
return collisions;
};
})();

View file

@ -36,6 +36,7 @@ Matter.Gui = Gui;
Matter.Render = Render;
Matter.RenderPixi = RenderPixi;
Matter.Events = Events;
Matter.Query = Query;
// CommonJS module
if (typeof exports !== 'undefined') {