[SOLVED] How to detect if entities are touching (any given time)

I am trying to figure out how to / if it is possible to know if two entities are touching at ANY given time. I am using this because I have a 2D game with a melee weapon that needs to send players flying whenever they are touching the item

PHYSICS: p2.js

Does anyone know if I find when two entities are touching at any time? (I am able to get the entities, I just need to know if they are touching) and I am using p2.js, (NOT AMMO.js)


using the “beginContact” event was correct, and by listening for this event I was able to know if two entities began colliding. The second part was another event listener.
“endContact” was the missing event listener which was used for closing the collision (e.g. making the “collidingPlayers” set to false)

Hi @Chhes,

It’s being a while since I’ve touched p2.js, though the concept for what you ask would be similar with ammo.js as well.

For p2.js check this tutorial here on how to get an event fire at any instant when a contact happens in the physics world:

What you can do to know at any instant if an entity is touch another is to save that contact event on a property and reset it in your script post update method. So when a contact event occurs, have a property get updated:

// code not tested!

    // example logic to check if this a contact that interest me
    if( evt.bodyA.entity.tags.has('column-point') ){
       this.entitiesTouching = true;

}, this);

In your script post update method set that property to false:

MyScript.prototype.postUpdate = function(){
   this.entitiesTouching = false;

Now at any point in your gameplay code you know if those entities are touching by accessing that property.

1 Like

Thanks, I actually do understand this Leonidas!

I just have a question about the Post Update method,

Where would this go?
What does this do?


The PlayCanvas application with each frame rendered it will find all entities that hold script instances and implement an update method and call them in order.

After that process finishes it will do the same with entities that implement the postUpdate method. It’s a safe mechanism to execute code that at the end of the normal update cycle.

So in this case I told you to clear it in the postUpdate method to be sure that any entity that accesses the property in the update method will get the value before it’s cleared.

You can declare a postUpdate method in your script in the same way you declare the update method. Check the manual page for more info:


Ah, I see

But wouldn’t this mean if the entities are touching still then the value would be false? (there is a high chance this situation would happen in my game)


So, the p2World sim will step (and report any contacts) in a normal script update method:

One good advice is to add this script to the top of your scene hierarchy so it updates first. Then your scripts updating later on will have the freshly updated property available.

Oh, so are you saying I should put that code into the top of the hierarchy? or the code from before at the top?

If you mean the code you just posted that would make less sense, so currently I am assuming you mean the code from before at the top of the hierarchy, please correct me if I am wrong.

MY IDEA of what you have said:

  1. create the event listener (set the touching to true when colliding)
  2. set the (entity touching) to false in the postUpdate
  3. thats it, I can use the value as I please
    *Please correct me for any of the above things, I want to get this as right as possible so I do not bother you again :smiley:


I mean put the p2World.js script at the top of your hierarchy, so the physics world is updated before your gameplay scripts run.

Yes, I think you got it right! Curious how it works, let us know :wink:

oh yeah, this game is gonna work (tested it for a very long time in a demo version) and it was huge (enough) in numbers.

Ill be sure to let you know, Thanks for your help Leonidas


1 Like


do I need a p2Body WITH a p2Circle when making a collision (this question is pertaining to the one above about collision, so I want to know if I only need a p2Circle to make this collision happen)


I vaguely remember that yes you need both, a physics body and a shape that defines the volume.

Check the p2 examples to be certain:


Hey again Leonidas,
sadly the idea you had for the entities touching did not work as it does not keep the value after postUpdate, which was what I had suspected would be the issue with before.

Do you happen to know any other way this could work because this is crucial to the entire gameplay of my game, and would mean a long time of either working on a new physics engine or writing my own logic.

Any other events may also be helpful.


If you clear the value in the post update then it’s expected to be like that. I think that was the goal right, to know if there was a contact in the current frame.

You may have to increase the number of steps the physics simulation does to match your gameplay code. Or even better add a callback in the p2 world script that executes after each step and use that to clear the value.

Not sure if there is another way to handle this, apart from changing your gameplay code to be event based, so you don’t need this info per frame.