如何在LibGDX Bullet Physics中使软体弹跳?

时间:2018-09-22 06:15:29

标签: libgdx game-physics bulletphysics

如何在LibGDX Bullet Physics中使软体弹跳。我将“软体球”的“恢复”设置为0.5,将刚体地面的“恢复”设置为1.0。我为球对象尝试了btSoftBody,为地面对象尝试了btRigidBody。碰到地面时球不会反弹。

@Override
public void create() {
    super.create();
    world.maxSubSteps = 20;
    world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 0.25f + 0.5f * (float) Math.random(), 1f);
    final Texture texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
    final Material material = new Material(TextureAttribute.createDiffuse(texture), ColorAttribute.createSpecular(1, 1, 1, 1), FloatAttribute.createShininess(8f));
    final long attributes = Usage.Position | Usage.Normal | Usage.TextureCoordinates;
    final Model sphere = modelBuilder.createSphere(0.5f, 0.5f, 0.5f, 24, 24, material, attributes);
    model = sphere;
    MeshPart meshPart = model.nodes.get(0).parts.get(0).meshPart;
    meshPart.mesh.scale(6, 6, 6);
    indexMap = BufferUtils.newShortBuffer(meshPart.size);
    positionOffset = meshPart.mesh.getVertexAttribute(Usage.Position).offset;
    normalOffset = meshPart.mesh.getVertexAttribute(Usage.Normal).offset;
    softBody = new btSoftBody(worldInfo, meshPart.mesh.getVerticesBuffer(), meshPart.mesh.getVertexSize(), positionOffset, normalOffset, meshPart.mesh.getIndicesBuffer(), meshPart.offset, meshPart.size, indexMap, 0);
    btSoftBody.Material pm = softBody.appendMaterial();
    pm.setKLST(0.9f);
    pm.setFlags(0);
    softBody.generateBendingConstraints(2, pm);
    softBody.setConfig_piterations(90);
    softBody.setConfig_kDF(0.5f);
    softBody.setConfig_kCHR(1);
    softBody.setConfig_kDP(0);
    softBody.randomizeConstraints();
    softBody.setTotalMass(0.01f, true);
    softBody.setContactStiffnessAndDamping(2000f, 0f);
    softBody.generateClusters(1);
    softBody.setConfig_maxvolume(200f);
    softBody.setRestitution(0.5f);
    softBody.setCollisionFlags(softBody.getCollisionFlags() | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
    softBody.setFriction(0f);
    ((btSoftRigidDynamicsWorld) (world.collisionWorld)).addSoftBody(softBody);
    world.entities.get(0).body.setRestitution(1f);
    world.add(entity = new BulletEntity(model, (btCollisionObject) null, 1, 5, 1));
    softBody.setActivationState(Collision.WANTS_DEACTIVATION);
    softBody.translate(tmpV.set(1, 5, 1));

}

1 个答案:

答案 0 :(得分:0)

尝试一下:

  softBody.generateBendingConstraints(2, pm);
  pm.setKLST(0.2f);
  pm.setFlags(0);
  softBody.generateBendingConstraints(2, pm);
  softBody.setConfig_piterations(7);
  softBody.setConfig_kDF(0.2f);
  softBody.randomizeConstraints();
  softBody.setTotalMass(1);
  softBody.translate(tmpV.set(1, 5, 1));
  ((btSoftRigidDynamicsWorld)(world.collisionWorld)).addSoftBody(softBody);

而不是:

 pm.setKLST(0.9f);
pm.setFlags(0);
softBody.generateBendingConstraints(2, pm);
softBody.setConfig_piterations(90);
softBody.setConfig_kDF(0.5f);
softBody.setConfig_kCHR(1);
softBody.setConfig_kDP(0);
softBody.randomizeConstraints();
softBody.setTotalMass(0.01f, true);
softBody.setContactStiffnessAndDamping(2000f, 0f);
softBody.generateClusters(1);
softBody.setConfig_maxvolume(200f);
softBody.setRestitution(0.5f);
softBody.setCollisionFlags(softBody.getCollisionFlags() | 
btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
softBody.setFriction(0f);