Interact with each other without rigidbody

Hi, I have 2 different objects and these two objects do not have solid objects. One of the objects is in motion, but not dynamically, but statically. How do I know if these 2 different objects collide?

Hi @Onur_Ozturk,

Maybe use bounding boxes? This example does something similar to detect picking without using physics:

https://developer.playcanvas.com/en/tutorials/entity-picking-without-physics/

Bounding boxes have a method that can detect intersections (when two bodies collide), which will give you what you are looking for.

https://developer.playcanvas.com/api/pc.BoundingBox.html#intersects

4 Likes

Hi @Leonidas , thank you for your answer but i am undecided on how to integrate this example into my own project. If you have time, can you post a small sample project that prints a message on the console screen as soon as the 2 boxes touch each other? I am grateful to you :pray:

Here is a simple example, move the boxes in editor so they intersect:

var ShapePicker = pc.createScript('shapePicker');

// initialize code called once per entity
ShapePicker.prototype.initialize = function() {

    // Keep an array of all the entities we can pick at
    this.pickableEntities = [];
    
    // Register events for when pickable entities are created
    this.app.on("shapepicker:add", this.addItem, this);
};

ShapePicker.prototype.addItem = function (entity, shape) {
    if (entity) {
        this.pickableEntities.push({entity, shape});
    }
};

ShapePicker.prototype.update = function (dt) {

    this.pickableEntities.forEach(obj => {

        const results = this.detectIntersections(obj);

        if(results.length > 0){
            console.log('Intersects: ', results.map(o => o.name));
        }
    });
};

ShapePicker.prototype.detectIntersections = function(referenceObj){

    const results = [];

    this.pickableEntities.forEach(obj => {

        if(referenceObj !== obj){

            const result = referenceObj.shape.intersects(obj.shape);

            if(result) results.push(obj.entity);
        }
    });

    return results;
};

https://playcanvas.com/project/894183/overview/entity-intersections

3 Likes

Thank you so much :pray:

1 Like