raycast method
bool
raycast(
- RayCastOutput output,
- RayCastInput input,
- Transform transform,
- int childIndex,
override
Cast a ray against a child shape.
output
is the ray-cast results.
input
the ray-cast input parameters.
transform
to be applied to the shape.
childIndex
the child shape index
Returns true if the child shape is hit.
Implementation
@override
bool raycast(
RayCastOutput output,
RayCastInput input,
Transform transform,
int childIndex,
) {
final inputP1 = input.p1;
final inputP2 = input.p2;
final tq = transform.q;
final tp = transform.p;
final positionX = tq.cos * position.x - tq.sin * position.y + tp.x;
final positionY = tq.sin * position.x + tq.cos * position.y + tp.y;
final sx = inputP1.x - positionX;
final sy = inputP1.y - positionY;
final b = sx * sx + sy * sy - radius * radius;
// Solve quadratic equation.
final rx = inputP2.x - inputP1.x;
final ry = inputP2.y - inputP1.y;
final c = sx * rx + sy * ry;
final rr = rx * rx + ry * ry;
final sigma = c * c - rr * b;
// Check for negative discriminant and short segment.
if (sigma < 0.0 || rr < settings.epsilon) {
return false;
}
// Find the point of intersection of the line with the circle.
var a = -(c + sqrt(sigma));
// Is the intersection point on the segment?
if (0.0 <= a && a <= input.maxFraction * rr) {
a /= rr;
output.fraction = a;
output.normal.x = rx * a + sx;
output.normal.y = ry * a + sy;
output.normal.normalize();
return true;
}
return false;
}