p2.js icon indicating copy to clipboard operation
p2.js copied to clipboard

Combining my own physics and using p2 for collision detection/response

Open spacejack opened this issue 10 years ago • 3 comments

Hi schteppe,

I'd like to get p2 to handle collision response between a moving rectangle and static shapes (capsules.) The rectangle is moving around using my own custom physics.

I'm having some trouble combining the two and wondered if you had any ideas on how to do this best.

I tried writing my update loop like this:

  1. Apply object's current position, velocity, angle and angularVelocity to a p2.Body (Rectangle shape)
  2. Do p2.World.step(dt)
  3. beginContact/endContact events will turn on/off a 'contact' boolean
  4. If contacting, apply the resulting p2.Body state to my object. Otherwise, don't.
  5. Do my own physics update on rectangle

This caused some weird 'sticky' issues during contact. I thought maybe this was because my update was causing the object to penetrate the capsules. So I used my own collision detection to remove the object from overlapping... however this is still causing some weird sticky problems.

I'm wondering if there's a better way to do this. Normally I would simply apply forces to the p2.Body, however since it is a top-down vehicle with its own driving/skid physics, this isn't so easy.

spacejack avatar Jul 01 '15 16:07 spacejack

The first thing that comes to my mind is, why not use the built in p2 top down vehicle? I guess it's less complex than your own physics but it would make the simulation loop drastically simpler. See https://vine.co/v/em1aAuelJ0l and source at https://github.com/schteppe/p2.js/blob/master/src/objects/TopDownVehicle.js

Feels like your algorithm looks like it should work though... Might be a long shot but, try setting the .contactSkinSize of your ContactMaterial to zero. If you're using the default one, set world.defaultContactMaterial.contactSkinSize=0;.

Are you sure that you get a single contact event? Just checking.

What does your custom car physics do? Add friction in the sideways direction? If you replace it with simple extrapolation (x+=v*dt), does it work better?

Do you have some video of this happening, or code?

schteppe avatar Jul 01 '15 18:07 schteppe

Ahh now I feel silly! I didn't know p2 had a vehicle. Although I didn't see it in the docs and I worked on the vehicle before I found p2, so that's my excuse. Mine's very simple, I'll try this out and see how it compares.

I get multiple begin/end contact events and they look correct to me. I use a counter, when numContacts > 0 then contact is happening.

spacejack avatar Jul 01 '15 19:07 spacejack

Well, the vehicle is new, no wonder you didn't notice it. It's not ben tested much but it should work. Let me know how it goes :)

schteppe avatar Jul 02 '15 06:07 schteppe