Search code examples
flutterdartbox2dgame-physicsflame

Box2D emulation on flutter painfully slow


I am re-writing a game a made using easlejs with Box2Dweb to flutter's flame engine with the dart port of box2d. My problem is that the objects are move really really slow. Gravity setting seems to progress in a linear fashion.

I read about scale factors etc... just don't know how to link it all. The World class does not have that, can someone show me an example of how to setup the initial screen to box2d world ratios? I get the screenSize using flames resize override I want to use that to set the scale or whatever works.

The examples in GitHub never seem to use that and even when I download then and run them... again painfully slow falling bodies.

A simple screen with a circle or a Square falling (correctly) will be appreciated. Here's how I instantiate the code. (I need the object to be 80x80 pixels)

class MyGame extends Game with TapDetector {
    MyGame() : _world = Box2D.World.withGravity(Box2D.Vector2(0, 10)) {
        _world.setAllowSleep(true);
        spawnBlocks();
    }

    void createSquare(int index, double w, double h, double x, double y) {
        int randomNumber = random.nextInt(letters.length);
        var bodyDef = Box2D.BodyDef();
        bodyDef.type = Box2D.BodyType.DYNAMIC; 
        bodyDef.position = Box2D.Vector2(x - 5, y); 
        bodyDef.angle = 0; 
        dynamicBody = _world.createBody(bodyDef); 
    
        dynamicBody.userData = letters[randomNumber];
        var boxShape = Box2D.PolygonShape();
        boxShape.setAsBox(w / 2, h / 2, Box2D.Vector2(w / 2, -h * 2), 0);

        var boxFixtureDef = Box2D.FixtureDef();
        boxFixtureDef.shape = boxShape;

        boxFixtureDef.density = 0;
        boxFixtureDef.restitution = 0;
        boxFixtureDef.friction = 1;
        dynamicBody.createFixtureFromFixtureDef(boxFixtureDef);

        blocks.add(dynamicBody);
  }

  spawnBlocks() {
      for (var i = 0; i < 8; i++) {
        createSquare(
          i, blockWidth, blockHeight, blockWidth * i + 18, -100 * i.toDouble());
      }
  }
}

It doesn't matter how high I set the gravity, the body still falls at the same speed. Even when they hit the floor they bounce very slowly, I have used the body.setTransform to increase the position.y etc but it just seems to move right through the static body(floor).


Solution

  • Since the density of your square is 0 it is not affected by gravity, try to set it to something higher and see if the objects are more affected.

    Don't use the body.setTransform if you don't really need to, since it will break the physics set up in the world.

    Did you try this example? https://github.com/flame-engine/flame/tree/master/doc/examples/box2d/contact_callbacks

    And don't forget to add the scale argument to your world, otherwise you'll hit the speed limits quite fast since you'll be so zoomed out.

    I'm the maintainer of box2d for flame (now Forge2D), if you have more questions you can join the box2d channel on our discord and I'll try to help you.