/// <summary> /// 返回一个最极端的闵科夫斯基差,根据前形状,和传入的形状 /// </summary> /// <param name="other"></param> /// <param name="direction"></param> /// <returns></returns> private Vector3 MinkowskiDiffSupport(GJKCollider other, Vector3 direction) { //搜索direction方向最远的点 Vector3 my_support = support.Support(direction); Vector3 other_support = other.support.Support(-direction); //闵可夫斯基差 Vector3 result = my_support - other_support; return(result); }
/// <summary> /// 碰撞函数 /// </summary> /// <param name="other"></param> /// <returns></returns> public bool CollidesWithOther(GJKCollider other) { Vector3 newest_point; //任意方向,暂定vector3.right Vector3 direction = Vector3.right; Vector3 C = MinkowskiDiffSupport(other, direction); //如果该点和原点方向相反,直接返回false if (Vector3.Dot(C, direction) < 0) { return(false); } //取反 direction = -C; Vector3 B = MinkowskiDiffSupport(other, direction); //如果该点和原点方向相反,直接返回false if (Vector3.Dot(B, direction) < 0) { return(false); } //设置下一个方向为垂直于该线 BC X BO X BC direction = Cross_ABA(C - B, -B); //单形,已经包含两个点,B 和C List <Vector3> simplex = new List <Vector3> { B, C }; //在最大迭代次数范围内迭代 for (int i = 0; i < MAX_ITERATIONS; i++) { newest_point = MinkowskiDiffSupport(other, direction); //搜索方向的最远点仍然没有接近原点,false if (Vector3.Dot(newest_point, direction) < 0) { return(false); } //对单形状进行操作, //如果单形是包含原点的四面体,则返回true if (DoSimplex(newest_point, ref simplex, ref direction)) { return(true); } } //超过迭代次数,如果还没检测到,直接返回false return(false); }
public void OnMyCollisionEnter(GJKCollider other) { Debug.Log(other.gameObject.name + " enter"); }