private MinDistInfo FindClosestPoint(Vector3[] points, Vector3 position) { float minDist = 999.0f; int id = 0; // the target that our user has approached int i = 0; foreach (Vector3 corner in points) { // is now inside vs. is before inside // I think we should use physical pos here float distance = Vector3.Distance(FlattenedDir3D(corner), FlattenedDir3D(position)); if (distance < minDist) { minDist = distance; id = i; } ++i; } MinDistInfo info = new MinDistInfo(); info.minDist = minDist; //info.min = turningPoints[id]; info.id = id; return(info); }
// detect if user is close to the corner // returns 0 if our user is out of corner // returns 1 if our user is fully inside the corner private float Transit() { // first check if user is in corner if (!isInTransit) { MinDistInfo info = FindClosestPoint(turningPoints, head.localPosition); float minDist = info.minDist; int id = info.id; //min = info.min; if (minDist <= CORNER_ENTER && isBeforeInside == false) { // outside in GoFromOutsideIn(); } else if (minDist >= CORNER_LEAVE && isBeforeInside == true) { // inside out GoFromInsideOut(); // re-calculate radius CalcCurvatureRadius(); turningCenterRelativePos = CalcTurningCenterRelativePos(id); // change to next target currentTarget = GetNextTarget(id); targetText.text = currentTarget.ToString(); } // gather all points as a vector Vector3[] virtTargets = TransformToPos(corners); // Approaching next virtual target MinDistInfo vinfo = FindClosestPoint(virtTargets, head.position); if (vinfo.minDist <= VCORNER_BOUND) { currentVirtTarget = GetNextVirtualTarget(vinfo.id); } } // rate based on timer return(TransitRate()); }