planck.js
planck.js copied to clipboard
Collision with sensors
Hi, i am trying to make collision with two sensors. When two sensors collision, i will print console "on".
I did something. But it says "on" in every collision with any item. I just want with 2 sensors.
world.on("pre-solve", function(contact, oldManifold) {
var manifold = contact.getManifold();
var fixtureA = contact.getFixtureA();
var fixtureB = contact.getFixtureB();
var sensor = fixtureA.getBody();
var infectedsensor = fixtureB.getBody();
var sensorData = sensor.getUserData();
var infectedsensorData = infectedsensor.getUserData();
if (sensor && sensorData) {
console.log("on");
}
else {
console.log("off");
}
});
My sensors :
var a = world.createBody({
type: "dynamic",
angularDamping: 3.0,
linearDamping: 4.0,
position: Vec2(0.0, 10.0),
angle: Math.PI,
allowSleep: false
});
var box = pl.Box(0.5, 0.5);
var boxfix = a.createFixture(box, 2.0);
sensor = a.createFixture({
shape: pl.Circle(Vec2(0.0, 0.0), 2.0),
isSensor: true
});
sensor1 = a.createFixture({
shape: pl.Circle(Vec2(0.0, 0.0), 5.0),
isSensor: true
});
So far I've gotten around it by using a non-sensor body and in the 'pre-contact' event handler, doing
contact.setEnabled(false)
This makes the body not cause physical collisions so it acts similar to a sensor. If you find something else out I'd love to hear it though.
Contacts between sensors are not resolved, API doc for pre-solve
already says that "Note: this is not called for sensors.". However, begin-contact
is called for sensors, so you could use it.
Also in the code provided above both fixtures are added to the same body, and fixtures in the same body do not collide.
Here is the working code for listening to contacts between two sensor: https://piqnt.com/space/v0Tza2NVz