예제 #1
0
 public void TestCloseMiss()
 {
     Assert.IsFalse(
         Line3Triangle3Collider.FindContacts(
             new Vector3(0.0f, 0.5f, 0.0f), Vector3.Right,
             Vector3.Zero, Vector3.UnitX, Vector3.UnitX + Vector3.UnitY
             ).HasContact
         );
 }
예제 #2
0
        public void TestHitThroughCenter()
        {
            LineContacts contacts = Line3Triangle3Collider.FindContacts(
                new Vector3(-0.25f, -0.5f, -1.0f), Vector3.Normalize(Vector3.One),
                Vector3.Zero, Vector3.UnitX, Vector3.UnitX + Vector3.UnitY
                );
            float contactTime = 1.0f / Vector3.Normalize(Vector3.One).Z;

            Assert.That(
                contacts.EntryTime,
                Is.EqualTo(contactTime).Within(Specifications.MaximumDeviation).Ulps
                );
            Assert.That(
                contacts.ExitTime,
                Is.EqualTo(contactTime).Within(Specifications.MaximumDeviation).Ulps
                );
        }
        /// <summary>Determines the contact location between a ray and a triangle</summary>
        /// <param name="rayStart">
        ///   Offset of the ray from the coordinate system's center
        /// </param>
        /// <param name="rayDirection">Direction of the line</param>
        /// <param name="triangleA">
        ///   First corner point of triangle in counter-clockwise order
        /// </param>
        /// <param name="triangleB">
        ///   Second corner point of triangle in counter-clockwise order
        /// </param>
        /// <param name="triangleC">
        ///   Third corner point of triangle in counter-clockwise order
        /// </param>
        /// <returns>The point of intersection of the line with the triangle, if any</returns>
        /// <remarks>
        ///   <para>
        ///     I saw this algorithm in an article to line/triangle intersections tests
        ///     by Christopher Bartlett. The material was stated to be free for learning
        ///     purposes, so I felt free to apply what I've learned here =)
        ///   </para>
        ///   <para>
        ///     There is no special case for when the line precisely touches one of
        ///     the triangle's corners. It will either enter and exit the triangle or
        ///     no contacts will be detected at all.
        ///   </para>
        /// </remarks>
        internal static LineContacts FindContacts(
            Vector3 rayStart, Vector3 rayDirection,
            Vector3 triangleA, Vector3 triangleB, Vector3 triangleC
            )
        {
            LineContacts contacts = Line3Triangle3Collider.FindContacts(
                rayStart, rayDirection, triangleA, triangleB, triangleC
                );

            // If the line has entered the triangle before the reference point, this means
            // that the ray starts within the triangle and its first contact occurs immediately
            if (!float.IsNaN(contacts.EntryTime))
            {
                if (contacts.EntryTime < 0.0f)
                {
                    return(LineContacts.None);
                }
            }

            return(contacts);
        }