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

Event handlers are not called

Open deniszatsepin opened this issue 2 years ago • 8 comments

Hi guys, first of all, thanks for the great library you are working on. I'm using 0.8.0-alpha.0 version of rapier.js. I have a ground as a static body and two dynamic bodies. I've added colliders of these bodies to one collision group and set active contact events on them with the code below:

collider.setCollisionGroups(0x00010001)
collider.setActiveEvents(
  RAPIER.ActiveEvents.CONTACT_EVENTS
)

I've created an event queue and added one dummy contact event handler:

const eventQueue = new RAPIER.EventQueue(true)

eventQueue.drainContactEvents((handle1, handle2, started) => {
  /* Handle the contact event. */
  console.log(handle1, handle2, started)
})

in a render loop I pass eventQueue as a parameter to the step method:

world.step(eventQueue)

One of the dynamic bodies is a character controlled by a keyboard/mouse. The problem is, that regardless of the way I collide these bodies the contact events handler with console.log is not called.

Do I do something wrong, or is it something missing in the alpha version of the library? Thanks.

deniszatsepin avatar Jan 22 '22 10:01 deniszatsepin

It works for me. One difference with your code I see is I also set active collision types on the collider:

collider.setActiveCollisionTypes(COLLISION_TYPE_ALL);

LeXXik avatar Jan 22 '22 12:01 LeXXik

Thanks, @LeXXik for your quick response. I've tried calling .setActiveCollisionTypes(RAPIER.ActiveCollisionTypes.ALL) on all of participating collaiders, but, unfortunately, it didn't help. btw, do you use 0.8.0-alpha.0 version as well?

deniszatsepin avatar Jan 23 '22 18:01 deniszatsepin

Hmm, not sure what's up... if you could make a PlayCanvas sample project, I could jump in to take a look... otherwise not sure what may be the issue. Next best place to look at is the groups/masks: https://rapier.rs/docs/user_guides/javascript/colliders#collision-groups-and-solver-groups

LeXXik avatar Jan 23 '22 23:01 LeXXik

Thanks for your support, @LeXXik. I'll try with collision groups.

deniszatsepin avatar Jan 24 '22 07:01 deniszatsepin

@deniszatsepin Did you ever solve it? I'm facing a similar issue. I have 2 dynamic bodies that do make contact but the callback is never fired.

A bit unrelated but it would be great if there was a higher level API, e.g. rigidBody.addListener('collideStart', () => {})

alexandernanberg avatar Mar 05 '22 17:03 alexandernanberg

@deniszatsepin Did you ever solve it? I'm facing a similar issue. I have 2 dynamic bodies that do make contact but the callback is never fired.

A bit unrelated but it would be great if there was a higher level API, e.g. rigidBody.addListener('collideStart', () => {})

imo this way won't work efficiently. try imagine there is hundred of rigidbodies.

contact report for a world is better and actually you can write your own emit to dispatch events in the contact callback for the individual bodies.

jcyuan avatar Mar 06 '22 06:03 jcyuan

@deniszatsepin The .drainContactEvents does not add an event handler (the eventQueue won’t store your callback). It will call the provided callback on each event contained in eventQueue at the time of calling .drainContactEvents. So you should call .drainContactEvents with your callback after each call to .step.

sebcrozet avatar Mar 06 '22 13:03 sebcrozet

contact report for a world is better and actually you can write your own emit to dispatch events in the contact callback for the individual bodies.

Fair point!

So you should call .drainContactEvents with your callback after each call to .step

Nice, that + adding colliderDesc.setActiveEvents(RAPIER.ActiveEvents.CONTACT_EVENTS) fixed the issue for me 👍

alexandernanberg avatar Mar 06 '22 14:03 alexandernanberg