hello world bullet

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

hello world bullet

Postby matan45 » Wed Oct 23, 2019 9:21 am

hi i am trying to do the helloworld in gdxbullet and i getting all 000 in the result i think the problem is in the Transform or in the loop
https://ibb.co/gj7Ng3c
Code: Select all
package test;

import java.util.ArrayList;

import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.bullet.collision.btBoxShape;
import com.badlogic.gdx.physics.bullet.collision.btBroadphaseInterface;
import com.badlogic.gdx.physics.bullet.collision.btCollisionConfiguration;
import com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher;
import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
import com.badlogic.gdx.physics.bullet.collision.btCollisionShape;
import com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase;
import com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration;
import com.badlogic.gdx.physics.bullet.collision.btSphereShape;
import com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo;
import com.badlogic.gdx.physics.bullet.dynamics.btSequentialImpulseConstraintSolver;
import com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState;
import com.badlogic.gdx.physics.bullet.linearmath.btTransform;

public class HelloBullet {

   public static void main(String[] args) {
     
      System.load("D:\\java\\gdxbullet\\gdx-bullet64.dll");
     
      btCollisionConfiguration collisionConfig;
      btCollisionDispatcher dispatcher;
      btBroadphaseInterface overlappingPairCache;
      btSequentialImpulseConstraintSolver solver;
      btDiscreteDynamicsWorld dynamicWorld;
      ArrayList<btCollisionShape> collisionShapes = new ArrayList<btCollisionShape>();

      collisionConfig = new btDefaultCollisionConfiguration();
      dispatcher = new btCollisionDispatcher(collisionConfig);
      overlappingPairCache = new btDbvtBroadphase();
      solver = new btSequentialImpulseConstraintSolver();
      dynamicWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfig);

      dynamicWorld.setGravity(new Vector3(0, -10f, 0));
      {
         btCollisionShape groundShape = new btBoxShape(new Vector3(50, 50, 50));
         collisionShapes.add(groundShape);

         btTransform groundTransform = new btTransform();
         groundTransform.setIdentity();
         groundTransform.setOrigin(new Vector3(0, -56, 0));

         float mass = 0;
         boolean isDynamic = (mass != 0.f);
         Vector3 localInertia = new Vector3(0, 0, 0);
         if (isDynamic)
            groundShape.calculateLocalInertia(mass, localInertia);

         btDefaultMotionState myMotionState = new btDefaultMotionState(groundTransform.inverse());
         btRigidBodyConstructionInfo rbInfo = new btRigidBodyConstructionInfo(mass, myMotionState, groundShape,
               localInertia);
         btRigidBody body = new btRigidBody(rbInfo);
         dynamicWorld.addRigidBody(body);
      }

      {
         btCollisionShape colShape = new btSphereShape(1);
         collisionShapes.add(colShape);

         btTransform startTransform = new btTransform();
         startTransform.setIdentity();
         float mass = 1;
         boolean isDynamic = (mass != 0.f);
         Vector3 localInertia = new Vector3(0, 0, 0);
         if (isDynamic)
            colShape.calculateLocalInertia(mass, localInertia);
         startTransform.setOrigin(new Vector3(2, 10, 0));
         btDefaultMotionState myMotionState = new btDefaultMotionState(startTransform.inverse());
         btRigidBodyConstructionInfo rbInfo = new btRigidBodyConstructionInfo(mass, myMotionState, colShape,
               localInertia);
         btRigidBody body = new btRigidBody(rbInfo);
         dynamicWorld.addRigidBody(body);
      }

      for(int i=0;i<100;i++) {
         dynamicWorld.stepSimulation(1.f/60.f,10);
         for (int j=dynamicWorld.getNumCollisionObjects()-1;j>=0;j--) {
            btCollisionObject obj= dynamicWorld.getCollisionObjectArray().atConst(j);
            btRigidBody body = (btRigidBody)obj;
            if(body != null && body.getMotionState() != null) {
               btTransform trans=new btTransform();
               body.getMotionState().getWorldTransform(trans.inverse());
               System.out.printf("world pos %d = %f,%f,%f\n",j, trans.getOrigin().x, trans.getOrigin().y, trans.getOrigin().z);
            }
         }
      }

      collisionShapes.clear();
      dynamicWorld.dispose();
      overlappingPairCache.dispose();
      dispatcher.dispose();
      collisionConfig.dispose();
   }
}
matan45
 
Posts: 6
Joined: Wed Oct 23, 2019 8:30 am

Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest