/// <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); }
public static void Draw(ShapBase shap, Color color, float time = 0.1f) { for (int i = 0; i < shap.PointsCount - 1; i++) { UnityEngine.Debug.DrawLine((shap.position + shap.GetPoint(i)).ToVector3(), (shap.position + shap.GetPoint(i + 1)).ToVector3(), color, time); } UnityEngine.Debug.DrawLine((shap.position + shap.GetPoint(shap.PointsCount - 1)).ToVector3(), (shap.position + shap.GetPoint(0)).ToVector3(), color, time); }
public List <Tree4> GetInTrees(ShapBase shap) { List <Tree4> treeList = new List <Tree4>(); foreach (var t in trees) { treeList.AddRange(t.GetInTress(shap)); } return(treeList); }
public void SetShap(Vector2[] points) { var ps = new Fixed2[points.Length]; for (int i = 0; i < ps.Length; i++) { ps[i] = points[i].ToFixed2(); } Shap = new ShapBase(ps); }
public static bool BoxCheck(ShapBase objA, ShapBase objB) { if (FixedNumber.Abs((objA.position.x - objB.position.x)) < (objA.width + objB.width) / 2 && FixedNumber.Abs((objA.position.y - objB.position.y)) < (objA.height + objB.height) / 2 ) { return(true); } return(false); }
public bool IsIn(ShapBase shap) { if (((border.center.x - shap.position.x).Abs() <= (border.size + shap.width / 2)) && ((border.center.y - shap.position.y).Abs() <= (border.size + shap.height / 2)) ) { return(true); } return(false); }
public List <Tree4> GetInTress(ShapBase shap) { List <Tree4> treeList = new List <Tree4>(); if (child == null) { if (IsIn(shap)) { treeList.Add(this); } } else { treeList.AddRange(child.GetInTrees(shap)); } return(treeList); }
public List <NetData> CheckShap(ShapBase shap) { List <NetData> objs = new List <NetData>(); var treeList = GetInTress(shap); foreach (var tree in treeList) { for (int i = 0; i < tree.objs.Count; i++) { if (ShapPhysics.Check(shap, tree.objs[i].Shap)) { objs.Add(tree.objs[i]); } } } return(objs); }
public List <NetData> CheckShap(ShapBase shap) { List <NetData> objs = new List <NetData>(); var treeList = GetInTress(shap); foreach (var tree in treeList) { for (int i = 0; i < tree.objs.Count; i++) { if (!tree.objs[i].isTrigger && ShapPhysics.Check(shap, tree.objs[i].Shap)) { //当物体处于四叉树交界处 可能会添加多次 if (!objs.Contains(tree.objs[i])) { objs.Add(tree.objs[i]); } } } } return(objs); }
public List <NetData> OverlapShap(ShapBase shap) { return(tree.CheckShap(shap)); }
public List <NetData> OverlapShap(ShapBase shap, Fixed2 position) { shap.position = position; return(OverlapShap(shap)); }
//GJK算法原理 //两个物体进行明可夫斯基差操作 得到的新图形形状包含原点则这两个图形的是相交的 /// <summary> /// 立即检测两个物体是否发生细节碰撞 /// 先包围盒检测(粗略) 后GJK碰撞检测(细节) /// </summary> /// <param name="a">检测对象a</param> /// <param name="b">检测对象b</param> /// <returns>是否碰撞</returns> public static bool Check(ShapBase a, ShapBase b) { return(BoxCheck(a, b) && GJKCheck(a, b));//;// xB&&yB; }
public List <NetData> OverlapShap(ShapBase shap) { ShapDebug.Draw(shap, UnityEngine.Color.red); return(tree.CheckShap(shap)); }