physics_debug.dart 2.51 KB
Newer Older
1 2 3 4 5
part of flutter_sprites;

class _PhysicsDebugDraw extends box2d.DebugDraw {
  _PhysicsDebugDraw(
    box2d.ViewportTransform transform,
6
    this.physicsWorld
7 8 9 10 11 12 13
  ) : super(transform) {
    appendFlags(
      box2d.DebugDraw.JOINT_BIT |
      box2d.DebugDraw.CENTER_OF_MASS_BIT |
      box2d.DebugDraw.WIREFRAME_DRAWING_BIT
    );
  }
14

15
  PhysicsWorld physicsWorld;
16

Adam Barth's avatar
Adam Barth committed
17
  Canvas canvas;
18 19

  void drawSegment(Vector2 p1, Vector2 p2, box2d.Color3i color) {
20 21 22
    Paint paint = new Paint()
      ..color = _toColor(color)
      ..strokeWidth = 1.0;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    canvas.drawLine(_toPoint(p1), _toPoint(p2), paint);
  }

  void drawSolidPolygon(
    List<Vector2> vertices,
    int vertexCount,
    box2d.Color3i color
  ) {
    Path path = new Path();
    Point pt = _toPoint(vertices[0]);
    path.moveTo(pt.x, pt.y);
    for (int i = 1; i < vertexCount; i++) {
      pt = _toPoint(vertices[i]);
      path.lineTo(pt.x, pt.y);
    }

    Paint paint = new Paint()..color = _toColor(color);
    canvas.drawPath(path, paint);
  }

  void drawCircle(Vector2 center, num radius, box2d.Color3i color, [Vector2 axis]) {
    Paint paint = new Paint()
      ..color = _toColor(color)
46
      ..style = ui.PaintingStyle.stroke
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      ..strokeWidth = 1.0;

    canvas.drawCircle(_toPoint(center), _scale(radius), paint);
  }

  void drawSolidCircle(Vector2 center, num radius, Vector2 axis, box2d.Color3i color) {
    Paint paint = new Paint()
      ..color = _toColor(color);

    canvas.drawCircle(_toPoint(center), _scale(radius), paint);
  }

  void drawPoint(Vector2 point, num radiusOnScreen, box2d.Color3i color) {
    drawSolidCircle(point, radiusOnScreen, null, color);
  }

  void drawParticles(
    List<Vector2> centers,
    double radius,
    List<box2d.ParticleColor> colors,
    int count
  ) {
    // TODO: Implement
  }

  void drawParticlesWireframe(
    List<Vector2> centers,
    double radius,
    List<box2d.ParticleColor> colors,
    int count
  ) {
    // TODO: Implement
  }

  void drawTransform(box2d.Transform xf, box2d.Color3i color) {
    drawCircle(xf.p, 0.1, color);
    // TODO: Improve
  }

  void drawStringXY(num x, num y, String s, box2d.Color3i color) {
    // TODO: Implement
  }

  Color _toColor(box2d.Color3i color) {
    return new Color.fromARGB(255, color.x, color.y, color.z);
  }

  Point _toPoint(Vector2 vec) {
    return new Point(
96 97
      vec.x * physicsWorld.b2WorldToNodeConversionFactor,
      vec.y * physicsWorld.b2WorldToNodeConversionFactor
98 99 100 101
    );
  }

  double _scale(double value) {
102
    return value * physicsWorld.b2WorldToNodeConversionFactor;
103 104
  }
}