// untested // determine sector by world coordinates, returning 401 means out of perimeter, 404 generic error private int DetermineSector(GameObject plot) { Vector3 plotBearing3D = plot.gameObject.transform.position - perimeter.center; float sqrToPlot = Vector3.SqrMagnitude(plotBearing3D); if (sqrToPlot > (perimeter.Sectors[12].distanceRange.y * perimeter.Sectors[12].distanceRange.y)) { return(404); } // if the dbogie*dbogie is farther than an outer sector farther distance*distance, return 404, i.e out of perimeter float headingToPlot = NTools.HeadingFromVector(new Vector2(plotBearing3D.x, plotBearing3D.y)); float bearing = NTools.GetBearingFromHeadings(perimeter.headingFloat, headingToPlot); if (bearing > 360 || bearing < 0) { return(404); } // Sanity check // 13 different sectors exist, loop through all, store the one where it is for (int i = 0; i <= 12; i++) { if (sqrToPlot < Mathf.Pow(perimeter.Sectors[i].headingRange.y, 2f) && sqrToPlot > Mathf.Pow(perimeter.Sectors[i].headingRange.x, 2f)) { if (perimeter.Sectors[i].headingRange.x > perimeter.Sectors[i].headingRange.y) { if (bearing < perimeter.Sectors[i].headingRange.x && bearing > perimeter.Sectors[i].headingRange.y) { return(i); // i represents the sector index from the sectors array } } else if (perimeter.Sectors[i].headingRange.x < perimeter.Sectors[i].headingRange.y) { if (bearing < perimeter.Sectors[i].headingRange.x || bearing > perimeter.Sectors[i].headingRange.y) { return(i); // i represents the sector index from the sectors array } } } } Debug.Log("Determine sector: 404"); return(404); }
// Used to determine for example in which direction the enemy lies and how widespread they are. // works in CCW mode public static NTools.Slice AnalyseGroupDisposition(Vector3 originPos, float originHeading, List <GameObject> objects) { NTools.Slice slice = new NTools.Slice(originPos, originHeading, 0, 0, 0, 0, new Vector3(0, 0, 0)); // sanity check, no objects means return of 000000 !!! if (objects.Count == 0) { return(slice); } float tempBear, leftBear = 0, rightBear = 0; // statistical analysis floats: float meanbearing = 0f, sumDiffFromMean = 0f; float[] bearings = new float[objects.Count]; slice.centerOfMass = NTools.GetCenterOfObjects3D(objects); for (int i = 0; i < objects.Count; i++) { // left bearing is positive, right bearing is negative, clamped between -180 to 180 tempBear = Mathf.Clamp( NTools.GetBearingFromHeadings( originHeading, NTools.HeadingFromVector(objects[i].transform.position - originPos) ), -180f, 180f); // storing for later analysis bearings[i] = tempBear; // with the first heading set both left and right bearings if (i == 0) { leftBear = tempBear; rightBear = tempBear; } else { if (tempBear > leftBear) { leftBear = tempBear; } else if (tempBear < rightBear) { rightBear = tempBear; } } } // disperison computation for (int i = 0; i < bearings.Length; i++) { meanbearing += bearings[i]; } meanbearing = meanbearing / bearings.Length; for (int i = 0; i < bearings.Length; i++) { sumDiffFromMean += bearings[i] - meanbearing; } // Filling the slice !!! slice.leftmostBearing = leftBear; slice.rightmostBearing = rightBear; slice.bearingRange = leftBear - rightBear; slice.stdDeviation = Mathf.Sqrt(Mathf.Pow(sumDiffFromMean, 2f) / (bearings.Length - 1)); return(slice); }