box2d & contours

I am a bit late to this party, but I’ve started playing with ofxBox2D & ofxDelaunay for some physics fun

I’ve not used any examples here as have my own framework for linking modules of code together, plus wanted to learn for myself. Apart from Theo’s isPointInside function.

  1. resample contour
  2. run through delaunay to get triangles
  3. create triangle list of only those with centers inside contour
  4. add to box2d

I am trying to see how the box2d part could be improved.

Firstly every frame I am doing:

  1. remove polys from the world
  2. create & validate new polys to the world
  3. update box2d world

If you are fairly slow moving it works great and stuff bounces off you, however if you move fast you end up creating polys over the position of other objects, so they get stuck inside you. You can remove them, but not great really. Any ideas how to overcome this?

Also there isn’t much in the way of a push, because they are new triangles each frame, like when you move fast you’d hit and object and that object would have a force applied to it.

In terms of speed, could the overhead be improved by not wiping and creating new polys every frame, but by having a large fixed num array of polys and changing their properties?

Also I guess by default each triangle must be checking for collisions with other triangles, so turning this off would probably speed it up?

Any of you box2d folks out there would be great to hear your experiences

I have been going down a similar route with the Kinect but I have yet to add the Box2d part in (I have that functionality in a different project). I am not doing any of #3 as I am using the Kinect threshold to isolate what I triangulate.

I can tell you that one way I was able to speed up things is to thread ofxCvContourFinder/ contour simplification/ofxDelauny creation. I figured I would then merge the Box2d stuff into the main thread.

Hopefully I can merge these this week with Box2d and I will have a better insight to what is working.

Hope it helps

Hey Chris I have recently update ofxBox2d and put it up on git. It is using box2d 2.2 something. Lots of bug fixes. The polygon contour is much more solid, when you download the addon it is has Triangle++ in the lib. There are methods for ofxBox2dPolygon like:

void simplify(float tolerance);
void triangulate();

this makes it super super easy to make a opencv contour shape. I will be pushing some example soon…

hope that helps.


Thanks Todd, will try it out.

Are you suggesting that creating a polyline is better than internal triangles?

Either way, would be interested to hear peoples thoughts on your contour physics having a pushing force on objects, not just collissions.


Hi all,

Just back to exploring this again.

Todd / anyone else, do you have any thoughts on the body being a force against objects, rather than them just stopping on you?

Hey. Actually im working on a similar project. What i’ve done was implement a contact listener which will get the acceleration of the contact point via OpticalFlow and feed that value into the body, as in a setVelocity or addForce or addImpulse. You can filter out the contacts so that it only updates with point from your shape.

Does it seems valid? Im getting some good results actually, only having some problems with the opticalflow from kinect, it has a lot of noise on the contour so sometimes i get erroneous values for acc.