computeDistanceToOut method

  1. @override
double computeDistanceToOut(
  1. Transform xf,
  2. Vector2 p,
  3. int childIndex,
  4. Vector2 normalOut,
)
override

Compute the distance from the current shape to the specified point. This only works for convex shapes.

xf is the shape world transform. p is a point in world coordinates. normalOut returns the direction in which the distance increases. Returns the distance from the current shape.

Implementation

@override
double computeDistanceToOut(
  Transform xf,
  Vector2 p,
  int childIndex,
  Vector2 normalOut,
) {
  final xfq = xf.q;
  final centerX = xfq.cos * p.x - xfq.sin * p.y + xf.p.x;
  final centerY = xfq.sin * p.x + xfq.cos * p.y + xf.p.y;
  final dx = p.x - centerX;
  final dy = p.y - centerY;
  final d1 = sqrt(dx * dx + dy * dy);
  normalOut.x = dx * 1 / d1;
  normalOut.y = dy * 1 / d1;
  return d1 - radius;
}