Detecting if there is a contact between two colliders

Hi guys!

Is there a way to check if two entities (ofc they have collision components) are colliding/touching? Ideally, something like

if ( entityA.touches(entityB) ) { ... }

Or maybe there is a way to get list of all contacts of some entity? Like

const contactingEntities = entity.getContactsList();

Thanks!

1 Like

Hey @Igor, an approach I’ve used to detect collision:

var Collider = pc.createScript('collider');
//SUMMARY
//this script is used to detect a collision between entity (that has attached script) and any other object that has a rigid body
//only triggers when a new object starts a collision
//when collision event occurs, message printed to console and entity changes color (as long as it has a material attached)

// initialize code called once per entity
Collider.prototype.initialize = function () {
    //collision logic gets triggered whenever a new collision event is detected (afer incoming object left this entity's collision area)
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

// collision logic
Collider.prototype.onCollisionStart = function (result) {
    if (result.other.rigidbody) {
        console.log('collision success');
        this.entity.model.material.diffuse.set(Math.random(), Math.random(), Math.random());
        this.entity.model.material.update();
    }
};

*I’ve used this logic in my test game (ie. “Collider.js” script)
*Note: Both entities would need to have “rigid body” and “collision” components

Thank you for the answer, @Vdizzle! But it’s a bit different, collisionstart event appears only once when collision begins. But I would like to know how to get list of entity’s collisions at any moment. Is it possible in PlayCanvas at all or I need to dig into Ammo.js docs?

2 Likes