public GameObject GetNearestPlane(Transform nearObj, float r)
    {
        string d = "Seeking planes: ";

        if (Mathf.Abs(lastNearestPlaneTime - Time.time) > nearestPlaneSeekInterval)
        {
//			UnityEngine.XR.iOS.UnityARMatrixOps.GetPosition
            GameObject nearest = null;
            lastNearestPlaneTime = Time.time;
            float nearDist = Mathf.Infinity;
            int   i        = 0;
            foreach (PlaneInfo pi in FindObjectsOfType <PlaneInfo>())
            {
                float curDist = Vector3.Magnitude(pi.gameObject.transform.position - nearObj.position);
                d += i + ": " + curDist + ",";
                if (curDist < nearDist && curDist < r)
                {
                    curDist = nearDist;
                    nearest = pi.gameObject;
                    d      += "nearest!..";
                }
                i++;
            }
            cachedPlane = nearest;
            DebugText.SeekPlanes(d);
            return(nearest);
        }
        else
        {
            DebugText.SeekPlanes(d + "cached plane");
            return(cachedPlane);
        }
    }
Esempio n. 2
0
    public bool FoundTargetNearOnion(MetalOnion mo)
    {
        if (targetFeatureClusterTimer < 0)
        {
            foundAnyQuadrant = false;
            if (lastOnionWhoRequested != mo)
            {
                searchRadius = 8f;
            }
            lastOnionWhoRequested     = mo;
            targetFeatureClusterTimer = targetFeatureClusterInterval;
//			InitQuadrantScores ();
            QuadrantScores.Clear();
            // Let's make 27 quadrants relative to onion's world position
            // Check each quadrant for total green features
            // The quadrant with the most green features is where the onion moves.

            // iterate through all Green map points
            Vector3[] greenPoints = CC.featuresVisualizer.CurrentGreenPoints;
            DebugText.SeekPlanes("greenpts:" + greenPoints.Length);
            foreach (Vector3 gp in greenPoints)
            {
                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        for (int k = -1; k < 2; k++)
                        {
                            // For each point compare its distance to one of 27 quadrants surrounding the onion
                            // Closer quadrants are scored more highly
                            // Result should be that the quadrant with the most green dots gets the highest score.
                            float   distFromThisQuadrantToGreenPoint = Vector3.SqrMagnitude(mo.transform.position + new Vector3(i, j, k) * searchRadius - gp);
                            Vector3 key = new Vector3(i, j, k);
                            float   val = Mathf.Min(1, 1 / distFromThisQuadrantToGreenPoint);
//							Debug.Log ("val for " + i + "," + j + "," + k + ": " + val);
                            if (QuadrantScores.ContainsKey(key))
                            {
                                QuadrantScores [key] += val;                                 // never more than 1
                            }
                            else
                            {
                                QuadrantScores.Add(key, val);
                            }
                        }
                    }
                }
            }
//
//			foreach (Vector3 k in QuadrantScores.Keys) {
//				GameObject d = GameObject.CreatePrimitive (PrimitiveType.Sphere);
//				d.transform.position = k;
//				d.transform.localScale = .002f * QuadrantScores [k] * Vector3.one;
//				d.transform.position = mo.transform.position + k * searchRadius;
//				d.name = "debSphere";
////				d.GetComponent<Renderer>().material.color = new Color(
//			}


//			float max = 0;
            Vector3 bestQuadrant = GetBestQuadrant();

            if (foundAnyQuadrant)
            {
//				Debug.Log ("set cached target;" + mo.transform.position + " plus <color=green>" + bestQuadrant + "</color>");
                cachedTarget = mo.transform.position + bestQuadrant;
                // If the best quadrant is zero, shrink the radius
                if (bestQuadrant == Vector3.zero)
                {
                    searchRadius /= 2f;
//					Debug.Log("<color=blue>Decereased by half. New radius:"+searchRadius+"</color>");
                }
            }
            else
            {
                Debug.Log("Found no quads");
            }
//			Debug.Log("<color=blue>Cached :"+cachedTarget+"</color>");
        }
        return(foundAnyQuadrant);
    }