Project290.Physics.Collision.Shapes.CircleShape.RayCast C# (CSharp) Method

RayCast() public method

Cast a ray against a child shape.
public RayCast ( RayCastOutput &output, RayCastInput &input, Transform &transform, int childIndex ) : bool
output RayCastOutput The ray-cast results.
input RayCastInput The ray-cast input parameters.
transform Transform The transform to be applied to the shape.
childIndex int The child shape index.
return bool
        public override bool RayCast(out RayCastOutput output, ref RayCastInput input, ref Transform transform,
                                     int childIndex)
        {
            // Collision Detection in Interactive 3D Environments by Gino van den Bergen
            // From Section 3.1.2
            // x = s + a * r
            // norm(x) = radius

            output = new RayCastOutput();

            Vector2 position = transform.Position + MathUtils.Multiply(ref transform.R, Position);
            Vector2 s = input.Point1 - position;
            float b = Vector2.Dot(s, s) - Radius*Radius;

            // Solve quadratic equation.
            Vector2 r = input.Point2 - input.Point1;
            float c = Vector2.Dot(s, r);
            float rr = Vector2.Dot(r, r);
            float sigma = c*c - rr*b;

            // Check for negative discriminant and short segment.
            if (sigma < 0.0f || rr < Settings.Epsilon)
            {
                return false;
            }

            // Find the point of intersection of the line with the circle.
            float a = -(c + (float) Math.Sqrt(sigma));

            // Is the intersection point on the segment?
            if (0.0f <= a && a <= input.MaxFraction*rr)
            {
                a /= rr;
                output.Fraction = a;
                Vector2 norm = (s + a*r);
                norm.Normalize();
                output.Normal = norm;
                return true;
            }

            return false;
        }