/// <summary> /// Collides two shapes using GJK. /// </summary> /// <typeparam name="T1">The type of the first shape.</typeparam> /// <typeparam name="T2">The type of the second shape.</typeparam> /// <param name="shape1">The first shape.</param> /// <param name="shape2">The second shape.</param> /// <param name="support1">The GJK support function for first shape.</param> /// <param name="support2">The GJK support function for the second shape.</param> /// <returns>A result indicating if the shapes are disjoint or intersect.</returns> /// <remarks> /// Collide uses the following algorithm, if delgates are proving slow you can copy the below and call /// the support functions direct. /// <code> /// GJK gjk = new GJK(); /// Double3 a = support1(shape1, gjk,Direction); /// Double3 b = support2(shape2, -gjk.Direction); /// GJKResult result; /// while ((result = gjk.AddSupportPoint(a - b)) == GJKResult.Unsolved) /// { /// a = support1(shape1, gjk.Direction); /// b = support2(shape2, -gjk.Direction); /// } /// return result; /// </code> /// </remarks> public static GJKResult Collide <T1, T2>(T1 shape1, T2 shape2, Func <T1, Vector3d, Vector3d> support1, Func <T2, Vector3d, Vector3d> support2) { GJK gjk = new GJK(); var a = support1(shape1, gjk.Direction); var b = support2(shape2, -gjk.Direction); GJKResult result; while ((result = gjk.AddSupportPoint(a - b)) == GJKResult.Unsolved) { a = support1(shape1, gjk.Direction); b = support2(shape2, -gjk.Direction); } return(result); }
/// <summary> /// Collides two shapes using GJK taking a maximum of stepLimit steps. /// </summary> /// <typeparam name="T1">The type of the first shape.</typeparam> /// <typeparam name="T2">The type of the second shape.</typeparam> /// <param name="shape1">The first shape.</param> /// <param name="shape2">The second shape.</param> /// <param name="support1">The GJK support function for first shape.</param> /// <param name="support2">The GJK support function for the second shape.</param> /// <param name="stepLimit">The GJK support function for the second shape.</param> /// <returns>A result indicating if the shapes are disjoint or intersect.</returns> /// <remarks> /// Collide uses the following algorithm, if delgates are proving slow you can copy the below and call /// the support functions direct. /// <code> /// GJK gjk = new GJK(); /// Double3 a = support1(shape1, gjk,Direction); /// Double3 b = support2(shape2, -gjk.Direction); /// GJKResult result; /// while (--stepLimit > 0 & (result = gjk.AddSupportPoint(a - b)) == GJKResult.Unsolved) /// { /// a = support1(shape1, gjk.Direction); /// b = support2(shape2, -gjk.Direction); /// } /// return result; /// </code> /// </remarks> public static GJKResult Collide <T1, T2>(T1 shape1, T2 shape2, Func <T1, Vector3d, Vector3d> support1, Func <T2, Vector3d, Vector3d> support2, int stepLimit) { GJK gjk = new GJK(); var a = support1(shape1, gjk.Direction); var b = support2(shape2, -gjk.Direction); GJKResult result; //& NOT &&, both sides should always be evaluated, no shortcircuiting. while (--stepLimit > 0 & (result = gjk.AddSupportPoint(a - b)) == GJKResult.Unsolved) { a = support1(shape1, gjk.Direction); b = support2(shape2, -gjk.Direction); } return(result); }