public static Vector3 GetRandomWorldPosistionInBoundaryClosest(GameObject element, GameObject container, GameObject target) { DatasetObjectInfo elementInfo = element.GetComponent <DatasetObjectInfo>(); DatasetObjectInfo containterInfo = container.GetComponent <DatasetObjectInfo>(); DatasetObjectInfo targetInfo = target.GetComponent <DatasetObjectInfo>(); var elementSize = elementInfo.GetBoundarySize(); var containerMin = containterInfo.GetMinBoundaryPoint(); var containerMax = containterInfo.GetMaxBoundaryPoint(); Vector3 transformedCenterOffset = element.transform.TransformDirection(elementInfo.center); Vector3 currentElementPosition = element.transform.position; float x = Random.Range(containerMin.x + elementSize.x / 2f - transformedCenterOffset.x, containerMax.x - elementSize.x / 2f - transformedCenterOffset.x); float y = Random.Range(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y, containerMax.y - elementSize.y / 2f - transformedCenterOffset.y); float z = Random.Range(containerMin.z + elementSize.z / 2f - transformedCenterOffset.z, containerMax.z - elementSize.z / 2f - transformedCenterOffset.z); if (-(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y) + (containerMax.y - elementSize.y / 2f - transformedCenterOffset.y) < 0) { y = containerMin.y + elementSize.y / 2f - transformedCenterOffset.y; } Vector3 endPoint = target.transform.TransformPoint(targetInfo.testPoints[Random.Range(0, targetInfo.testPoints.Count)]); float t = 0f; float p = 0f, k = 1f, s; for (int i = 0; i < 10; i++)//Bin search { s = (p + k) / 2f; element.transform.position = Vector3.Lerp(new Vector3(x, y, z), endPoint, s); if (!BoundaryIsColliding(element, target) && IsInside(element, container)) { p = s; t = s; } else { k = s; t = p; } } float offset = Settings.config.datasetOptions.closestMaxDistanceOffset; float dt = 1f / (new Vector3(x, y, z) - endPoint).magnitude * offset * Random.value; element.transform.position = Vector3.Lerp(new Vector3(x, y, z), endPoint, t - dt); if (!BoundaryIsColliding(element, target) && IsInside(element, container)) { element.transform.position = currentElementPosition; return(Vector3.Lerp(new Vector3(x, y, z), endPoint, t - dt)); } else { element.transform.position = currentElementPosition; return(Vector3.Lerp(new Vector3(x, y, z), endPoint, t)); } }
public static Vector3 GetRandomWorldPosistionInBoundary(GameObject element, GameObject container) { DatasetObjectInfo elementInfo = element.GetComponent <DatasetObjectInfo>(); DatasetObjectInfo containterInfo = container.GetComponent <DatasetObjectInfo>(); var elementSize = elementInfo.GetBoundarySize(); var containerMin = containterInfo.GetMinBoundaryPoint(); var containerMax = containterInfo.GetMaxBoundaryPoint(); Vector3 transformedCenterOffset = element.transform.TransformDirection(elementInfo.center); float x = Random.Range(containerMin.x + elementSize.x / 2f - transformedCenterOffset.x, containerMax.x - elementSize.x / 2f - transformedCenterOffset.x); float y = Random.Range(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y, containerMax.y - elementSize.y / 2f - transformedCenterOffset.y); float z = Random.Range(containerMin.z + elementSize.z / 2f - transformedCenterOffset.z, containerMax.z - elementSize.z / 2f - transformedCenterOffset.z); if (-(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y) + (containerMax.y - elementSize.y / 2f - transformedCenterOffset.y) < 0) { y = containerMin.y + elementSize.y / 2f - transformedCenterOffset.y; } return(new Vector3(x, y, z)); }
public static Vector3 GetRandomWorldPosistionInBoundary(GameObject element, GameObject container, GameObject target) { DatasetObjectInfo elementInfo = element.GetComponent <DatasetObjectInfo>(); DatasetObjectInfo containterInfo = container.GetComponent <DatasetObjectInfo>(); var elementSize = elementInfo.GetBoundarySize(); var containerMin = containterInfo.GetMinBoundaryPoint(); var containerMax = containterInfo.GetMaxBoundaryPoint(); Vector3 transformedCenterOffset = element.transform.TransformDirection(elementInfo.center); List <Vector3> positions = new List <Vector3>(10); List <float> distancesToElement = new List <float>(positions.Count); for (int i = 0; i < positions.Capacity; i++) { float x = Random.Range(containerMin.x + elementSize.x / 2f - transformedCenterOffset.x, containerMax.x - elementSize.x / 2f - transformedCenterOffset.x); float y = Random.Range(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y, containerMax.y - elementSize.y / 2f - transformedCenterOffset.y); float z = Random.Range(containerMin.z + elementSize.z / 2f - transformedCenterOffset.z, containerMax.z - elementSize.z / 2f - transformedCenterOffset.z); if (-(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y) + (containerMax.y - elementSize.y / 2f - transformedCenterOffset.y) < 0) { y = containerMin.y + elementSize.y / 2f - transformedCenterOffset.y; } positions.Add(new Vector3(x, y, z)); distancesToElement.Add((target.transform.position - positions[positions.Count - 1]).sqrMagnitude); } int index = -1; float smallestDistance = Mathf.Infinity; for (int i = 0; i < distancesToElement.Count; i++) { if (distancesToElement[i] < smallestDistance) { index = i; smallestDistance = distancesToElement[i]; } } return(positions[index]); }
public void PlaceRobotInStartZone() { var startZones = GameObject.FindGameObjectsWithTag("StartZone"); if (startZones.Length > 0) { System.Random r = new System.Random(); int i = r.Next(0, startZones.Length); var startZone = startZones[i]; DatasetObjectInfo elementInfo = robot.GetComponent <DatasetObjectInfo>(); var elementSize = elementInfo.GetBoundarySize(); var bounds = startZone.GetComponent <Collider>().bounds; var containerMin = bounds.min; var containerMax = bounds.max; Vector3 transformedCenterOffset = robot.transform.TransformDirection(elementInfo.center); float x = Random.Range(containerMin.x + elementSize.x / 2f - transformedCenterOffset.x, containerMax.x - elementSize.x / 2f - transformedCenterOffset.x); float y = Random.Range(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y, containerMax.y - elementSize.y / 2f - transformedCenterOffset.y); float z = Random.Range(containerMin.z + elementSize.z / 2f - transformedCenterOffset.z, containerMax.z - elementSize.z / 2f - transformedCenterOffset.z); if (-(containerMin.y + elementSize.y / 2f - transformedCenterOffset.y) + (containerMax.y - elementSize.y / 2f - transformedCenterOffset.y) < 0) { y = containerMin.y + elementSize.y / 2f - transformedCenterOffset.y; } robot.transform.position = new Vector3(x, 1, z); float direction = startZone.GetComponent <StartZoneController>().angle; float fov = startZone.GetComponent <StartZoneController>().fov; robot.transform.rotation = Quaternion.Euler(0, Random.Range(direction - fov / 2, direction + fov / 2), 0); } foreach (var cp in GameObject.FindGameObjectsWithTag("Checkpoint")) { cp.GetComponent <CheckpointController>().reached = false; } robotController.rb.velocity = Vector3.zero; robotController.rb.angularVelocity = Vector3.zero; }
public static bool IsInside(GameObject element, GameObject container) { DatasetObjectInfo elementInfo = element.GetComponent <DatasetObjectInfo>(); DatasetObjectInfo containterInfo = container.GetComponent <DatasetObjectInfo>(); var elementSize = elementInfo.GetBoundarySize(); var containerMin = containterInfo.GetMinBoundaryPoint(); var containerMax = containterInfo.GetMaxBoundaryPoint(); Vector3 transformedCenterOffset = element.transform.TransformDirection(elementInfo.center); Vector3 currentElementPosition = element.transform.position; float x = element.transform.position.x; float y = element.transform.position.y; float z = element.transform.position.z; float minX = containerMin.x + elementSize.x / 2f - transformedCenterOffset.x; float maxX = containerMax.x - elementSize.x / 2f - transformedCenterOffset.x; float minY = containerMin.y + elementSize.y / 2f - transformedCenterOffset.y; float maxY = containerMax.y - elementSize.y / 2f - transformedCenterOffset.y; float minZ = containerMin.z + elementSize.z / 2f - transformedCenterOffset.z; float maxZ = containerMax.z - elementSize.z / 2f - transformedCenterOffset.z; return(x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ); }