Box2D modelling flexible hair-like objects
I am looking to model 开发者_开发问答some thin & flexible strands/hairs/spines/lines in Box2D and am wondering what the best way to go about this is. I've Googled and seen something called line segments (http://www.box2d.org/forum/viewtopic.php?f=8&t=1437) but it seems to be just an extension for the Flash port - I was hoping to do it in a platform-agnostic way.
I am guessing I'll have to do it with chains of thin boxes, but does anyone have any advice or things to watch out for?
Update: Thanks to the feedback below the options so far are:
Make springy revolute joints by tracking the angle and setting motor speed - there is a thread on this at the Box2D forum: http://www.box2d.org/forum/viewtopic.php?f=3&t=1007
Attach springy distance joints between the segments (see diagram - similar to how people approach soft bodies).
Drop Box 2D and use verlet/particle system if there's minimal collision necessary (not possible for me unfortunately).
It depends how much a part of the physics world do they need to be, and how accurately you want to collide with all parts of the 'hair'. If collision is not paramount you could approximate with a reduced number of boxes in the chain, and render based on a spline of their locations. If very little collision is involved you could probably even use verlet integration which is pretty easy to code and lighter on CPU.
With just 10 of these though I expect you could simulate them fully with revolute joints although rather than limits I would control them so that they continually try to straighten their chain, but adjust the max torque of each joint so that they don't have enough power to straighten fully. One point to bear in mind if you go this way is that it helps to make the segments near the base heavier, and gradually lighter towards the ends. The motor strength will need to be weaker at the tips too.
Wow... Conceivably you could use many revolute joints with limits set on their movement (rotation), but I would think that your CPU and memory usage would quickly spiral out of control. Someone else may have a better answer but you may wish to look into using a handful of prerendered/preanimated objects. Also you could probably do this with the particle system. It wouldn't be so much rendered in a physics simulation but it could look cool if that is what you are going for.
Probably my best suggestion is to use a combination of these things. If I felt I really had to do this and really had to have it at least somewhat done in the physics simulation, I would combine the use of revolute joints with particles. I would draw a different particle file depending on which way the bodies are moving.
Sorry I can't give you specific examples as I have never had a need to do such a thing.
You guys bring up great points about performance.
Trying to simulate each strand would be very costly. What you could do is simulate a small number of strands spread evenly across a surface. Then use these strands as a reference to draw in-between strands.
Throwing in a bit of randomness could give a nice effect.
I wrote an article about simulating a tree trunk that is related and might be useful to some folks out there: http://aaronfarr.com/coding/simulating-a-tree-in-box2dweb/
Update: I have put together a demo of a grass simulation using this idea: http://aaronfarr.com/coding/simulating-grass-using-box2d/
精彩评论