Java 我的刚体模型掉到地板上了

Java 我的刚体模型掉到地板上了,java,libgdx,Java,Libgdx,我的刚体模型从地板上掉了下来。我正在试验Libgdx。当模型落在由长方体创建的地面上时,它们会停止移动,但当我用blender创建的模型替换地面时,对象会掉落。我正试图建造一条公路。下面是我尝试的代码和其他示例,我一直在获取从建模地面掉落的对象。我用搅拌机做模特 public BulletSample(ThrustCopter thrustCopter) { super(thrustCopter); // Create ModelBatch that will rend

我的刚体模型从地板上掉了下来。我正在试验Libgdx。当模型落在由长方体创建的地面上时,它们会停止移动,但当我用blender创建的模型替换地面时,对象会掉落。我正试图建造一条公路。下面是我尝试的代码和其他示例,我一直在获取从建模地面掉落的对象。我用搅拌机做模特

     public BulletSample(ThrustCopter thrustCopter) {
    super(thrustCopter);
    // Create ModelBatch that will render all models using a camera
    modelBatch = new ModelBatch(new DefaultShaderProvider());

    // Create a camera and point it to our model
    camera = new PerspectiveCamera(70, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(10f, 10f, 12f);
    camera.lookAt(0, 0, 0);
    camera.near = 0.1f;
    camera.far = 300f;
    camera.update();

    // Create the generic camera input controller to make the app interactive
    cameraController = new CameraInputController(camera);
    Gdx.input.setInputProcessor(cameraController);

    // Set up environment with simple lighting
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    // environment.set(new ColorAttribute(ColorAttribute.Fog, 0.13f, 0.13f, 0.13f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.8f, 0.3f, -1f));
    shadowLight = new DirectionalShadowLight(1024, 1024, 60, 60, 1f, 300);
    shadowLight.set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f);
    environment.add(shadowLight);
    environment.shadowMap = shadowLight;
    shadowBatch = new ModelBatch(new DepthShaderProvider());

    Bullet.init();

    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
    broadphase = new btDbvtBroadphase();
    solver = new btSequentialImpulseConstraintSolver();
    world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    world.setGravity(new Vector3(0, -9.81f, 1f));

    ModelBuilder modelBuilder = new ModelBuilder();
/*  Model ground = modelBuilder.createBox(40f, 2f, 40f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
    groundInstance=new ModelInstance(ground);

    btCollisionShape groundshape = new btBoxShape(new Vector3(20, 1f, 20));
    shapes.add(groundshape);
    btRigidBody body = new btRigidBody(0,null,groundshape);
    bodies.add(body);
    world.addRigidBody(body); */
// testing rigid body ground with model from blender
    game.manager.load("gameAssets/roadtest1.g3db", Model.class);
    game.manager.finishLoading();

    Model roadmodel = game.manager.get("gameAssets/roadtest1.g3db", Model.class);
     roadInstance = new ModelInstance(roadmodel);
    //instances.add(road);
    btCollisionShape roadshape = createConvexHullShape(roadmodel, true);
    shapes.add(roadshape);
    btRigidBody body = new btRigidBody(0, null, roadshape);
    world.addRigidBody(body);

    Vector3 position=new Vector3();
    for(int i=0;i<10;i++){
        Model box = modelBuilder.createBox(1f, 1f, 1f, 
                new Material(ColorAttribute.createDiffuse(Color.BLUE)),
                Usage.Position | Usage.Normal);
        ModelInstance boxInstance=new ModelInstance(box);
        instances.add(boxInstance);
        models.add(box);
        if(i<5){
            position.set(-1, i+1, 0);
        }else{
            position.set(1, i-4, 0);
        }
        boxInstance.transform.setToTranslation(position);

        btDefaultMotionState motionState = new btDefaultMotionState(boxInstance.transform);
        motionState.setWorldTransform(boxInstance.transform.trn(0, 0, 0));
        motionStates.add(motionState);
        btCollisionShape boxshape = new btBoxShape(new Vector3(0.5f, 0.5f, 0.5f));
        shapes.add(boxshape);
        btRigidBody boxbody = new btRigidBody(1, motionState, boxshape);
        bodies.add(boxbody);
        world.addRigidBody(boxbody);
    }


    //Loading model
    game.manager.load("planeanim1.g3db", Model.class);
    game.manager.finishLoading();

    Model model = game.manager.get("planeanim1.g3db", Model.class);
    ModelInstance plane = new ModelInstance(model);
    instances.add(plane);

    plane.transform.setToRotation(Vector3.Y, 180);
    plane.transform.trn(0, 7, 10);

    btDefaultMotionState motionState = new btDefaultMotionState(plane.transform);
    motionState.setWorldTransform(plane.transform.trn(0, 0, 0));
//  motionState.setWorldTransform(plane.transform.rotate(0, 0, .5f,45));
    motionStates.add(motionState);
    btCollisionShape planeshape = createConvexHullShape(model,true);
    shapes.add(planeshape);
    btRigidBody planebody = new btRigidBody(5, motionState, planeshape);
    bodies.add(planebody);
    world.addRigidBody(planebody);
    planebody.userData="plane";
    planebody.applyCentralImpulse(new Vector3(0,0,-65));

    // You use an AnimationController to control animations.  Each control is tied to the model instance
    controller = new AnimationController(plane);  
    // Pick the current animation by name
    controller.setAnimation("Scene",-1);

    contactListener = new MyContactListener();
}
public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize) {
    final Mesh mesh = model.meshes.get(0);
    final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
    if (!optimize) return shape;
    // now optimize the shape
    final btShapeHull hull = new btShapeHull(shape);
    hull.buildHull(shape.getMargin());
    final btConvexHullShape result = new btConvexHullShape(hull);
    // delete the temporary shape
    shape.dispose();
    hull.dispose();
    return result;
}

