Search code examples
androidlibgdxphysicsbulletbulletphysics

A ball inside a sphere using Bullet


I'm new at Bullet and all 3D and phisycs stuffs, so don't be angry :) I neen to create a big static rigid sphere and a little dynamic one inside. I want to use the big one like a bottle, so little sphere can move inside, but cann't leave the sphere, exept one plase on the top. I have written 2 models in Blender (of course i've made a hole in big sphere), created a world and placed objects inside. But when i start the application, the little sphere just throw out of the big one with extreme speed. Also I'm using Bullet with GDX library for Android, if it helps.

This code initializes the world.

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;
}

And some code for creating a big sphere

    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);

Code for a little sphere

    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);

Constructor class just creates btRigidBodyConstructionInfo and btCollisionShape objects, constructs spheres and place there in the world.

So, could you tell me how can i create an empty sphere with a ball inside?

P.S. Please, don't tell me to google, i've already done it

P.P.S Sorry for my english


Solution

  • Since your big sphere is not convex, you should not be using btSphereShape for it. You could try btBvhTriangleMeshShape, or some of the other non-convex shapes.

    It is a bit more complicated to construct, but looking at some examples might provide ideas. Anyway, there is no simple way to get what you want, because it is not even a regular empty sphere (I think the libgdx framework you are using only takes your sphereS model for rendering).