使用子弹在球体内的球

时间:2013-05-20 13:54:01

标签: android libgdx physics bullet bulletphysics

我是Bullet和所有3D和phisycs的新手,所以不要生气:) 我需要在里面创建一个大的静态刚性球体和一个小动态球体。我想像瓶子一样使用大的,所以小球体可以在里面移动,但是不能离开球体,在顶部有一个平面。 我在Blender写了2个模型(当然我在大球体上打了一个洞),创造了一个世界并在里面放置了物体。但是当我启动应用程序时,小球体会以极快的速度抛出大球。 此外,如果有帮助的话,我正在使用子弹与GDX库的Android。

此代码初始化世界。

public final btCollisionConfiguration collisionConfiguration;
public final btCollisionDispatcher dispatcher;
public final btBroadphaseInterface broadphase;
public final btConstraintSolver solver;
public final btCollisionWorld collisionWorld;
public PerformanceCounter performanceCounter;
public final Vector3 gravity;   

public int maxSubSteps = 5;

public World() {
    Vector3 gravity = new Vector3(0, -10, 0);

    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
    broadphase = new btDbvtBroadphase();
    solver = new btSequentialImpulseConstraintSolver();
    collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    ((btDynamicsWorld)collisionWorld).setGravity(gravity);
    this.gravity = gravity;
}

用于创建大球体的一些代码

    final StillModel sphereS = ModelLoaderRegistry.loadStillModel(Gdx.files.internal("data/Ssphere.obj"));
    sphereS.subMeshes[0].getMesh().scale(3f, 3f, 3f);

    final BoundingBox sphereSBounds = new BoundingBox();
    sphereS.getBoundingBox(sphereSBounds);
    final Constructor sphereSConstructor = new Constructor(sphereS, 0f, new btSphereShape(sphereSBounds.getDimensions().x));
    sphereSConstructor.bodyInfo.setM_restitution(1f);
    world.addConstructor("sphereS", sphereSConstructor);

小球的代码

    final StillModel sphereModel =          ModelLoaderRegistry.loadStillModel(Gdx.files.internal("data/sphere.obj"));
    sphereModel.subMeshes[0].getMesh().scale(0.8f, 0.8f, 0.8f);
    final BoundingBox sphereBounds = new BoundingBox();
    sphereModel.getBoundingBox(sphereBounds);

    final Constructor sphereConstructor = new Constructor(sphereModel, 0.25f, new btSphereShape(sphereBounds.getDimensions().x * 0.5f));
    sphereConstructor.bodyInfo.setM_restitution(1f); 
    world.addConstructor("sphere", sphereConstructor);

构造函数类只创建btRigidBodyConstructionInfo和btCollisionShape对象,构造球体并放置在世界中。

那么,你能告诉我怎样才能创建一个带球的空球体?

P.S。请不要告诉我google,我已经完成了它

P.P.S抱歉我的英文

2 个答案:

答案 0 :(得分:1)

由于你的大球体不是凸面的,你不应该使用btSphereShape。 您可以尝试btBvhTriangleMeshShape或其他一些非凸形状。

构建起来有点复杂,但查看一些示例可能会提供想法。无论如何,没有简单的方法来获得你想要的东西,因为它甚至不是一个常规的空球体(我认为你使用的libgdx框架只需要你的sphereS模型进行渲染)。

答案 1 :(得分:1)

Model sphere = modelBuilder.createSphere(-100f, 100f, 100f, 20, 20, new  Material(ColorAttribute.createDiffuse(Color.BLUE)),
            VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);

ModelInstance instance = new ModelInstance(sphere);

btBvhTriangleMeshShape  sphereShape = new btBvhTriangleMeshShape(instance.model.meshParts);

btRigidBody.btRigidBodyConstructionInfo constructionInfo = new btRigidBody.btRigidBodyConstructionInfo(0, null, sphereShape, new Vector3(0,0,0));

btRigidBody body = new btRigidBody(constructionInfo);

body.setCollisionFlags(body.getCollisionFlags()| btCollisionObject.CollisionFlags.CF_STATIC_OBJECT);

dynamicsWorld.addRigidBody(body);

它有效