dimforge / rapier.js

Official JavaScript bindings for the Rapier physics engine.
https://rapier.rs
Apache License 2.0
418 stars 57 forks source link

why `wolrd.contactsWith` callback never gets called? #241

Open TheMetaverseEngineer opened 1 year ago

TheMetaverseEngineer commented 1 year ago
const bodyDesc = RigidBodyDesc.dynamic()
      .setTranslation(startPos.x, startPos.y)
      .setLinearDamping(0)
      .setLinvel(-this.speed, 0)
    this.rb = Physics.instance.world.createRigidBody(bodyDesc);

    const colliderDesc = ColliderDesc.ball(this.size);
    const collider = Physics.instance.world.createCollider(
      colliderDesc,
      this.rb
    );
    Physics.instance.world.contactsWith(collider, (collider2) => {
      console.log(
        "🚀 ~ file: Bullet.physics.ts:58 ~ BulletPhysics ~ Physics.instance.world.contactsWith ~ collider2:",
        collider2
      );
    });

i don't know why but my callback never gets called. I couldn't find any examples!

wezzzyang commented 11 months ago

Have you found the reason, is not working too

Tresky commented 5 months ago

Popping in just to say that this happens for me too. Also happens for intersectsWith. Neither seem to actually get called.

I'm pretty confident that the masks are set up properly because I can see the event coming through drainCollisionEvents.

sebcrozet commented 5 months ago

These methods are not registering an event handler that will get called when a contact/intersection is detected. The callback are called only if there is any contact at the moment you call contactsWith. So, whenever you need to check these contacts (typically once per frame, after the physics step), you need to call .contactsWith again.

Tresky commented 5 months ago

OH! Well that's my problem. I must've misunderstood that in the docs. Thanks Seb!