rapier.js
rapier.js copied to clipboard
Event handlers are not called
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.
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);
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?
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
Thanks for your support, @LeXXik. I'll try with collision groups.
@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', () => {})
@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.
@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
.
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 👍