0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -05:00

Merged sensors from Misiur-master

This commit is contained in:
liabru 2016-04-05 23:54:40 +01:00
commit e1a52d59aa
6 changed files with 89 additions and 7 deletions

View file

@ -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>

View file

@ -63,6 +63,7 @@
<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>

70
examples/sensors.js Normal file
View file

@ -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;
};
})();

View file

@ -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,
@ -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.

View file

@ -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,

View file

@ -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;
@ -89,7 +89,7 @@ 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;
@ -97,7 +97,7 @@ var Bounds = require('../geometry/Bounds');
bodyB = collision.parentB;
normal = collision.normal;
positionImpulse = (pair.separation - pair.slop) * timeScale;
if (bodyA.isStatic || bodyB.isStatic)
positionImpulse *= 2;
@ -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;
@ -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];
@ -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,