如何让我的物理对象安定下来?

时间:2012-05-28 20:25:01

标签: c# bullet bulletphysics rigid-bodies

我正在使用BulletSharp,它是子弹库的C#发行版。我已经在一个据说具有0.0f的恢复原状的物体中得到了一些弹跳。

我有一个动态圆柱体(很快将是一个网状物体)落在两个静态圆​​柱体上。像这样:

starting object positions

顶部的气缸经常会剧烈反弹,通常会弹到一边。

这是我用来设置场景的代码:

        //now figure out bulletsharp stuff...
        CollisionConfiguration collConfig = new DefaultCollisionConfiguration();
        Dispatcher collDispatch = new CollisionDispatcher(collConfig);

        BroadphaseInterface broadphase = new DbvtBroadphase();
        ConstraintSolver sol = new SequentialImpulseConstraintSolver();
        world = new DiscreteDynamicsWorld(collDispatch, broadphase, sol, collConfig);

        world.Gravity = new Vector3(0.0f, -10.0f, 0.0f);

        //log (moving object)
        MotionState still = new DefaultMotionState();
        CylinderShape shape = new CylinderShapeZ(0.5f, 1.0f, 1.0f);
        still.WorldTransform = Matrix.Translation(0.0f, 0.4f, 0.0f);
        RigidBodyConstructionInfo constructInfo = new RigidBodyConstructionInfo(1.0f, still, shape);
        logBody = new RigidBody(constructInfo);
        logBody.SetDamping(0.04f, 0.1f);
        world.AddRigidBody(logBody);

        //rollers (static objects)
        CylinderShape r1s = new CylinderShapeZ(0.1f, 1.0f, 1.0f);
        MotionState r1m = new DefaultMotionState();
        r1m.WorldTransform = Matrix.Translation(-0.2f, -0.4f, 0.0f);
        RigidBodyConstructionInfo r1ci = new RigidBodyConstructionInfo(0.0f, r1m, r1s);
        r1 = new RigidBody(r1ci);
        world.AddRigidBody(r1);

        CylinderShape r2s = new CylinderShapeZ(0.1f, 1.0f, 1.0f);
        MotionState r2m = new DefaultMotionState();
        r2m.WorldTransform = Matrix.Translation(0.2f, -0.4f, 0.0f);
        RigidBodyConstructionInfo r2ci = new RigidBodyConstructionInfo(0.0f, r2m, r2s);
        r2 = new RigidBody(r2ci);
        world.AddRigidBody(r2);

我使用world.StepSimulation(0.05f, 100, 0.0005f);的每一帧来更新物理模拟。

我错过了任何明显的设置吗?为什么我的模拟会这样做?

小更新:我已成功在Blender的Bullet中进行了类似的模拟。那里没有弹跳......我不知道那和那之间可能有什么不同。

1 个答案:

答案 0 :(得分:4)

您没有将Inertia添加到模型中。这应该减慢抖动,并且不应该产生以其从滚筒中弹回的混响。您需要为所有三个对象添加它,包括滚轮上的零点。试试这个,让我知道它是如何工作的:

//now figure out bulletsharp stuff...
CollisionConfiguration collConfig = new DefaultCollisionConfiguration();
Dispatcher collDispatch = new CollisionDispatcher(collConfig);

BroadphaseInterface broadphase = new DbvtBroadphase();
ConstraintSolver sol = new SequentialImpulseConstraintSolver();
world = new DiscreteDynamicsWorld(collDispatch, broadphase, sol, collConfig);

world.Gravity = new Vector3(0.0f, -10.0f, 0.0f);

//log (moving object)
Vector3 cylinderInertia;
MotionState still = new DefaultMotionState();
CylinderShape shape = new CylinderShapeZ(0.5f, 1.0f, 1.0f);
still.WorldTransform = Matrix.Translation(0.0f, 0.4f, 0.0f);
shape.CalculateLocalInertia(1.0f, out cylinderInertia);    
RigidBodyConstructionInfo constructInfo = new RigidBodyConstructionInfo(1.0f, still, shape, cylinderInertia);
logBody = new RigidBody(constructInfo);
logBody.SetDamping(0.04f, 0.1f);
world.AddRigidBody(logBody);

//rollers (static objects)
CylinderShape r1s = new CylinderShapeZ(0.1f, 1.0f, 1.0f);
MotionState r1m = new DefaultMotionState();
r1m.WorldTransform = Matrix.Translation(-0.2f, -0.4f, 0.0f);
RigidBodyConstructionInfo r1ci = new RigidBodyConstructionInfo(0.0f, r1m, r1s, Vector3.Zero);
r1 = new RigidBody(r1ci);
world.AddRigidBody(r1);

CylinderShape r2s = new CylinderShapeZ(0.1f, 1.0f, 1.0f);
MotionState r2m = new DefaultMotionState();
r2m.WorldTransform = Matrix.Translation(0.2f, -0.4f, 0.0f);
RigidBodyConstructionInfo r2ci = new RigidBodyConstructionInfo(0.0f, r2m, r2s, Vector3.Zero);
r2 = new RigidBody(r2ci);
world.AddRigidBody(r2);