// can be used to find the nearest obstacle being tracked public static bool nearestObstacle(Dictionary <int, VRAVEObstacle> obs, out VRAVEObstacle nearest) { nearest = new VRAVEObstacle(); if (obs.Count == 0) { return(false); } float min = Mathf.Infinity; foreach (KeyValuePair <int, VRAVEObstacle> o in obs) { if (o.Value.Distance < min) { min = o.Value.Distance; nearest = o.Value; } } return(true); }
public bool Scan(out Dictionary <int, VRAVEObstacle> obstacles) { obstacles = new Dictionary <int, VRAVEObstacle> (); if (m_shortRangeSensorsEnable) { RaycastHit shortSensorsHit; Vector3 shortRangeSensorsStart = transform.position; shortRangeSensorsStart.y += shortRangeSensorsHeight; InitShortRangeSensors(); // Scan for nearby obstacles foreach (VRAVESensor scan in shortRangeSensorsArray) { Debug.DrawRay(shortRangeSensorsStart, scan.Direction * m_shortSensorLength, Color.green); if (Physics.Raycast(shortRangeSensorsStart, scan.Direction, out shortSensorsHit, m_shortSensorLength, layerMask)) { if (shortSensorsHit.collider.CompareTag(VRAVEStrings.Obstacle) || shortSensorsHit.collider.CompareTag(VRAVEStrings.AI_Car) || shortSensorsHit.collider.CompareTag(VRAVEStrings.Crazy_AI_Car)) { if (!obstacles.ContainsKey(scan.ID)) { Debug.DrawLine(shortRangeSensorsStart, shortSensorsHit.point, Color.yellow); VRAVEObstacle vo = new VRAVEObstacle(); vo.obstacle = shortSensorsHit; vo.obstacleTag = shortSensorsHit.collider.tag; vo.Distance = shortSensorsHit.distance; obstacles.Add(scan.ID, vo); } } } } } if (m_longRangeSensorsEnable) { RaycastHit longSensorsHit; Vector3 longRangeSensorsStart = transform.position; longRangeSensorsStart.y += longRangeSensorsHeight; // long sensors InitLongRangeSensors(); for (uint i = 0; i < numLongRangeSensors; ++i) { Vector3 sensorStart = longRangeSensorsStart; if (i == 1) { sensorStart += transform.right * m_lateralSensorShift; } else if (i == 2) { sensorStart -= transform.right * m_lateralSensorShift; } Debug.DrawRay(sensorStart, longRangeSensorsArray [i].Direction * m_longSensorLength, Color.red); if (Physics.Raycast(sensorStart, longRangeSensorsArray [i].Direction, out longSensorsHit, m_longSensorLength, layerMask)) { if (longSensorsHit.collider.CompareTag(VRAVEStrings.Obstacle) || longSensorsHit.collider.CompareTag(VRAVEStrings.AI_Car)) { if (!obstacles.ContainsKey(longRangeSensorsArray[i].ID)) { Debug.DrawLine(sensorStart, longSensorsHit.point, Color.yellow); VRAVEObstacle vo = new VRAVEObstacle(); vo.obstacle = longSensorsHit; vo.obstacleTag = longSensorsHit.collider.tag; vo.Distance = (longSensorsHit.point - transform.position).magnitude; obstacles.Add(longRangeSensorsArray[i].ID, vo); } } } } } return(obstacles.Count == 0 ? false : true); }
public bool Scan (out Dictionary<int, VRAVEObstacle> obstacles) { obstacles = new Dictionary<int, VRAVEObstacle> (); if (m_shortRangeSensorsEnable) { RaycastHit shortSensorsHit; Vector3 shortRangeSensorsStart = transform.position; shortRangeSensorsStart.y += shortRangeSensorsHeight; InitShortRangeSensors (); // Scan for nearby obstacles foreach (VRAVESensor scan in shortRangeSensorsArray) { Debug.DrawRay (shortRangeSensorsStart, scan.Direction * m_shortSensorLength, Color.green); if (Physics.Raycast (shortRangeSensorsStart, scan.Direction, out shortSensorsHit, m_shortSensorLength, layerMask)) { if (shortSensorsHit.collider.CompareTag (VRAVEStrings.Obstacle) || shortSensorsHit.collider.CompareTag (VRAVEStrings.AI_Car) || shortSensorsHit.collider.CompareTag (VRAVEStrings.Crazy_AI_Car)) { if (!obstacles.ContainsKey(scan.ID)) { Debug.DrawLine (shortRangeSensorsStart, shortSensorsHit.point, Color.yellow); VRAVEObstacle vo = new VRAVEObstacle (); vo.obstacle = shortSensorsHit; vo.obstacleTag = shortSensorsHit.collider.tag; vo.Distance = shortSensorsHit.distance; obstacles.Add (scan.ID, vo); } } } } } if (m_longRangeSensorsEnable) { RaycastHit longSensorsHit; Vector3 longRangeSensorsStart = transform.position; longRangeSensorsStart.y += longRangeSensorsHeight; // long sensors InitLongRangeSensors (); for (uint i = 0; i < numLongRangeSensors; ++i) { Vector3 sensorStart = longRangeSensorsStart; if (i == 1) { sensorStart += transform.right * m_lateralSensorShift; } else if (i == 2) { sensorStart -= transform.right * m_lateralSensorShift; } Debug.DrawRay (sensorStart, longRangeSensorsArray [i].Direction * m_longSensorLength, Color.red); if (Physics.Raycast (sensorStart, longRangeSensorsArray [i].Direction, out longSensorsHit, m_longSensorLength, layerMask)) { if (longSensorsHit.collider.CompareTag (VRAVEStrings.Obstacle) || longSensorsHit.collider.CompareTag (VRAVEStrings.AI_Car)) { if (!obstacles.ContainsKey (longRangeSensorsArray[i].ID)) { Debug.DrawLine (sensorStart, longSensorsHit.point, Color.yellow); VRAVEObstacle vo = new VRAVEObstacle (); vo.obstacle = longSensorsHit; vo.obstacleTag = longSensorsHit.collider.tag; vo.Distance = (longSensorsHit.point - transform.position).magnitude; obstacles.Add (longRangeSensorsArray[i].ID, vo); } } } } } return obstacles.Count == 0 ? false : true; }
// can be used to find the nearest obstacle being tracked public static bool nearestObstacle(Dictionary<int, VRAVEObstacle> obs, out VRAVEObstacle nearest) { nearest = new VRAVEObstacle (); if (obs.Count == 0) return false; float min = Mathf.Infinity; foreach (KeyValuePair<int, VRAVEObstacle> o in obs) { if (o.Value.Distance < min) { min = o.Value.Distance; nearest = o.Value; } } return true; }