Box2D emulation on flutter painfully slow

599

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.

Share:
599
Saabir Mohamed
Author by

Saabir Mohamed

Updated on November 25, 2022

Comments

  • Saabir Mohamed
    Saabir Mohamed over 1 year

    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).

  • Saabir Mohamed
    Saabir Mohamed over 3 years
    Thanks for your feedback, Even if I set the density higher this does not affect the falling speed ... I fiddled with these type of settings to experiment, also I know that transform causes issues as per the docs , but I set it to see if the frame rate was slow or the box2d calculations , I notice in the repo it makes use of "Viewport" , I will look into this and thanks again for your answer...can I send you my code ..its a work in progress.
  • Saabir Mohamed
    Saabir Mohamed over 3 years
    Just a quick update .... the link you provided helped me a lot ... exactly what I was looking for I was not using scale at all..when starting that code in a new project adjusting the gravity etc has the desired effect. I marked the solution as resolved. I joined the discord as well excited to interact.