flameforge2d

MouseJoint gets less and less reactive


Still new to flame and forge2d - both very impressive stuff.

I just learned that you need a MouseJoint to move a (Body)Component so you dont mess with the physics in Forge2d/Box2d.

From this example - MouseJointSample I see that it does work for dragging the body - but after multiple onDragUpdate and onDragEnd I find it to get less and less reactive to the mouse move. Hope the following gif animation can visualise this.

Every time the mouse stands still, I release the mouse/touch, and re-onDragUpdate/onDragEnd.

Do you see this and if so, what to do. This happens both on ios emulator and in Chrome web browser.

enter image description here

It looks like the mouseJoint is never really destroyed, and I can make it react to mouse/touch away from the BodyComponent:

Note: I just git cloned flame (ba61779)

enter image description here


Solution

  • I think I found the bug, the code should be:

      @override
      bool onDragUpdate(int pointerId, DragUpdateInfo details) {
        final mouseJointDef = MouseJointDef()
          ..maxForce = 3000 * ball.body.mass * 10
          ..dampingRatio = 0.1
          ..frequencyHz = 5
          ..target.setFrom(ball.body.position)
          ..collideConnected = false
          ..bodyA = groundBody
          ..bodyB = ball.body;
    
        if (mouseJoint == null) {
          mouseJoint = MouseJoint(mouseJointDef);
          world.createJoint(mouseJoint!);
        }
        mouseJoint?.setTarget(details.eventPosition.game);
        return false;
      }
    

    And not

      @override
      bool onDragUpdate(int pointerId, DragUpdateInfo details) {
        final mouseJointDef = MouseJointDef()
          ..maxForce = 3000 * ball.body.mass * 10
          ..dampingRatio = 0.1
          ..frequencyHz = 5
          ..target.setFrom(ball.body.position)
          ..collideConnected = false
          ..bodyA = groundBody
          ..bodyB = ball.body;
    
        mouseJoint ??= MouseJoint(mouseJointDef);
        world.createJoint(mouseJoint!);
    
        mouseJoint?.setTarget(details.eventPosition.game);
        return false;
      }