P2.js | P2Body is not following entity position

Hi all,

I am working on this project where I need collisions.
First I tried ammo.js collisions, which unfortunately only works in a 3D screen and not a 2D screen. (If there is any way to make this work under a 2D screen, please let me know)
I would like to use a 2D screen as it is easier to make the whole application responsive.

Then I looked at P2.js as it was mentioned as a lightweight 2D physics framework. However, I am also having troubles with implementing this. I would like to make the P2Body move with its entity when I change the position of that entity. Would this be possible?

P2World gravity is 0 btw.

Short clip to show the problem:

I saw this post but it does not really state a solution.

Any help/tips are very welcome.

Hi @Ramon!

As far as I know, Ammo should work normally in a 2D screen.

What is not working for you?

Hi @Albertos ,

Thanks for your reply.
When using a 2D screen, there are no collisions registering. Even though the positions of the two entities are at the same location. (Confirmed it by logging the positions)

Then when I move everything to a 3D screen, it works…

@Albertos ,

I made a small project to show you the problem.
Here there are 4 cubes, 2 in a 3D screen and 2 in a 2D screen.
Once the cube touches, they should log it in the console.

This works with the 3D screen cubes, but not with the 2D screen cubes.

Funny thing; if you give the 2D screen cubes the exact same z position it wil actually log…

I can’t really make sense of it to be honest. Would you give it a look?

I wasn’t aware of this behavior.

Maybe @LeXXik can help here.

Hmm, sorry, I have no experience with P2.js. Usually guys don’t use 2d/3d screens for any physics. Just use normal scene with orthographical camera and lock the motion of all objects to 2 axes.

Hi @LeXXik,

Thanks for your reply.
Just to confirm. Beside the point it is not usually done, it is normal behaviour for collisions to not work on 2D screens even though they have a 3D world location?

The setup you are describing is what I’ve had before but I did not find a good way to make the world responsive for all screens. However, now that I am thinking about it, it could probably be done by calculating a world point by using placeholder entities in a 2D screen. Utilizing the screenToWorld function.

It will not work. Even though visually it looks like it should, internally all objects in screen space are handled differently. The scales will be very different, so distances will be hundreds of meters (each pixel becomes a meter). It just won’t work.

Alright, thanks for the confirmation!