使用bullet3d物理在libgdx中弹跳球

时间:2014-06-26 11:24:43

标签: android libgdx game-physics bulletphysics

我正在尝试在3d中创建弹跳球uisng libgdx的程序。我是完全初学者刚开始接手libgdx。我能够发现蝙蝠和球之间的碰撞,但无法让它反弹

Here is my  complete code

    public class Main implements ApplicationListener {
        final static short GROUND_FLAG = 1 << 8;
        final static short OBJECT_FLAG = 1 << 9;
        final static short ALL_FLAG = -1;

        class MyContactListener extends ContactListener {
            @Override
            public boolean onContactAdded(int userValue0, int partId0, int index0,
                    int userValue1, int partId1, int index1) {
                //instances.get(userValue0).moving = false;
                //instances.get(userValue1).moving = false;
                return true;
            }
        }

        static class MyMotionState extends btMotionState {
            Matrix4 transform;

            @Override
            public void getWorldTransform(Matrix4 worldTrans) {
                worldTrans.set(transform);
            }

            @Override
            public void setWorldTransform(Matrix4 worldTrans) {
                transform.set(worldTrans);
            }
        }

        static class GameObject extends ModelInstance implements Disposable {

            public final btRigidBody body;
            public boolean moving;
            public final MyMotionState motionState;

            public GameObject(Model model, String node,
                    btRigidBody.btRigidBodyConstructionInfo constructionInfo) {
                super(model, node);
                motionState = new MyMotionState();
                motionState.transform = transform;
                body = new btRigidBody(constructionInfo);
                body.setMotionState(motionState);
            }

            @Override
            public void dispose() {
                // TODO Auto-generated method stub
                body.dispose();
                motionState.dispose();
            }

            static class Constructor implements Disposable {
                public final Model model;
                public final String node;
                public final btCollisionShape shape;
                public final btRigidBody.btRigidBodyConstructionInfo constructionInfo;
                public static Vector3 inertia = new Vector3();

                public Constructor(Model model, String node,
                        btCollisionShape shape, float mass) {
                    this.model = model;
                    this.node = node;
                    this.shape = shape;
                    if (mass > 0f) {
                        shape.calculateLocalInertia(mass, inertia);
                    } else {
                        inertia.set(0, 0, 0);
                    }
                    this.constructionInfo = new btRigidBody.btRigidBodyConstructionInfo(
                            mass, null, shape, inertia);
                }

                public GameObject construct() {
                    return new GameObject(model, node, constructionInfo);
                }

                @Override
                public void dispose() {
                    shape.dispose();
                    constructionInfo.dispose();
                }
            }
        }

        PerspectiveCamera camera;
        Environment environment;
        ModelBatch modelBatch;
        Model model;
        Array<GameObject> instances;
        ArrayMap<String, GameObject.Constructor> constructors;
        btCollisionConfiguration configuration;
        btDispatcher dispatcher;
        btBroadphaseInterface broadphaseInterface;
        btDynamicsWorld dynamicWorld;
        btConstraintSolver solver;
        MyContactListener contactListener;
        float spawTimer;

        @Override
        public void create() {
            // TODO Auto-generated method stub
            Bullet.init();

            modelBatch = new ModelBatch();
            environment = new Environment();
            environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f,
                    0.4f, 0.4f, 1f));
            environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f,
                    -0.8f, -0.2f));

            camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
                    Gdx.graphics.getHeight());
            camera.position.set(3f, 7f, 10f);
            camera.lookAt(0, 4f, 0);
            camera.near = 1f;
            camera.far = 300f;
            camera.update();

            ModelBuilder builder = new ModelBuilder();

            builder.begin();
            builder.node().id = "ground";
            builder.part("ground", GL20.GL_TRIANGLES,
                    Usage.Position | Usage.Normal,
                    new Material(ColorAttribute.createDiffuse(Color.RED))).box(5f,
                    1f, 5f);
            builder.node().id = "sphere";
            builder.part("sphere", GL20.GL_TRIANGLES,
                    Usage.Position | Usage.Normal,
                    new Material(ColorAttribute.createDiffuse(Color.GREEN)))
                    .sphere(1f, 1f, 1f, 10, 10);
            model = builder.end();
            constructors = new ArrayMap<String, Main.GameObject.Constructor>(
                    String.class, GameObject.Constructor.class);

            constructors.put("ground", new GameObject.Constructor(model, "ground",
                    new btBoxShape(new Vector3(2.5f, .5f, 2.5f)), 0f));
            constructors.put("ball", new GameObject.Constructor(model, "sphere",
                    new btSphereShape(0.5f), 3f));

            configuration = new btDefaultCollisionConfiguration();
            dispatcher = new btCollisionDispatcher(configuration);
            solver = new btSequentialImpulseConstraintSolver();
            broadphaseInterface = new btDbvtBroadphase();
            dynamicWorld = new btDiscreteDynamicsWorld(dispatcher,
                    broadphaseInterface, solver, configuration);
            dynamicWorld.setGravity(new Vector3(0, -10, 0));
            contactListener = new MyContactListener();

            instances = new Array<Main.GameObject>();
            GameObject object = constructors.get("ground").construct();
            object.body.setCollisionFlags(object.body.getCollisionFlags()
                    | btCollisionObject.CollisionFlags.CF_KINEMATIC_OBJECT);
            instances.add(object);
            dynamicWorld.addRigidBody(object.body);
            object.body.setActivationState(Collision.DISABLE_DEACTIVATION);
            createBall();

        }

        public void createBall() {
            GameObject obj = constructors.get("ball").construct();
            //obj.moving = true;
            obj.transform.setFromEulerAngles(MathUtils.random(360f),
                    MathUtils.random(360f), MathUtils.random(360f));
            obj.transform.trn(0f, 9f, 0f);
            //obj.body.proceedToTransform(obj.transform);
            obj.body.setRestitution(1.0f);
            obj.body.setFriction(1.0f);
            obj.body.setWorldTransform(obj.transform);
            obj.body.setUserValue(instances.size);
            obj.body.setCollisionFlags(obj.body.getCollisionFlags()
                    | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
            instances.add(obj);
            // dynamicWorld.addCollisionObject(obj.body, OBJECT_FLAG, GROUND_FLAG);
            dynamicWorld.addRigidBody(obj.body);
        }

        @Override
        public void resize(int width, int height) {

        }

        float angle, speed = 10f;

        @Override
        public void render() {
            // TODO Auto-generated method stub
            final float delta = Gdx.graphics.getDeltaTime();

            angle = (angle + delta+speed) % 360;
            instances.get(0).transform.setTranslation(0f,MathUtils.sinDeg(angle),0f);
            //instances.get(0).body.setWorldTransform(instances.get(0).transform);
            dynamicWorld.stepSimulation(delta, 5, 1 / 60f);


            Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1.f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

            modelBatch.begin(camera);
            modelBatch.render(instances, environment);
            modelBatch.end();
        }

        @Override
        public void pause() {

        }

        @Override
        public void resume() {

        }

        @Override
        public void dispose() {
            // TODO Auto-generated method stub
            for (GameObject obj : instances)
                obj.dispose();
            instances.clear();

            for (GameObject.Constructor ctor : constructors.values())
                ctor.dispose();
            constructors.clear();

            dynamicWorld.dispose();
            solver.dispose();
            broadphaseInterface.dispose();
            dispatcher.dispose();
            configuration.dispose();

            contactListener.dispose();

            modelBatch.dispose();
            model.dispose();
        }

    }
}

