首页 > 解决方案 > gdx-bullet hello world 首次使用

问题描述

我正在尝试单独使用 gdx-bullet 并像在 cpp 中一样运行 hello world ,并且我得到的值从 0 到 2 甚至更高。

为什么在 jbullet 和 cpp 项目符号中工作正常,知道为什么它可能是setToTranslation或字符串输出 图像。这是代码:

public static void main(String[] args) {
  Bullet.init();

  btCollisionConfiguration collisionConfig;
  btCollisionDispatcher dispatcher;
  btBroadphaseInterface overlappingPairCache;
  btSequentialImpulseConstraintSolver solver;
  btDiscreteDynamicsWorld dynamicWorld;
  ArrayList<btCollisionShape> collisionShapes;

  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.f, 50.f, 50.f));
     collisionShapes.add(groundShape);

     Matrix4 groundTransform = new Matrix4();
     groundTransform.setToTranslation(new Vector3(0, -56, 0));

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

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

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

     Matrix4 startTransform = new Matrix4();
     startTransform.setToTranslation(new Vector3(2, 10, 0));

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

     btDefaultMotionState myMotionState = new btDefaultMotionState(startTransform);
     btRigidBodyConstructionInfo rbInfo = new btRigidBodyConstructionInfo(mass, myMotionState, colShape,
           localInertia);
     btRigidBody body = new btRigidBody(rbInfo);
     dynamicWorld.addRigidBody(body);
  }


  for (int i = 0; i < 150; 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) {
           Matrix4 trans = new Matrix4();
           body.getMotionState().getWorldTransform(trans);
           System.out.println(
                 "world pos " + j + " = " + trans.val[12] + " , " + trans.val[13] + ", " + trans.val[14]);
        }
     }
  }

  collisionShapes.clear();
  dynamicWorld.dispose();
  overlappingPairCache.dispose();
  dispatcher.dispose();
  collisionConfig.dispose();

}

第二个角色对主角的感觉如何?

主角想对第二个角色说的话

标签: javalibgdx

解决方案


推荐阅读