schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

Dynamic bodies don't respond properly when static body positions are moved #231

Closed AndrewRayCode closed 5 years ago

AndrewRayCode commented 8 years ago

I'm working on trigger events in my game, as in you go over an area and it triggers another event. In the below screencap, you can see I trigger a moveable wall that's under a pushy block.

jiggly

However, the pushy block does not react until the player (a dynamic body) touches it. The wall is static and the block is dynamic and I update the wall's position each frame to move it. By static I mean it has mass 0. Is there a way to achieve what I want? Right now I only know the wall's data, if possible I'd prefer not to have to store the entity's data itself to wake it up. I'm looking for a generic solution that will make the block fall correctly as the wall slides.

AndrewRayCode commented 8 years ago

I tried setting allowSleep: false, on the pushy body, but the behavior is the same. Also, does setting allowSleep: false have a significant performance impact if applied to ~10 objects in a scene?

AndrewRayCode commented 8 years ago

Also, interestingly, after the wall moves the block will slowly slide down until it passes where the wall was, then it moves normally.

slow

schteppe commented 8 years ago

Hi. Did you try setting the body to kinematic? http://schteppe.github.io/p2.js/docs/classes/Body.html#property_type Static bodies are assumed to have infinite mass and never move, while kinematic have infinite mass but are assumed to move. Some stuff may get cached for your walls that really shouldn't.

If this is not the problem then maybe the dynamic box gets stuck on the edge of the moving one? Try making the moving block shape slightly smaller than a full block?

10 awake bodies with integration, collision and contact solving is more than none. So yeah, try make as many as possible sleep if you can, but only if you feel like that optimization is needed. PS. sometimes you may have to .wakeUp() bodies manually.