lyze237 / gdx-UnBox2D

A libGDX library to couple Unity's behaviour system and execution order with Box2D.

Home Page:https://lyze237.github.io/gdx-UnBox2D/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gdx-UnBox2D (Unity Box2D)

A libGDX library to couple Unity's behaviour system and execution order with Box2D.

License Jitpack Donate Donate

What is this whole thing about?

  • Unity's Game Objects and Behaviour system is a lot of fun to work with
  • This library tries to re-implement the execution order loop and this whole Game Object and Behaviour system (start, update, ...)
  • Everything is also coupled with Box2D:
    • Physics steps run at the correct time (fixedUpdate, ...)
    • All your Behaviours receive proper physics notifications (onCollisionEnter, ...)

Installation and Tutorial

Check the wiki for infos!

Example

public class CoolGame extends Game {
    private Viewport viewport;

    private SpriteBatch batch;

    private UnBox unBox;
    private Box2DDebugRenderer debugRenderer;

    @Override
    public void create() {
        viewport = new FitViewport(30, 10);
        viewport.getCamera().translate(-5, 0, 0);

        batch = new SpriteBatch();
        debugRenderer = new Box2DDebugRenderer();

        // Create an instance of the library, with no gravity
        unBox = new UnBox(); // Alternative if you want to use physics: new UnBox(new World(new Vector2(0, 0), true));

        // Create two game objects, those get automatically added to the libraries instance
        var rightGo = new GameObject(unBox);
        var leftGo = new GameObject(unBox);

        // Attach a Box2D body
        new Box2dBehaviour(BodyDefType.DynamicBody, rightGo);
        new Box2dBehaviour(BodyDefType.DynamicBody, leftGo);

        // Attach a logging behaviour to both of the game objects
        new SoutBehaviour("Right GO", false, rightGo);
        new SoutBehaviour("Left GO", false, leftGo);

        // Attach a movement behaviour to both game objects
        new MoveBehaviour(true, rightGo);
        new MoveBehaviour(false, leftGo);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(.25f, .25f, .25f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Step through physics and update loops
        unBox.preRender(Gdx.graphics.getDeltaTime());

        viewport.apply();
        batch.setProjectionMatrix(viewport.getCamera().combined);

        // Render the state
        batch.begin();
        unBox.render(batch);
        batch.end();

        // Debug render all box2d bodies (if you are using a physics world)
        //debugRenderer.render(unBox.getWorld(), viewport.getCamera().combined);

        // Clean up render loop
        unBox.postRender();
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height);
    }
}

About

A libGDX library to couple Unity's behaviour system and execution order with Box2D.

https://lyze237.github.io/gdx-UnBox2D/

License:Apache License 2.0


Languages

Language:Java 100.0%