这是我现在更新的代码我能够反弹不是我的目标,但我的目标是当它击中应用于移动地面的代码无法得到球时反弹球。请帮忙。

2 个答案:

答案 0 :(得分:1)

这是代码。最后球反弹

public class Main implements ApplicationListener {
    final static short GROUND_FLAG = 1 << 8;
    final static short OBJECT_FLAG = 1 << 9;
    final static short ALL_FLAG = -1;
    public float delta;

    class MyContactListener extends ContactListener {
        @Override
        public boolean onContactAdded(int userValue0, int partId0, int index0,
                int userValue1, int partId1, int index1) {
            // Gdx.app.log("onContact", "onContact Added");
            instances.get(1).body.setLinearVelocity(new Vector3(0, 10f, 0f));
            return true;
        }
    }

    static class MyMotionState extends btMotionState {
        Matrix4 transform;

        @Override
        public void getWorldTransform(Matrix4 worldTrans) {
            worldTrans.set(transform);
        }

        @Override
        public void setWorldTransform(Matrix4 worldTrans) {
            transform.set(worldTrans);
        }
    }

    static class GameObject extends ModelInstance implements Disposable {

        public final btRigidBody body;
        public boolean moving;
        public final MyMotionState motionState;

        public GameObject(Model model, String node,
                btRigidBody.btRigidBodyConstructionInfo constructionInfo) {
            super(model, node);
            motionState = new MyMotionState();
            motionState.transform = transform;
            body = new btRigidBody(constructionInfo);
            body.setMotionState(motionState);
        }

