mirror of
https://github.com/liabru/matter-js.git
synced 2025-01-21 17:14:38 -05:00
Merged sensors from Misiur-master
This commit is contained in:
commit
e1a52d59aa
6 changed files with 89 additions and 7 deletions
|
@ -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#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#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#stress2">Stress 2</a></li>
|
||||||
|
<li><a href="http://brm.io/matter-js/demo#sensors">Sensors</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<br>
|
<br>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -63,6 +63,7 @@
|
||||||
<option value="cloth">Cloth</option>
|
<option value="cloth">Cloth</option>
|
||||||
<option value="events">Events</option>
|
<option value="events">Events</option>
|
||||||
<option value="collisionFiltering">Collision Filtering</option>
|
<option value="collisionFiltering">Collision Filtering</option>
|
||||||
|
<option value="sensors">Sensors</option>
|
||||||
<option value="chains">Chains</option>
|
<option value="chains">Chains</option>
|
||||||
<option value="ballPool">Ball Pool</option>
|
<option value="ballPool">Ball Pool</option>
|
||||||
<option value="stack">Stack</option>
|
<option value="stack">Stack</option>
|
||||||
|
|
70
examples/sensors.js
Normal file
70
examples/sensors.js
Normal 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;
|
||||||
|
};
|
||||||
|
})();
|
|
@ -53,6 +53,7 @@ var Axes = require('../geometry/Axes');
|
||||||
angularSpeed: 0,
|
angularSpeed: 0,
|
||||||
velocity: { x: 0, y: 0 },
|
velocity: { x: 0, y: 0 },
|
||||||
angularVelocity: 0,
|
angularVelocity: 0,
|
||||||
|
isSensor: false,
|
||||||
isStatic: false,
|
isStatic: false,
|
||||||
isSleeping: false,
|
isSleeping: false,
|
||||||
motion: 0,
|
motion: 0,
|
||||||
|
@ -788,6 +789,14 @@ var Axes = require('../geometry/Axes');
|
||||||
* @default false
|
* @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.
|
* 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.
|
* If you need to set a body as sleeping, you should use `Sleeping.set` as this requires more than just setting this flag.
|
||||||
|
|
|
@ -33,6 +33,7 @@ var Contact = require('./Contact');
|
||||||
activeContacts: [],
|
activeContacts: [],
|
||||||
separation: 0,
|
separation: 0,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
|
isSensor: bodyA.isSensor || bodyB.isSensor,
|
||||||
timeCreated: timestamp,
|
timeCreated: timestamp,
|
||||||
timeUpdated: timestamp,
|
timeUpdated: timestamp,
|
||||||
inverseMass: parentA.inverseMass + parentB.inverseMass,
|
inverseMass: parentA.inverseMass + parentB.inverseMass,
|
||||||
|
|
|
@ -70,7 +70,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
for (i = 0; i < pairs.length; i++) {
|
for (i = 0; i < pairs.length; i++) {
|
||||||
pair = pairs[i];
|
pair = pairs[i];
|
||||||
|
|
||||||
if (!pair.isActive)
|
if (!pair.isActive || pair.isSensor)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
collision = pair.collision;
|
collision = pair.collision;
|
||||||
|
@ -89,7 +89,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
for (i = 0; i < pairs.length; i++) {
|
for (i = 0; i < pairs.length; i++) {
|
||||||
pair = pairs[i];
|
pair = pairs[i];
|
||||||
|
|
||||||
if (!pair.isActive || pair.separation < 0)
|
if (!pair.isActive || pair.isSensor || pair.separation < 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
collision = pair.collision;
|
collision = pair.collision;
|
||||||
|
@ -180,7 +180,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
for (i = 0; i < pairs.length; i++) {
|
for (i = 0; i < pairs.length; i++) {
|
||||||
pair = pairs[i];
|
pair = pairs[i];
|
||||||
|
|
||||||
if (!pair.isActive)
|
if (!pair.isActive || pair.isSensor)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
contacts = pair.activeContacts;
|
contacts = pair.activeContacts;
|
||||||
|
@ -239,7 +239,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
for (var i = 0; i < pairs.length; i++) {
|
for (var i = 0; i < pairs.length; i++) {
|
||||||
var pair = pairs[i];
|
var pair = pairs[i];
|
||||||
|
|
||||||
if (!pair.isActive)
|
if (!pair.isActive || pair.isSensor)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var collision = pair.collision,
|
var collision = pair.collision,
|
||||||
|
|
Loading…
Add table
Reference in a new issue