@Override
public void dispose() {
    groundInstance.model.dispose();
    instances.clear();
    modelBatch.dispose();
    for (Model model : models)
        model.dispose();
    for (btRigidBody body : bodies) {
        body.dispose();
    }
    for (btMotionState motion : motionStates)
        motion.dispose();
    for (btCollisionShape shape : shapes)
        shape.dispose();
    world.dispose();
    collisionConfiguration.dispose();
    dispatcher.dispose();
    broadphase.dispose();
    solver.dispose();
    contactListener.dispose();
    shadowBatch.dispose();
    shadowLight.dispose();
}

@Override
public void render(float delta) {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    Gdx.gl.glClearColor(0.13f, 0.13f, 0.13f, 1);

    if(started){
    world.stepSimulation(Gdx.graphics.getDeltaTime(), 5);
    for (int i = 0; i < motionStates.size; i++) {
        motionStates.get(i).getWorldTransform(instances.get(i).transform);
    }
    }else{
        if(Gdx.input.isTouched()){
            started=true;
            System.out.println(instances.size);
            if(Gdx.input.justTouched())instances.get(10).transform.rotate(0,0.2f,0,25);
        }
    }

    // Respond to user events and update the camera
    cameraController.update();
    controller.update(delta);

    shadowLight.begin(Vector3.Zero, camera.direction);
    shadowBatch.begin(shadowLight.getCamera());
    shadowBatch.render(instances);

    shadowBatch.end();
    shadowLight.end();

    // Draw all model instances using the camera
    modelBatch.begin(camera);
    //modelBatch.render(groundInstance, environment);
    modelBatch.render(roadInstance, environment);
    modelBatch.render(instances, environment);
    modelBatch.end();

    super.render(delta);
}  
公共公告样本(推进器直升机推进器直升机){
超级直升机;
//创建将使用摄影机渲染所有模型的ModelBatch
modelBatch=newmodelbatch(newdefaultshaderprovider());
//创建一个摄影机并将其指向我们的模型
camera=new PerspectiveCamera(70,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
摄像机位置设置(10f、10f、12f);
摄像机。注视(0,0,0);
摄像机近距离=0.1f;
照相机。远=300f;
camera.update();
//创建通用相机输入控制器,使应用程序具有交互性
cameraController=新的CameraInputController(摄像头);
Gdx.input.setInputProcessor(cameraController);
//设置简单的照明环境
环境=新环境();
set(新的ColorAttribute(ColorAttribute.AmbientLight,0.4f,0.4f,0.4f,1f));
//设置(新的ColorAttribute(ColorAttribute.Fog,0.13f,0.13f,0.13f,1f));
添加(新的DirectionalLight().set(0.8f,0.8f,0.8f,-0.8f,0.3f,-1f));
阴影灯=新方向阴影灯(1024、1024、60、60、1f、300);
阴影灯组(0.8f,0.8f,0.8f,-1f,-.8f,-.2f);
添加(阴影灯);
environment.shadowMap=阴影灯;
shadowBatch=newmodelbatch(newdepthshaderprovider());
Bullet.init();
collisionConfiguration=新的btDefaultCollisionConfiguration();
dispatcher=新的btCollisionDispatcher(collisionConfiguration);
broadphase=新的btDbvtBroadphase();
解算器=新的BTSequentialPulseConstraintSolver();
world=新的BTDiscretedDynamicsWorld(调度程序、宽相位、解算器、碰撞配置);
世界地心引力(新矢量3(0,-9.81f,1f));
ModelBuilder ModelBuilder=新的ModelBuilder();
/*模型地面=modelBuilder.createBox(40f,2f,40f,
新材质(ColorAttribute.createDiffuse(Color.GREEN)),
用法.位置|用法.正常);
groundInstance=新模型实例(地面);
btCollisionShape groundshape=新的btBoxShape(新矢量3(20,1f,20));
形状。添加(groundshape);
B刚体=新的B刚体(0,空,地面形状);
主体。添加(主体);
世界.刚体(体)*/
//用搅拌机模型测试刚体地面
game.manager.load(“gameAssets/roadtest1.g3db”,Model.class);
game.manager.finishLoading();
Model roadmodel=game.manager.get(“gameAssets/roadtest1.g3db”,Model.class);
roadInstance=新模型实例(roadmodel);
//例.加上(道路);;
btCollisionShape roadshape=createConvexHullShape(道路模型,真);
形状。添加(道路形状);
B刚体=新的B刚体(0,空,道路形状);
世界.刚体(体);
Vector3位置=新Vector3();

对于(int i=0;i尝试使用调试模式(如此处所述)查找bug您的createConvexHullShape方法假设未转换的网格。由于您使用blender,很可能是模型零件被转换。这可能会导致您看到的行为。感谢您的响应,但我如何更正此问题,是从代码还是从混合器文件?如果你真的需要一个凸包,那么你应该确保你的模型适用于此。但是,看看你的代码,你可能不应该一开始就使用凸包。使用
Bullet.getainStaticNodeShape
可能更合适。但是在这种情况下,你的模型仍然不能进行缩放,或者太大或太小oo小三角形,因此您可能必须检查它。即使如此,它也可能不适用。变量名为“road”,如果您希望汽车或角色在该形状上平稳移动,那么您可以更好地使用原始形状,如长方体。