From 7341bf55b6b1bdc53ad59ee54a452e50888db8a4 Mon Sep 17 00:00:00 2001 From: ivanegegia Date: Thu, 30 Apr 2015 13:03:03 +0400 Subject: [PATCH] matter-js, matter-js-tests Added Query declaration to matter-js Added tests for matter-js --- matter-js/matter-js-tests.ts | 57 ++++++++++++++++++++++++++++++++++++ matter-js/matter-js.d.ts | 26 +++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 matter-js/matter-js-tests.ts diff --git a/matter-js/matter-js-tests.ts b/matter-js/matter-js-tests.ts new file mode 100644 index 000000000..cc9b7d99d --- /dev/null +++ b/matter-js/matter-js-tests.ts @@ -0,0 +1,57 @@ +/// + +var Engine = Matter.Engine, + World = Matter.World, + Body = Matter.Body, + Bodies = Matter.Bodies, + Composites = Matter.Composites, + Constraint = Matter.Constraint, + Events = Matter.Events, + Query = Matter.Query + + +var engine = Engine.create(document.body) + +//Bodies +var box1 = Bodies.rectangle(400,200,80,80) +var box2 = Bodies.rectangle(400,610,810,60, { + angle: 10, + angularSpeed: 11, + angularVelocity: 1, + density: 4, + isStatic: true +}) + +var circle1 = Bodies.circle(100,100,50) + +World.addBody(engine.world, box1) +World.add(engine.world, [box2, circle1]) + + +//Composites +var stack = Composites.stack(0, 100, 5, 1, 20, 0, function(x, y, column, row) { + return Bodies.circle(x, y, 75, { restitution: 0.9 }); + }); + +World.add(engine.world, stack); + +//Constraints +var constraint1 = Constraint.create({ + bodyA: box1, + bodyB: box2, + stiffness: 0.02 +}) + +//Query +var collisions = Query.ray([box1, box2, circle1], {x:1, y:2}, {x:3, y:4}); + +World.addConstraint(engine.world, constraint1) + + +//events +Events.on(engine, "beforeTick", (e:any)=>{ + +}) + + +Engine.run(engine) diff --git a/matter-js/matter-js.d.ts b/matter-js/matter-js.d.ts index 1365ccca2..5c4426ea2 100644 --- a/matter-js/matter-js.d.ts +++ b/matter-js/matter-js.d.ts @@ -44,7 +44,7 @@ declare module Matter { /** * Clears the engine including the world, pairs and broadphase. - * @param engine + * @param engine */ static clear(engine:Engine):void; @@ -844,6 +844,30 @@ declare module Matter */ type?:string; } + + export class Query + { + /** + * Casts a ray segment against a set of bodies and returns all collisions, ray width is optional. Intersection points are not provided. + * + * @param bodies + * @param startPoint + * @param endPoint + * @param [rayWidth] + * + * @returns Object[] Collisions + */ + static ray( bodies:Array, startPoint:Vector, endPoint:Vector, rayWidth?:number ):Array; + + /** + * Returns all bodies whose bounds are inside (or outside if set) the given set of bounds, from the given set of bodies. + * + * @param bodies + * @param bounds + * @returns Body[] The bodies matching the query + */ + static region( bodies:Array, bounds:Bounds, outside?:boolean ):Array; + } export class Mouse {