C++ Buoyancy

I've ported over my FlashAS3 buoyancy code, which I ported from Erin's C++ code. And I will now have to report it back to Flash again. I've got to stop doing things this way...

Anyway, the attached file really has two seperate parts: firstly, the buoyancy stuff people were looking forward too, and also a b2Controller framework of dubious utility.

For those who haven't seen the Flash version, controllers were my take on how to standardize hooks to Box2D for calling code repeatedly each step. They are rather simple: you create a controller, add it to the world, and add bodies to it, and Box2D will take care of the rest. At the moment, responsibility for creating and destroying controllers is on the user; it does not use the factory pattern.

It's possible to rip out the buoyancy code if you want, just use the updated Shapes folder, and rip the relevant code out of b2BuoyancyController.cpp. But I would rather get some feedback if people think controllers are a useful tool. I hope something like this will spur contributions to Box2D, by making it a bit more "pluggable", while not being abused for non-physics features.
Excellent work! I've been really looking forward to the port. I think controllers are a very useful addition to Box2D and hope others embrace them as well.

I worked on merging my favorite recent user contributions to Box2D -- Thin Line Segments (Edges) and Buoyancy, and updated both to Box2D SVN r177. I attached** the result. Note that dynamic edges have no area, so they won't respond to buoyancy. Not sure what we can/should do about that. Also added a simple combination of the DynamicEdge and Buoyancy test to make sure there were no apparent crashes (EdgesBuoyancy.h).

Whether or not it makes it into the main branch, it certainly will be included on the Python end (which I think you were expecting, seeing me post here) 🙂

** Can't attach more than 256k, so it's here http://pybox2d.googlecode.com/files/controllers-and-edges-r177.zip
Uh, can the line segments be put on dynamic objects now? I must have missed that detail.
It's actually fairly easy for me to update the code to handle line segments (as long as they are well defined closed meshes), I can put that in if needed.
Yes, the line segments can be dynamic:
Quote:
shaktool wrote:
- Edge shapes can be dynamic. However, I don't really recommend it, aside from the novelty of it. They work fine if you turn on bullet mode for them, but they don't handle deep intersections very well, and they will only collide with polygons and circles. Edge shapes will never collide with each other. If you do decide to make dynamic edge shapes, don't forget to attach another shape to the same body, or maybe try messing around with b2MassData, since edge shapes always have zero mass and ignore the density property.
(http://www.box2d.org/forum/viewtopic.php?f=3&t=1577)
Ok, here is a largely untested b2EdgeShape.cpp that should do that trick. Note that a similar technique will also make SetMassFromShapes work on well defined line segments, but I haven't included it here.
The bigger work is doing segment-segment collisions before they are useful in a dynamic situation, but there are real problems with that.
Good stuff. Except it launches the big line segment in the EdgesBuoyancy test out of the world. Hmm... I'll have to look into how it's implemented a bit more later.

Oh, to get it to compile, I had to make a few changes (attached).
Yes, as I said, it only works for meshes where every edge is connected. Otherwise, it gives rather odd results. I suppose it would have been better to assert against that:
b2Assert(m_nextEdge && m_prevEdge);
This was only a quickie. If this body of code is put in the trunk I'll go over it more thoroughly.

(There is one circumstance that is consistent, if not useful: single edges which pass through the body origin will always have 0 submerged area).
So, uh, no one actually cares for controllers then?
BorisTheBrave wrote:
So, uh, no one actually cares for controllers then?

I was initially going to use them. But then I realized that since I would exclusively be using them with sensors, and I already had a per-shape contact list and step function mechanism in place, I pretty much had all of the functionality that I would get from controllers (I can just loop through the contact list for a sensor in its step function and apply the force). So... brilliant idea, but not needed here.
Relax. Build it and they will come... just not overnight. Not everybody is at a point where they need this. The biggest project I'm helping out with now would have used controllers had they existed at the time, though we have workarounds now. But my next personal project will likely employ controllers.

Original thread: https://box2d.org/forum/viewtopic.php?t=1664