Skip to content

Commit

Permalink
Merged sensors from Misiur-master
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Apr 5, 2016
2 parents 1944f6a + 28084b0 commit e1a52d5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<li><a href="http://brm.io/matter-js/demo#beachBalls">Beach Balls</a></li>
<li><a href="http://brm.io/matter-js/demo#stress">Stress 1</a></li>
<li><a href="http://brm.io/matter-js/demo#stress2">Stress 2</a></li>
<li><a href="http://brm.io/matter-js/demo#sensors">Sensors</a></li>
</ul>
<br>
</td>
Expand Down
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ <h1>Matter.js Demo</h1>
<option value="cloth">Cloth</option>
<option value="events">Events</option>
<option value="collisionFiltering">Collision Filtering</option>
<option value="sensors">Sensors</option>
<option value="chains">Chains</option>
<option value="ballPool">Ball Pool</option>
<option value="stack">Stack</option>
Expand Down
70 changes: 70 additions & 0 deletions examples/sensors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
(function() {

var World = Matter.World,
Bodies = Matter.Bodies,
Common = Matter.Common,
Events = Matter.Events;

Example.sensors = function(demo) {
var engine = demo.engine,
world = engine.world,
sceneEvents = demo.sceneEvents;

var redColor = '#C44D58',
greenColor = '#C7F464';

var collider = Bodies.rectangle(400, 300, 500, 50, {
isSensor: true,
isStatic: true,
render: {
strokeStyle: redColor,
fillStyle: 'transparent'
}
});

World.add(world, collider);

World.add(world,
Bodies.circle(400, 40, 30, {
render: {
strokeStyle: greenColor,
fillStyle: 'transparent'
}
})
);

sceneEvents.push(
Events.on(engine, 'collisionStart', function(event) {
var pairs = event.pairs;

for (var i = 0, j = pairs.length; i != j; ++i) {
var pair = pairs[i];

if (pair.bodyA === collider) {
pair.bodyB.render.strokeStyle = redColor;
} else if (pair.bodyB === collider) {
pair.bodyA.render.strokeStyle = redColor;
}
}
}),
Events.on(engine, 'collisionEnd', function(event) {
var pairs = event.pairs;

for (var i = 0, j = pairs.length; i != j; ++i) {
var pair = pairs[i];

if (pair.bodyA === collider) {
pair.bodyB.render.strokeStyle = greenColor;
} else if (pair.bodyB === collider) {
pair.bodyA.render.strokeStyle = greenColor;
}
}
})
);

var renderOptions = engine.render.options;
renderOptions.wireframes = false;
renderOptions.background = '#222';
renderOptions.showAngleIndicator = false;
};
})();
9 changes: 9 additions & 0 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var Axes = require('../geometry/Axes');
angularSpeed: 0,
velocity: { x: 0, y: 0 },
angularVelocity: 0,
isSensor: false,
isStatic: false,
isSleeping: false,
motion: 0,
Expand Down Expand Up @@ -788,6 +789,14 @@ var Axes = require('../geometry/Axes');
* @default false
*/

/**
* A flag that indicates whether a body is a sensor. Sensor triggers collision events, but doesn't react with colliding body physically.
*
* @property isSensor
* @type boolean
* @default false
*/

/**
* A flag that indicates whether the body is considered sleeping. A sleeping body acts similar to a static body, except it is only temporary and can be awoken.
* If you need to set a body as sleeping, you should use `Sleeping.set` as this requires more than just setting this flag.
Expand Down
1 change: 1 addition & 0 deletions src/collision/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var Contact = require('./Contact');
activeContacts: [],
separation: 0,
isActive: true,
isSensor: bodyA.isSensor || bodyB.isSensor,
timeCreated: timestamp,
timeUpdated: timestamp,
inverseMass: parentA.inverseMass + parentB.inverseMass,
Expand Down
14 changes: 7 additions & 7 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ var Bounds = require('../geometry/Bounds');
for (i = 0; i < pairs.length; i++) {
pair = pairs[i];

if (!pair.isActive)
if (!pair.isActive || pair.isSensor)
continue;

collision = pair.collision;
bodyA = collision.parentA;
bodyB = collision.parentB;
Expand All @@ -89,15 +89,15 @@ var Bounds = require('../geometry/Bounds');
for (i = 0; i < pairs.length; i++) {
pair = pairs[i];

if (!pair.isActive || pair.separation < 0)
if (!pair.isActive || pair.isSensor || pair.separation < 0)
continue;

collision = pair.collision;
bodyA = collision.parentA;
bodyB = collision.parentB;
normal = collision.normal;
positionImpulse = (pair.separation - pair.slop) * timeScale;

if (bodyA.isStatic || bodyB.isStatic)
positionImpulse *= 2;

Expand Down Expand Up @@ -180,7 +180,7 @@ var Bounds = require('../geometry/Bounds');
for (i = 0; i < pairs.length; i++) {
pair = pairs[i];

if (!pair.isActive)
if (!pair.isActive || pair.isSensor)
continue;

contacts = pair.activeContacts;
Expand All @@ -189,7 +189,7 @@ var Bounds = require('../geometry/Bounds');
bodyB = collision.parentB;
normal = collision.normal;
tangent = collision.tangent;

// resolve each contact
for (j = 0; j < contacts.length; j++) {
contact = contacts[j];
Expand Down Expand Up @@ -239,7 +239,7 @@ var Bounds = require('../geometry/Bounds');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];

if (!pair.isActive)
if (!pair.isActive || pair.isSensor)
continue;

var collision = pair.collision,
Expand Down

0 comments on commit e1a52d5

Please sign in to comment.