Search code examples
javalibgdxbox2d

Libgdx: How to access/modify Box2D World from another input processing class?


I'm working on a small game and I'm trying to keep things as neat as possible. I've split up the inputprocessor into another class and it works fine. I've setup a playState class as well which handles and creates bodies and fixtures for Box2D.

And in my input processing class:

public class TouchProcessor implements InputProcessor  {

    //.....

    @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            System.out.println("Dragging...");
            touchHappening = true;

            return true;
        }

    // ....

Now on touch drag, I want to reposition a body/camera from the Box2D world. But I don't have access to those in my Processor class if I separate it from the playState. How do I go about this?


Solution

  • Every time when you decouple your application parts (which is a good idea btw) you need a strategy how the different parts can work together.

    An solution often seen in the wild is to use some kind of central singleton to make important game methods accessible from other code. But singletons have their own flaws. Another way would be to use some kind of event system here.

    You may want to have a look at these decoupling patterns to get some ideas about a good game architecture.