FreshRSS

Normální zobrazení

Jsou dostupné nové články, klikněte pro obnovení stránky.
PředevčíremHlavní kanál

Libgdx Bullet Physics not applying gravity to model instance

I create a modelinstance in Libgdx that I call yellowInstance. The modelinstance is defined as follows: I need it to fall down under the force of gravity, But it just stays in the air!

 ModelBuilder yellowBuilder = new ModelBuilder();
            yellowBuilder.begin();
            Node nod = yellowBuilder.node();
            nod.id = "yellowboxy";
            Material yellowMat = new Material();
            yellowMat.set(PBRColorAttribute.createBaseColorFactor(Color.YELLOW));
            MeshPartBuilder yellowPartBuilder = yellowBuilder.part("yellowboxy", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal, yellowMat);
            BoxShapeBuilder.build(yellowPartBuilder, 10f, 20f, 10f, 2f, 2f, 2f);
            yellowInstance = new ModelInstance(yellowBuilder.end());

I create the physics for it using Bullet Physics library as such:

btCollisionShape btBox = new btBoxShape(new Vector3(1,1,1)); //notice we take halves!
    Vector3 localInertia = new Vector3();
    btBox.calculateLocalInertia(5f,localInertia);

    //MotionStateForPhys msphys = new MotionStateForPhys(yellowInstance.transform);

    btRigidBody.btRigidBodyConstructionInfo info = new btRigidBody.btRigidBodyConstructionInfo(5f,null,btBox,localInertia);
    btRigidBody btYellowBody = new btRigidBody(info);

    /*btYellowBody.setMotionState(msphys);*/

    btYellowBody.setWorldTransform(yellowInstance.transform);

    dynamicsWorld.addRigidBody(btYellowBody);

    btYellowBody.activate(true);

here are the definitions for the important physics variables needed by the library

private btCollisionConfiguration collisionConfiguration;
private com.badlogic.gdx.physics.bullet.collision.btDispatcher btDispatcher;
private btDiscreteDynamicsWorld dynamicsWorld;
private btSequentialImpulseConstraintSolver solver;

collisionConfiguration = new btDefaultCollisionConfiguration();
    btDispatcher = new btCollisionDispatcher(collisionConfiguration);
    btInterface = new btDbvtBroadphase();
    solver = new btSequentialImpulseConstraintSolver();
    dynamicsWorld = new 
btDiscreteDynamicsWorld(btDispatcher,btInterface,solver,collisionConfiguration);
    dynamicsWorld.setGravity(new Vector3(0,-10f,0));

Here is how I update the timeStep for the Physics library:

private void update(float deltatime){
btYellowBody.activate(true);
    dynamicsWorld.stepSimulation(deltatime , 5 , 1/60f);

} 

Here is my render method that calls the update method where my Physics variables are:

inputHandler.UpdateAfterKeyPress(Gdx.graphics.getDeltaTime(),"levelone");
    
    worldBuilder.update(delta); //will update the physics in levelone!

    Gdx.gl.glClearColor(0, .25f, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    ScreenUtils.clear(BACKGROUND_COLOUR, true);

managerScenes.update(Gdx.graphics.getDeltaTime());
    managerScenes.render();

if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE))
        Gdx.app.exit();

EDIT: IF I REMOVE THE FLOOR FROM THE PHYSICS SIMULATION, THE YELLOW BOX FALLS UNDER THE FORCE OF GRAVITY! WHY REMOVING THE FLOOR WILL MAKE THE BOX FALL DOWN!

this is how the floor is added: THERE IS SOMETHING I'M MISSING HERE!!

ModelBuilder mBuilder = new ModelBuilder();
    mBuilder.begin();

    // Start a new node with a specific name
    Node node = mBuilder.node();
    node.id = "floory"; // Set the node's id

    Material mat = new Material();
    mat.set(PBRColorAttribute.createBaseColorFactor(Color.BLACK));
    MeshPartBuilder mpartbuilder = mBuilder.part("floory", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position|VertexAttributes.Usage.Normal,mat);
    BoxShapeBuilder.build(mpartbuilder,0,-0.5f,0,300f,1f,400f);
    ModelInstance mInstance = new ModelInstance(mBuilder.end());

    sManager.addScene(new Scene(mInstance));

    //create the physics id and body/shape properties
   

    btCollisionShape shape = Bullet.obtainStaticNodeShape(mInstance.nodes);


    btBoxShape/*btCollisionShape*/ btBox = new btBoxShape(new Vector3(150,0.5f,200)); //notice we take halves!
    btRigidBody.btRigidBodyConstructionInfo info = new btRigidBody.btRigidBodyConstructionInfo(0,null,btBox,Vector3.Zero);
    btRigidBody btBody = new btRigidBody(info);
    btBody.setWorldTransform(mInstance.transform);
  

    dynamicsWorld.addCollisionObject(btBody);
❌
❌