开发者

Non-Rigid Body 2D Physics Engines in C++

I'm trying to experiment with 2D physics engines in C++. So far, it seems the most popular开发者_如何学JAVA is Box2D. Unfortunately, Box2D is a rigid body physics engine and that's not really going to help me with what I want to try.

I want to be able to define a shape which has a number of vertices joined by springs, such that when this shape collides with rigid or other non-rigid shapes its shape will be flexible.

Now I've tried to think of ways of doing this in Box2D using only rigid bodies, but there always seems to be flaws:

  1. Use rectangles or line segments for the outer edges of the flexible shape. These will collide with other shapes. Unfortunately, they are not springy, so the desired effect would not be seen.
  2. Each vertex of the shape could be a body with its own small circular shape. These bodies can then be joined together by springs. This would work great in terms of the deformation of the shape, but imagine if the shape landed on a rigid spike and the spike just passed between the vertices. Then the shape would become stuck on the spike.

So what is the best way to do this kind of physics in C++? Preferably without having to write an entire physics engine. Maybe I'm just missing a feature of Box2D. Maybe it's just not the right choice. Then what is the right choice?


There are several packages/engines out there that support deformable/soft bodies. If you want something free you can for example check out Phyz, SOFA or Bullet.

There is a detailed listing on wikipedia. Most of these are 3D-based but you can adapt them to a 2D model by setting up the scene as a plane.

Happy coding!


I think Dax Phyz has both 2D and soft bodies.


If you are only looking for deformation, I would suggest to use a polygon model (trimesh) where I would catch collisions callbacks to the rigidbody that represents that shape. Upon the colliding point I would determine the amount of deformation caused to one or several vertices.

For un-deformation I would suggest that each timestep you should iterate through your deformed points and try to push them outwards. I guess the query could be done through very small sphere-queries in your collision world. That would also require that you keep a "maximum" undeformed position.

This method would be fairly simple to implement although it wouldn't be a "soft body" which could be what you are looking for. http://chriscavanagh.wordpress.com/2008/06/24/silverlight-soft-body-physics/ seems to be an implementation that gives out source code that you might want to look into.


I don't really know what is THE best way, but here, near the end of the page is the code for "springs".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