Jump to content

onBeginContact - how to get both bodies ?


valueerror
 Share

Recommended Posts

the docs say:

Dispatched when a first contact is created between shapes in two bodies. This event is fired during the step, so collision has already taken place. The event will be sent 4 parameters: The body it is in contact with, the shape from this body that caused the contact, the shape from the contact body and the contact equation data array.

 

why the hell do i need the shapes? can't i acces the shape of every body when i have the body?  but hey..  the actual question is:

 

how do i get both colliding bodies?

 fireball.body.onBeginContact.add(fireballCollision, this);
function fireballCollision(target){    if (target.sprite && target.sprite.name == 'redtoad'){        killEnemy(target);       //here i also want to kill the fireball so it can't harm anyone else ;-)        }    }
Link to comment
Share on other sites

Bodies can have many shapes within them - how would you know which of those shapes created the contact if we didn't give it to you?

 

Anyway there are 2 events, one raised by P2.World which sends:

callback(bodyA, bodyB, shapeA, shapeB, contactEquations);

And one raised from the bodies themselves, which send:

callback(bodyB. shapeA, shapeB, contactEquations)
Link to comment
Share on other sites

 

Bodies can have many shapes within them - how would you know which of those shapes created the contact if we didn't give it to you?

 

Good point !!

 

 

im sorry but i just cant figure out how to use you tip ..

 

when are those two raised?

how do i get the world to raise the one that sends both bodies..  ??  

 

thank you!

 

 

i am playing around with this because you once said that impact events are very expensive..  so i thought i'll try onBeginContact signals..  but if the world sends this on EVERY contact event and i have to compare "names" or "sprite keys" every time this signal is sent to determine if a specific action has to be triggered (because bullet hit enemy ) it feels like even more overhead ..    or did i misunderstand you?

Link to comment
Share on other sites

  • 11 months later...

@latestpost:  this is probably the best answer to this.. thx!

 

you know what i did until now?? 

 

here:

fireball.body.onBeginContact.add(fireballCollision, fireball);  //notice that instead of this i gave it the fireball as contextfunction fireballCollision(target){    thefireball = this;   // here i grab the context (the fireball) and put it on "thefireball"    if (target.sprite && target.sprite.name == 'redtoad'){        killEnemy(target);        thefireball.kill();        }    }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...