Skip to content

Commit e1a52d5

Browse files
committed
Merged sensors from Misiur-master
2 parents 1944f6a + 28084b0 commit e1a52d5

6 files changed

Lines changed: 89 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<li><a href="http://brm.io/matter-js/demo#beachBalls">Beach Balls</a></li>
5959
<li><a href="http://brm.io/matter-js/demo#stress">Stress 1</a></li>
6060
<li><a href="http://brm.io/matter-js/demo#stress2">Stress 2</a></li>
61+
<li><a href="http://brm.io/matter-js/demo#sensors">Sensors</a></li>
6162
</ul>
6263
<br>
6364
</td>

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ <h1>Matter.js Demo</h1>
6363
<option value="cloth">Cloth</option>
6464
<option value="events">Events</option>
6565
<option value="collisionFiltering">Collision Filtering</option>
66+
<option value="sensors">Sensors</option>
6667
<option value="chains">Chains</option>
6768
<option value="ballPool">Ball Pool</option>
6869
<option value="stack">Stack</option>

examples/sensors.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(function() {
2+
3+
var World = Matter.World,
4+
Bodies = Matter.Bodies,
5+
Common = Matter.Common,
6+
Events = Matter.Events;
7+
8+
Example.sensors = function(demo) {
9+
var engine = demo.engine,
10+
world = engine.world,
11+
sceneEvents = demo.sceneEvents;
12+
13+
var redColor = '#C44D58',
14+
greenColor = '#C7F464';
15+
16+
var collider = Bodies.rectangle(400, 300, 500, 50, {
17+
isSensor: true,
18+
isStatic: true,
19+
render: {
20+
strokeStyle: redColor,
21+
fillStyle: 'transparent'
22+
}
23+
});
24+
25+
World.add(world, collider);
26+
27+
World.add(world,
28+
Bodies.circle(400, 40, 30, {
29+
render: {
30+
strokeStyle: greenColor,
31+
fillStyle: 'transparent'
32+
}
33+
})
34+
);
35+
36+
sceneEvents.push(
37+
Events.on(engine, 'collisionStart', function(event) {
38+
var pairs = event.pairs;
39+
40+
for (var i = 0, j = pairs.length; i != j; ++i) {
41+
var pair = pairs[i];
42+
43+
if (pair.bodyA === collider) {
44+
pair.bodyB.render.strokeStyle = redColor;
45+
} else if (pair.bodyB === collider) {
46+
pair.bodyA.render.strokeStyle = redColor;
47+
}
48+
}
49+
}),
50+
Events.on(engine, 'collisionEnd', function(event) {
51+
var pairs = event.pairs;
52+
53+
for (var i = 0, j = pairs.length; i != j; ++i) {
54+
var pair = pairs[i];
55+
56+
if (pair.bodyA === collider) {
57+
pair.bodyB.render.strokeStyle = greenColor;
58+
} else if (pair.bodyB === collider) {
59+
pair.bodyA.render.strokeStyle = greenColor;
60+
}
61+
}
62+
})
63+
);
64+
65+
var renderOptions = engine.render.options;
66+
renderOptions.wireframes = false;
67+
renderOptions.background = '#222';
68+
renderOptions.showAngleIndicator = false;
69+
};
70+
})();

src/body/Body.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var Axes = require('../geometry/Axes');
5353
angularSpeed: 0,
5454
velocity: { x: 0, y: 0 },
5555
angularVelocity: 0,
56+
isSensor: false,
5657
isStatic: false,
5758
isSleeping: false,
5859
motion: 0,
@@ -788,6 +789,14 @@ var Axes = require('../geometry/Axes');
788789
* @default false
789790
*/
790791

792+
/**
793+
* A flag that indicates whether a body is a sensor. Sensor triggers collision events, but doesn't react with colliding body physically.
794+
*
795+
* @property isSensor
796+
* @type boolean
797+
* @default false
798+
*/
799+
791800
/**
792801
* 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.
793802
* If you need to set a body as sleeping, you should use `Sleeping.set` as this requires more than just setting this flag.

src/collision/Pair.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var Contact = require('./Contact');
3333
activeContacts: [],
3434
separation: 0,
3535
isActive: true,
36+
isSensor: bodyA.isSensor || bodyB.isSensor,
3637
timeCreated: timestamp,
3738
timeUpdated: timestamp,
3839
inverseMass: parentA.inverseMass + parentB.inverseMass,

src/collision/Resolver.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ var Bounds = require('../geometry/Bounds');
7070
for (i = 0; i < pairs.length; i++) {
7171
pair = pairs[i];
7272

73-
if (!pair.isActive)
73+
if (!pair.isActive || pair.isSensor)
7474
continue;
75-
75+
7676
collision = pair.collision;
7777
bodyA = collision.parentA;
7878
bodyB = collision.parentB;
@@ -89,15 +89,15 @@ var Bounds = require('../geometry/Bounds');
8989
for (i = 0; i < pairs.length; i++) {
9090
pair = pairs[i];
9191

92-
if (!pair.isActive || pair.separation < 0)
92+
if (!pair.isActive || pair.isSensor || pair.separation < 0)
9393
continue;
9494

9595
collision = pair.collision;
9696
bodyA = collision.parentA;
9797
bodyB = collision.parentB;
9898
normal = collision.normal;
9999
positionImpulse = (pair.separation - pair.slop) * timeScale;
100-
100+
101101
if (bodyA.isStatic || bodyB.isStatic)
102102
positionImpulse *= 2;
103103

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

183-
if (!pair.isActive)
183+
if (!pair.isActive || pair.isSensor)
184184
continue;
185185

186186
contacts = pair.activeContacts;
@@ -189,7 +189,7 @@ var Bounds = require('../geometry/Bounds');
189189
bodyB = collision.parentB;
190190
normal = collision.normal;
191191
tangent = collision.tangent;
192-
192+
193193
// resolve each contact
194194
for (j = 0; j < contacts.length; j++) {
195195
contact = contacts[j];
@@ -239,7 +239,7 @@ var Bounds = require('../geometry/Bounds');
239239
for (var i = 0; i < pairs.length; i++) {
240240
var pair = pairs[i];
241241

242-
if (!pair.isActive)
242+
if (!pair.isActive || pair.isSensor)
243243
continue;
244244

245245
var collision = pair.collision,

0 commit comments

Comments
 (0)