/// <summary>Determines where a ray will hit a triangle, if at all</summary> /// <param name="rayStart">Starting point of the ray</param> /// <param name="rayDirection">Direction into which the ray extends</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 intersection points between the ray and the box, if any</returns> public static LineContacts FindContacts( Vector2 rayStart, Vector2 rayDirection, Vector2 triangleA, Vector2 triangleB, Vector2 triangleC ) { LineContacts contacts = Line2Triangle2Collider.FindContacts( rayStart, rayDirection, triangleA, triangleB, triangleC ); // If the line has entered the box before the reference point, this means // that the ray starts within the box, thus, its first contact occurs immediately if (!float.IsNaN(contacts.EntryTime)) { if (contacts.ExitTime < 0.0f) // Entry & exit before the ray's beginning? { return(LineContacts.None); } else if (contacts.EntryTime < 0.0f) // Only entry before ray's beginning? { contacts.EntryTime = 0.0f; } } return(contacts); }
public void TestCloseMiss() { Assert.IsFalse( Line2Triangle2Collider.FindContacts( new Vector2(-0.1f, 0.0f), Vector2.Normalize(Vector2.One), Vector2.Zero, Vector2.UnitX, Vector2.One ).HasContact ); }
public void TestHitTopToBottom() { LineContacts contacts = Line2Triangle2Collider.FindContacts( new Vector2(0.5f, 1.0f), -Vector2.UnitY, Vector2.UnitX, Vector2.One, Vector2.Zero ); Assert.That( contacts.EntryTime, Is.EqualTo(0.5f).Within(Specifications.MaximumDeviation).Ulps ); Assert.That( contacts.ExitTime, Is.EqualTo(1.0f).Within(Specifications.MaximumDeviation).Ulps ); }
public void TestHitBottomToRight() { LineContacts contacts = Line2Triangle2Collider.FindContacts( new Vector2(0.0f, -0.5f), Vector2.Normalize(Vector2.One), Vector2.UnitX, Vector2.One, Vector2.Zero ); float entryTime = 0.5f / Vector2.Normalize(Vector2.One).X; float exitTime = 1.0f / Vector2.Normalize(Vector2.One).Y; Assert.That( contacts.EntryTime, Is.EqualTo(entryTime).Within(Specifications.MaximumDeviation).Ulps ); Assert.That( contacts.ExitTime, Is.EqualTo(exitTime).Within(Specifications.MaximumDeviation).Ulps ); }