简体   繁体   中英

How to combine Scene2d and Bullet?

I want to display some animation using bullet on top of a scene2d stage. No interaction with the bullet part is required, its supposed to be only an animation.

If required I can also add the SimpleSimulator (so the file has still around 200 lines, even after removing the stuff I don't need for this example).

The call to SimpleSimulator#create basically sets up the world, environment and the objects. (The whole bullet thing is based on https://github.com/xoppa/blog/blob/master/tutorials/src/com/xoppa/blog/libgdx/g3d/bullet/dynamics/step6/BulletTest.java )

public class SimpleExample implements ApplicationListener {

    private class ExampleStage extends Stage {

        public ExampleStage() {

            Image background = new Image(new Texture(Gdx.files.internal("background.png")));
            addActor(background);

        }
    }

    private Camera cam;
    private Stage stage;
    private SimpleSimulator simulator;

    @Override
    public void create() {

        Bullet.init();

        cam = new OrthographicCamera(30, 30);
        cam.lookAt(0f, 1f, 0f);
        cam.near = 0f;
        cam.far = 500f;
        cam.update();

        this.stage = new ExampleStage();

        simulator = new SimpleSimulator();
        simulator.create();

    }

    @Override
    public void render() {

        stage.draw();
        stage.act();

        //simulator.simulate();

        // if added only the bullet part will be animated,
        // if not only scene2d
        // Gdx.gl.glClearColor(0f, 0f, 0f, 1.f);
        // Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

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

    public void resize(int width, int height) {}
    public void pause() {}
    public void resume() {}

    @Override
    public void dispose() {
        stage.dispose();
        simulator.dispose();
    }
}

I might just miss a simple flag in the render method. If I call the Gdx.gl.glClear method's I only manage to see the one or the other.

I also would like to restrict the bullet animation part to a specific area on the screen, so other elements I want to have on the stage are not obstructed by it, but I have not figured out how to do that yet.

Changing the order in the render method as suggested by Tenfour04 did the trick.
I just update this here to mark this question as answered.

    @Override
    public void render() {

        Gdx.input.setInputProcessor(stage);

        simulator.simulate();

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

        stage.act();
        stage.draw();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM