예제 #1
0
    //public void SetDirectionX(string s) {
    //    float x;
    //    float.TryParse(s, out x);
    //    direction = new Vector3(x, direction.y, direction.z);
    //}

    //public void SetDirectionY(string s) {
    //    float y;
    //    float.TryParse(s, out y);
    //    direction = new Vector3(direction.x, y, direction.z);
    //}

    //public void SetDirectionZ(string s) {
    //    float z;
    //    float.TryParse(s, out z);
    //    direction = new Vector3(direction.x, direction.y, z);
    //}

    public static bool Intersects(IConvexRegion regionOne, Transform oneTrans, IConvexRegion regionTwo, Transform twoTrans, GJKState state) {
        // Get an initial point on the Minkowski difference.
        Vector3 s = Support(regionOne, regionTwo, twoTrans.position - oneTrans.position, state, out sa);


        // Create our initial simplex.
        Simplex simplex = new Simplex();
        simplex.Add(s);

        // TODO: Choose an initial direction.
        direction = -s;

        state.simplices.Add(simplex.Clone());

        // Choose a maximim number of iterations to avoid an 
        // infinite loop during a non-convergent search.
        int maxIterations = 32;

        for (int i = 0; i < maxIterations; i++) {
            // Get our next simplex point toward the origin.
            Vector3 a = Support(regionOne, regionTwo, direction, state, out sa, out sb);

            // If we move toward the origin and didn't pass it 
            // then we never will and there's no intersection.
            if (a.IsInOppositeDirection(direction)) {
                return false;
            }

            // otherwise we add the new point to the simplex and process it.
            simplex.Add(a);
            state.simplices.Add(simplex.Clone());
            // Here we either find a collision or we find the closest feature of
            // the simplex to the origin, make that the new simplex and update the direction
            // to move toward the origin from that feature.
            if (ProcessSimplex(simplex, ref direction)) {
                float tolerance = 0.000001f; // Or another such small number
                    while (true) {
                    Edge e = simplex.FindClosestEdge();
                    Vector3 p = Support(regionOne, regionTwo, e.normal, state, out sa, out sb);
                    float proj = Vector3.Dot(p, e.normal);

                    if (proj - e.distance < tolerance) {
                    }
}
                    else {
                    }
                }
                return true;
            }