Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulating body within a higher level object #22

Closed
RamonBeast opened this issue Mar 29, 2020 · 2 comments
Closed

Encapsulating body within a higher level object #22

RamonBeast opened this issue Mar 29, 2020 · 2 comments

Comments

@RamonBeast
Copy link

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?

@mdealiaga
Copy link

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.

@RamonBeast
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants