flutterdart2d-gamesflameforge2d

Why does the body move much slower in a larger world?


When I tested my Flutter Flame Forge2d game on a PC instead of a phone, I found out that the larger screen size, and therefore world size, caused a body component to move much slower than expected.

MyGame() : super(zoom: 10, gravity: Vector2.zero());

I was initially debugging the world size and camera zoom, thinking that the camera was just zoomed out farther away, but apparently the issue was actally from the density of the body component, as the component was also larger than before. The size of my body component is controlled by 2 lines of code, where the size of the body adjusts based on size of the world in order to keep the object relatively the same size as the world.

FlutterView view = WidgetsBinding.instance.platformDispatcher.views.first;
Size sizeDP = view.physicalSize / view.devicePixelRatio;

double widthDP = sizeDP.width;
double heightDP = sizeDP.height;

Vector2 kScreenSize = Vector2(widthDP, heightDP);
Vector2 kWorldSize = Vector2(widthDP / 10, heightDP / 10);
kHalfWidth: kWorldSize.x / 51,
kHalfHeight: kWorldSize.x / 51,

However, the increased size caused all linear impluses to move the object less than normal. Conversely decreasing the screen size would decrease the world size, which would then make the object travel faster. Same thing with increasing or decreasing the density.

kDensity: 1.5,
kFriction: 0.5,
kRestitution: 0.02,

This is the code to handle the objects linear velocity, where Vector2 value ranges from +-1 in both x and y.

  // all constants are positive 
  @override
  void firstJoystickMovement(Vector2 value, Body body, dynamic constants) {
    body.applyLinearImpulse(value * constants.kTranslationalAccelerationRate);

    if (kDebugMode) {
      print(body.linearVelocity.length);
    }
    body.linearVelocity.clampLength(0, constants.kMaxTranslationalSpeed);
    if (body.linearVelocity.length >
        value.length * constants.kTranslationalAccelerationRate) {
      body.linearDamping = constants.kTranslationalDeccelerationRate;
    } else {
      body.linearDamping = constants.kTranslationalIdleDeccelerationRate;
    }
  }

Currently, I don't know whether the issue is stemming from

Any help or improvements to the original code and the problem is appreciated and explanations of how forge2d treats coordinates are also appreciated.


Solution

  • You are hitting the speed limit of Box2D/Forge2D, that's why it needs to be zoomed out. This is not only Forge2D specific, but applies to pretty much all ports of box2d and box2d itself. You can read more about it here.

    Your body sized should be static and not dependent on the screen size, use for example the FixedResolutionViewport to make sure your game is scaled properly instead.