flutterdartbox2dflameforge2d

How to render DistanceJoint


I need to render a DistanceJoint, just a line connecting the 2 anchors, nothing fancy. I've seen something about a render method here but I coulen't figure out how to use it. I also don't mind using debug features. Thank you very much :)


Solution

  • Since you have tagged the question with Flame I'm guessing that you are using flame_forge2d?

    The link that you provide is for pure forge2d web, but you should be able to do it in a similar fashion in flame_forge2d. I would try something like this in your Forge2DGame:

      final _bodyARenderPosition = Vector2.zero();
      final _bodyBRenderPosition = Vector2.zero();
    
      @override
      void render(Canvas c) {
        super.render(c);
        c.save();
        c.scale(camera.zoom);
        for (final joint in world.joints) {
          _bodyARenderPosition
            ..setFrom(joint.bodyA.position)
            ..y *= -1;
          _bodyBRenderPosition
            ..setFrom(joint.bodyB.position)
            ..y *= -1;
          c.drawLine(
            _bodyARenderPosition.toOffset(),
            _bodyBRenderPosition.toOffset(),
            debugPaint,
          );
        }
        c.restore();
      }