planck.js icon indicating copy to clipboard operation
planck.js copied to clipboard

Collision with sensors

Open ekofi opened this issue 4 years ago • 1 comments

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

ekofi avatar Mar 18 '20 13:03 ekofi

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.

bushmango avatar Apr 19 '21 23:04 bushmango

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

shakiba avatar Aug 25 '23 00:08 shakiba