Sinova/Collisions

Encapsulating body within a higher level object

Closed this issue · 2 comments

I'm playing with the Tank demo and I would like to encapsulate a collision body within different objects, such as: target and bullet. This way when the bullet collides with a target, I can check what hit target (if the bullet, the tank or other) and react accordingly.

Unfortunately I can't seem to find the correct approach. When I iterate the body.potentials(); I can't backtrack to which object contained it and I can't seem to find a unique id that is assigned to a given body that I could use as reference.

Can you please advise on the correct approach?

You can set additional properties to the collision object.

So for example:

let bullet = system.createCircle(x,y,r)
let bullet.objectType = 'bullet'

Then when you are testing if your tank collides you can do something like

const potentials = this.tankCollision.potentials();
        for (const object of potentials) {
            if (this.playerCollision.collides(object, result)) {
                switch (object.objectType) {
                    case 'bullet':
                            this.takeBulletDamage(object.spellID);
                        break;
                      default:
                            this.x = this.playerCollision.x -= result.overlap * result.overlap_x;
                             this.y = this.playerCollision.y -= result.overlap * result.overlap_y;
                        break;
                 }})
}

Hope that helps.

Apologies for the late reply, this is very helpful thank you very much