        @Override
        public void dispose() {
            // TODO Auto-generated method stub
            body.dispose();
            motionState.dispose();
        }

        static class Constructor implements Disposable {
            public final Model model;
            public final String node;
            public final btCollisionShape shape;
            public final btRigidBody.btRigidBodyConstructionInfo constructionInfo;
            public static Vector3 inertia = new Vector3();


            public Constructor(Model model, String node,
                    btCollisionShape shape, float mass) {
                this.model = model;
                this.node = node;
                this.shape = shape;
                if (mass > 0f) {
                    shape.calculateLocalInertia(mass, inertia);

                } else {
                    inertia.set(0, 0, 0);
                }
                this.constructionInfo = new btRigidBody.btRigidBodyConstructionInfo(
                        mass, null, shape, inertia);
            }

            public GameObject construct() {
                return new GameObject(model, node, constructionInfo);
            }

            @Override
            public void dispose() {
                shape.dispose();
                constructionInfo.dispose();
            }
        }
    }

    PerspectiveCamera camera;
    Environment environment;
    ModelBatch modelBatch;
    Model model;
    Array<GameObject> instances;
    ArrayMap<String, GameObject.Constructor> constructors;
    btCollisionConfiguration configuration;
    btDispatcher dispatcher;
    btBroadphaseInterface broadphaseInterface;
    btDynamicsWorld dynamicWorld;
    btConstraintSolver solver;
    MyContactListener contactListener;
    float spawTimer;
    GameObject obj;

