bartzy
bartzy

Reputation: 328

Allowing specific bodies to collide with edges but not with other fixed bodies

I'm using the edge-collision-detection behavior to add collisions with the edge of the viewport, and the body-impulse-response behavior to respond to these collisions so the colliding bodies will bounce off the "walls".

However, I can't seem to make body-impulse-response only work for the given bodies and the edges (body-impulse-response has no applyTo method apparently?), so now bodies are colliding with other fixed bodies even though I don't want them too.

How can I fix this issue?

Thanks!

Upvotes: 0

Views: 211

Answers (1)

bartzy
bartzy

Reputation: 328

I found a possible solution:

The body-impulse-response behavior works on all collisions in a given channel. Instead of trying to restrict that behavior to specific bodies by using applyTo(bodies), you can change the channel that the collisions are being reported on, and give that channel as the input for the body-impulse-response behavior.

(incomplete) Example:

Phyiscs(function(world) {
    var viewportBounds = Physics.aabb(0, 0, viewportWidth, viewportHeight);

    world.add(Physics.behavior('edge-collision-detection', {
        channel: 'collisions-edge:detected',
        aabb: viewportBounds,
        restitution: 0.7,
        cof: 1
    }));

    world.add(Physics.behavior('body-impulse-response', {
                check: 'collisions-edge:detected'
    }));

    /* You can now add a body-collision-detection behavior with the default 
     * collisions:detected (or a custom name) channel, and the
     * body-impulse-response added above won't respond to these collisions.
     */
});

If you have a different/better solution - please share it!

Upvotes: 0

Related Questions