C++ 中的非刚体 2D 物理引擎
我正在尝试用 C++ 尝试 2D 物理引擎。到目前为止,似乎最受欢迎的是 Box2D。不幸的是,Box2D 是一个刚体物理引擎,它并不能真正帮助我实现我想要尝试的目标。
我希望能够定义一个具有多个由弹簧连接的顶点的形状,这样当该形状与刚性或其他非刚性形状碰撞时,其形状将是灵活的。
现在,我尝试考虑仅使用刚体在 Box2D 中执行此操作的方法,但似乎总是存在缺陷:
- 使用矩形或线段作为柔性形状的外边缘。这些将与其他形状发生碰撞。不幸的是,它们没有弹性,因此看不到预期的效果。
- 该形状的每个顶点都可以是一个具有自己的小圆形形状的实体。然后可以通过弹簧将这些主体连接在一起。这对于形状的变形来说效果很好,但想象一下,如果形状落在一个刚性的尖峰上,并且尖峰刚刚穿过顶点之间。然后形状就会卡在尖刺上。
那么在 C++ 中进行此类物理处理的最佳方法是什么?最好不必编写整个物理引擎。也许我只是缺少 Box2D 的一个功能。也许这不是正确的选择。那么什么是正确的选择呢?
I'm trying to experiment with 2D physics engines in C++. So far, it seems the most popular 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:
- 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.
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有几个支持可变形/软体的包/引擎。如果您想要免费的东西,您可以查看 Phyz、SOFA 或 子弹。
wikipedia 上有详细列表。其中大多数都是基于 3D 的,但您可以通过将场景设置为平面来将它们调整为 2D 模型。
快乐编码!
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!
我认为 Dax Phyz 既有 2D 又有软体。
I think Dax Phyz has both 2D and soft bodies.
如果您只是寻找变形,我建议使用多边形模型(修剪网格),我可以在其中捕获对表示该形状的刚体的碰撞回调。在碰撞点上,我将确定对一个或多个顶点造成的变形量。
对于不变形,我建议每个时间步都应该迭代变形点并尝试将它们向外推。我想查询可以通过碰撞世界中非常小的球体查询来完成。这还要求您保持“最大”不变形位置。
这种方法实施起来相当简单,尽管它不是您正在寻找的“软体”。 http://chriscavanagh.wordpress.com/2008/06 /24/silverlight-soft-body-physicals/ 似乎是一个实现,它给出了您可能想要研究的源代码。
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.
我真的不知道什么是最好的方法,但是 这里,靠近页面末尾的是“springs”的代码。
I don't really know what is THE best way, but here, near the end of the page is the code for "springs".