Skip to content

Commit

Permalink
Merge pull request #355 from tier4/fix/spawn-distance-to-ego
Browse files Browse the repository at this point in the history
fix: do not spawn NPC Vehicle when is closer than X to EGO
  • Loading branch information
mackierx111 authored Sep 4, 2024
2 parents 97af062 + cedd13f commit a5397cb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Assets/AWSIM/Scenes/Main/AutowareSimulation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ MonoBehaviour:
HistoryPolicy: 1
Depth: 1
publishHz: 100
--- !u!1 &30319277 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 4981081891045692864, guid: 164645b2d105b6b4ab4e3d2e7c65dab2, type: 3}
m_PrefabInstance: {fileID: 2465439770829660650}
m_PrefabAsset: {fileID: 0}
--- !u!1 &51962876
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2364,6 +2369,10 @@ PrefabInstance:
propertyPath: duration
value: 25
objectReference: {fileID: 0}
- target: {fileID: 445656315769820269, guid: 58d73df60b244d146bdf5f5896f78355, type: 3}
propertyPath: _egoVehicle
value:
objectReference: {fileID: 30319277}
- target: {fileID: 445656315769820269, guid: 58d73df60b244d146bdf5f5896f78355, type: 3}
propertyPath: maxVehicleCount
value: 10
Expand Down Expand Up @@ -4454,7 +4463,7 @@ MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 4981081891045692866, guid: 164645b2d105b6b4ab4e3d2e7c65dab2, type: 3}
m_PrefabInstance: {fileID: 2465439770829660650}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_GameObject: {fileID: 30319277}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 80ebb0e40b62ca443ba1d8ccf61fcbda, type: 3}
Expand Down Expand Up @@ -8215,8 +8224,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 1925267639}
m_HandleRect: {fileID: 1925267638}
m_Direction: 2
m_Value: 0
m_Size: 1
m_Value: 1
m_Size: 0.8368603
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down
10 changes: 10 additions & 0 deletions Assets/AWSIM/Scripts/RandomTraffic/TrafficManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using GeometryUtility = AWSIM.Lanelet.GeometryUtility;

namespace AWSIM.TrafficSimulation
{
Expand All @@ -24,6 +25,9 @@ public class TrafficManager : MonoBehaviour
[SerializeField, Tooltip("A maximum number of vehicles that can simultaneously live in the scene. Lowering this value results in less dense traffic but improves the simulator's performance.")]
public int maxVehicleCount = 40;

[SerializeField, Tooltip("A minimal distance between the EGO and the NPC to spawn")]
private float spawnDistanceToEgo = 50.0f;

[SerializeField, Tooltip("Ego vehicle handler. If not set, the manager creates a dummy ego. This reference is also set automatically when the Ego spawns via the traffic simulator.")]
private GameObject _egoVehicle;

Expand Down Expand Up @@ -226,6 +230,12 @@ private void FixedUpdate()
{
NPCVehicle spawnedVehicle;

var distance2D = GeometryUtility.Distance2D(_egoVehicle.transform.position, spawnLoc.Key.Position);
if (distance2D < spawnDistanceToEgo)
{
continue;
}

if (spawnLoc.Value.Count == 1)
{
var tsimAndPrefab = spawnLoc.Value.First();
Expand Down
3 changes: 3 additions & 0 deletions docs/Components/Traffic/TrafficComponents/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ The `Vehicle Layer Mask` and `Ground Layer Mask` are used to make sure all vehic
`Max Vehicle Count` specifies how many `NPCVehicles` can be present on the scene at once.
When the number of `NPCVehicles` on the scene is equal to this value the [`RandomTrafficSimulator`](#randomtrafficsimulator) stops spawning new vehicles until some existing vehicles drive away and disappear.

`Spawn Distance to EGO` defines the minimal distance between the EGO and the NPC to spawn

The `EgoVehicle` field provides the information about Ego vehicle used for correct behavior of`NPCVehicles`when interacting with Ego.

`Show Gizmos` checkbox specifies whether the [Gizmos](#gizmos) visualization should be displayed when running the simulation.
Expand Down Expand Up @@ -173,6 +175,7 @@ Set to `0` to disable this restriction.
| ----------------------- | ----------------------------------------------------------------------------------------------------------- |
| **General Settings** | |
| Seed | Seed value for random generator |
| Spawn Distance to EGO | Minimal distance between the EGO and the NPC to spawn |
| Ego Vehicle | Transform of ego vehicle |
| Vehicle Layer Mask | LayerMask that masks only vehicle(NPC and ego) colliders |
| Ground Layer Mask | LayerMask that masks only ground colliders of the map |
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a5397cb

Please sign in to comment.