/// <summary> /// GJK算法的多边形碰撞检测 /// </summary> /// <param name="a">形状a</param> /// <param name="b">形状b</param> /// <returns>是否碰撞</returns> public static bool GJKCheck(ShapBase a, ShapBase b) { Fixed2 direction = a.position - b.position; Simplex s = new Simplex(); s.Push(ShapBase.Support(a, b, direction)); direction = -direction; while (true) //迭代 { s.Push(ShapBase.Support(a, b, direction)); if (s.GetA().Dot(direction) < 0) { return(false); } else { if (s.ContainsOrigin()) { return(true); } else { direction = s.GetDirection(); } } } }
/// <summary> /// 给定方向 给定两个凸体 该函数返回这两个凸体明可夫斯基差形状中的一个点 /// </summary> /// <param name="a">形状a</param> /// <param name="b">形状b</param> /// <param name="direction">迭代方向</param> /// <returns>指定方向内的一点</returns> public static Fixed2 Support(ShapBase a, ShapBase b, Fixed2 direction) { Fixed2 p1 = a.Support(direction); Fixed2 p2 = b.Support(-direction); //Debug.Log("Support{ p1:" + p1 + "p2:" + p2 + "p3:" + (p1 - p2)); return(p1 - p2); }