    @Override
    public void create() {
        // TODO Auto-generated method stub
        Bullet.init();

        modelBatch = new ModelBatch();
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f,
                0.4f, 0.4f, 1f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f,
                -0.8f, -0.2f));

        camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight());
        camera.position.set(3f, 7f, 10f);
        camera.lookAt(0, 4f, 0);
        camera.near = 1f;
        camera.far = 300f;
        camera.update();

        ModelBuilder builder = new ModelBuilder();

        builder.begin();
        builder.node().id = "ground";
        builder.part("ground", GL20.GL_TRIANGLES,
                Usage.Position | Usage.Normal,
                new Material(ColorAttribute.createDiffuse(Color.RED))).box(5f,
                1f, 5f);
        builder.node().id = "sphere";
        builder.part("sphere", GL20.GL_TRIANGLES,
                Usage.Position | Usage.Normal,
                new Material(ColorAttribute.createDiffuse(Color.GREEN)))
                .sphere(1f, 1f, 1f, 10, 10);
        model = builder.end();
        constructors = new ArrayMap<String, Main.GameObject.Constructor>(
                String.class, GameObject.Constructor.class);

        constructors.put("ground", new GameObject.Constructor(model, "ground",
                new btBoxShape(new Vector3(2.5f, .5f, 2.5f)), 0f));
        constructors.put("ball", new GameObject.Constructor(model, "sphere",
                new btSphereShape(0.5f), 0.5f));

        configuration = new btDefaultCollisionConfiguration();
        dispatcher = new btCollisionDispatcher(configuration);
        solver = new btSequentialImpulseConstraintSolver();
        broadphaseInterface = new btDbvtBroadphase();
        dynamicWorld = new btDiscreteDynamicsWorld(dispatcher,
                broadphaseInterface, solver, configuration);
        dynamicWorld.setGravity(new Vector3(0, -8f, 0));
        contactListener = new MyContactListener();

        instances = new Array<Main.GameObject>();
        GameObject object = constructors.get("ground").construct();
        object.body.setCollisionFlags(object.body.getCollisionFlags()
                | btCollisionObject.CollisionFlags.CF_KINEMATIC_OBJECT);
        instances.add(object);
        dynamicWorld.addRigidBody(object.body);
        object.body.setActivationState(Collision.DISABLE_DEACTIVATION);
        createBall();

    }

    public void createBall() {
        obj = constructors.get("ball").construct();

        obj.transform.trn(0f, 11f, 0f);
        obj.body.setWorldTransform(obj.transform);
        obj.body.setUserValue(instances.size);
        obj.body.setCollisionFlags(obj.body.getCollisionFlags()
                | btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
        obj.body.setFriction(1.0f);
        obj.body.setRestitution(3.0f);
        instances.add(obj);

        dynamicWorld.addRigidBody(obj.body);
    }

    @Override
    public void resize(int width, int height) {

    }

    float angle, speed = 10f, yTranslate = 0f, zAngle = 0f;

    @Override
    public void render() {
        // TODO Auto-generated method stub
        delta = Gdx.graphics.getDeltaTime();
        angle = (angle + delta + speed) % 360;

        if (Gdx.input.isTouched()) {

            if (yTranslate > -60f) {
                zAngle -= 0.2f;
            } else {
                // yTranslate = 30f;
            }
            instances.get(0).transform.setToRotation(0f, 0f, 2f, zAngle);


        } else {
            instances.get(0).transform.setToRotation(0f, 0f, 1f, 30f);
            instances.get(0).transform.setTranslation(0f, 0f, 0f);
        }

        //instances.get(0).transform.setTranslation(0f,0f,0f);
        dynamicWorld.stepSimulation(delta, 5, 1 / 60f);

        Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1.f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        modelBatch.begin(camera);
        modelBatch.render(instances, environment);
        modelBatch.end();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        for (GameObject obj : instances)
            obj.dispose();
        instances.clear();

        for (GameObject.Constructor ctor : constructors.values())
            ctor.dispose();
        constructors.clear();

        dynamicWorld.dispose();
        solver.dispose();
        broadphaseInterface.dispose();
        dispatcher.dispose();
        configuration.dispose();

        contactListener.dispose();

        modelBatch.dispose();
        model.dispose();
    }

}

答案 1 :(得分:0)

您正在从collision语句中读取if,该语句针对!collision进行测试,当发生碰撞时,不会进一步阅读并且碰撞将永远保持为真。

你应该做的是:

  • 有一些变量用于存储球的x,y和z坐标,但也存储vX,vY和vZ用于球速。
  • 每次检查碰撞(从if语句中移除碰撞值的读数)
  • 当发生碰撞时,你应该检查球击中了什么,并反转适当的速度变量
  • 如果你移动球取决于自上一帧以来经过的时间,可能会发生球在第一次弹跳后较少时间过去并且球移动小于前一帧所以球仍然与平面碰撞(表面,物体) ),再次弹跳并保持弹跳(卡住)。要解决这个问题,您可以:

    a)不仅仅是反转球速,而是根据击球的方向设定新的速度。即如果球击中左侧总是将vX设置为+某事,而不是仅将其反转。

    b)提前检查碰撞,检查下一帧(如果可能的话),这样球在进入水面之前就会被反弹。

    c)以某种方式提供恒定的球速,所以如果球进入表面并在下一帧反弹,那么它将在2帧之前,并且在表面之外。