schteppe / p2.js

JavaScript 2D physics library
Other
2.65k stars 329 forks source link

Control Bodies #61

Open englercj opened 10 years ago

englercj commented 10 years ago

I noticed #28 which seems to imply that CCD (or swept collisions) is not yet implemented. Currently I am using Chipmunk-js and for seting velocity of bodies without causing teleportation, tunneling, or other weird stuff I need to use control bodies joined by a specific pivot joint.

Will CCD be implemented soon, or will I have to use the same methodology here?

Sorry for the questions I've been posting lately! I am evaluating p2.js for my game engine, and being able to directly control bodies will reduce complexity significantly.

schteppe commented 10 years ago

That's correct about CCD, not available yet. I have an idea on how to implement it, but not not enough time on my hands right now.

I see why you need CCD when setting the velocity of a dynamic body - this will prevent tunneling, for example. But how can another body + a joint help you there? Is the control body dynamic or kinematic? Do you set the velocity of it, or position? Does it have collisions enabled, or is it a ghost helper body? My guess for now is that this "control joint" makes the user input more soft and that is easier to handle for the collision detection. Correct?

In any case, CCD would make things a lot easier for you. Since I can't tell when that will be available in p2.js, I'd suggest you to go with some other solution for now.

englercj commented 10 years ago

Is the control body dynamic or kinematic?

Kinematic

Do you set the velocity of it, or position?

velocity

Does it have collisions enabled, or is it a ghost helper body?

It is simulated, but has no collision shapes so it doesn't collide.

The idea behind this technique is that if you set the velocity of a body directly you get tunneling, and also for topdown games you can make the player much "stronger" that they should be if you are setting the constant velocity. Using the joint makes the player have the correct "strength" when moving around.

This is only used in 0 gravity topdown environments. With gravity environments (eg. a side-scroller), the control body would not let the player be effected by gravity. In that case you use surface velocity techniques to move the player.

In any case CCD is always easier for the user :D

You can read more in the Chipmunk Topdown Tutorial:

http://chipmunk-physics.net/tutorials/ChipmunkTileDemo/

And see some code in the Tank Demo:

https://github.com/slembcke/Chipmunk2D/blob/master/Demo/Tank.c

schteppe commented 10 years ago

All right, then I'm with you :) Btw, surface velocity would also be nice. I'll create an issue for that.

schteppe commented 8 years ago

How about this? https://github.com/schteppe/p2.js/commit/74a31518030d4ca4a7bb5dc8b8c2359610505bca

https://twitter.com/schteppe/status/703643753798623232