【问题标题】:Bullet Physics Convex hulls with cubesBullet Physics 带立方体的凸壳
【发布时间】:2014-06-20 07:40:14
【问题描述】:

我正在用 c# 开发一个游戏引擎,并且正在使用 BulletSharp 进行物理。除了立方体外,它运行良好:

http://i.stack.imgur.com/EPfrw.png

(Axis-Aligned Bounding box是透明的红色,模型是白色的)

休息时,站在他们的边缘。因为我是从 Collada 模型加载的,所以我正在创建一个 ConvexHullShape() 并将数据添加为矢量云。虽然使用BoxShape() 会更有效(并且工作正常),但我不能,因为不能保证所有模型都是立方体。我无法弄清楚为什么它们停留在顶点而不是平坦的边缘。我对ConvexHullShape 的实现是错误的,还是我需要使用不同类型的形状(以使物理正常工作)?

public RigidBody AddDynamicGeometry(ColladaGeometry geometry, Matrix4 transform)
        {
            List<Vector3> points = new List<Vector3>();
            foreach (Triangle tri in geometry.triangles)
            {
                points.Add(tri.vertices[0]);
                points.Add(tri.vertices[1]);
                points.Add(tri.vertices[2]);
            }
            CollisionShape shape = new ConvexHullShape(points);



            shape.UserObject = geometry;

            collisionShapes.Add(shape);

            RigidBody body = CreateRigidBody(geometry.triangles.Count * 10, transform, shape);

            return body;
        }

        public RigidBody CreateRigidBody(float mass, Matrix4 startTransform, CollisionShape shape)
        {
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;
            if (isDynamic)
                shape.CalculateLocalInertia(mass, out localInertia);

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            physics_world.AddRigidBody(body);

            return body;
        }

【问题讨论】:

    标签: c# bulletphysics


    【解决方案1】:

    ConvexHullShape 预计质心 (COM) 为 (0,0,0),但立方体偏离中心,使其向角倾斜。

    您可以使用 ConvexTriangleMeshShape.CalculatePrincipalAxisTransform 找到正确的 COM。然后,您可以从每个顶点中减去 COM,使 COM 回到 0。但是,创建具有立方体局部中心的 CompoundShape 会更容易。

    // Create a ConvexTriangleMeshShape from the points
    const int indexStride = 3 * sizeof(int);
    const int vertexStride = 12;
    int vertexCount = points.Count;
    int indexCount = vertexCount / 3;
    
    TriangleIndexVertexArray vertexArray = new TriangleIndexVertexArray();
    IndexedMesh mesh = new IndexedMesh();
    mesh.Allocate(vertexCount, vertexStride, indexCount, indexStride);
    Vector3Array vdata = mesh.Vertices;
    IntArray idata = mesh.TriangleIndices;
    for (int i = 0; i < vertexCount; i++)
    {
        vdata[i] = points[i];
        idata[i] = i;
    }
    vertexArray.AddIndexedMesh(mesh);
    ConvexTriangleMeshShape shape = new ConvexTriangleMeshShape(vertexArray, true);
    
    // Calculate center of mass
    Matrix center = Matrix.Identity;
    Vector3 inertia;
    float volume;
    shape.CalculatePrincipalAxisTransform(ref center, out inertia, out volume);
    
    // Create a CompoundShape with COM offset
    CompoundShape compound = new CompoundShape();
    compound.AddChildShape(Matrix.Invert(center), shape);
    

    注意:ConvexTriangleMeshShape.CalculatePrincipalAxisTransform 在 SVN 主干中有效,但在 BulletSharp 2.82 中无效。很快就会有一个错误修复版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      相关资源
      最近更新 更多