Skip to content

Commit

Permalink
Updated Obstacle handling
Browse files Browse the repository at this point in the history
Added auto-subdivision option + automatically closing polylines
  • Loading branch information
Nebukam committed Sep 22, 2023
1 parent 41e64ab commit fdfe9bc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Runtime/Obstacle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Nebukam.ORCA
/// Obstacle definition within an ORCA simulation.
/// An Obstacle can be seen as an either open or closed polyline : it is a list of linked points in space
/// </summary>
public class Obstacle : VertexGroup<ObstacleVertex>, IRequireInit
public class Obstacle : PolyLine<ObstacleVertex>, IRequireInit
{

protected internal ORCALayer m_layerOccupation = ORCALayer.ANY;
Expand Down
34 changes: 24 additions & 10 deletions Runtime/ObstacleGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,50 +88,64 @@ public Obstacle Add(Obstacle obstacle)
/// <summary>
/// Add an obstacle to the group, in the form of a list of vertices.
/// </summary>
/// <param name="m_vertices">A list of vertices</param>
/// <param name="vertices">A list of vertices</param>
/// <param name="inverseOrder">Whether or not the vertices should be added in reverse order</param>
/// <param name="maxSegmentLength">If > 0.0f, will subdivide segments larger than this threshold.</param>
/// <returns>The newly created Obstacle</returns>
public Obstacle Add(IList<float3> m_vertices, bool inverseOrder = false)
public Obstacle Add(IList<float3> vertices, bool inverseOrder = false, float maxSegmentLength = 10.0f)
{
Obstacle obstacle = Pool.Rent<Obstacle>();

int count = m_vertices.Count;
int count = vertices.Count;
if (!inverseOrder)
{
for (int i = 0; i < count; i++)
obstacle.Add(m_vertices[i]);
obstacle.Add(vertices[i]);
}
else
{
for (int i = count - 1; i >= 0; i--)
obstacle.Add(m_vertices[i]);
obstacle.Add(vertices[i]);
}

if (math.distancesq(obstacle.vertices.Last().pos, obstacle.vertices.First().pos) != 0.0f)
obstacle.Add(obstacle.vertices.First().pos); // Close obstacle

if (maxSegmentLength > 0.0f)
obstacle.Subdivide(maxSegmentLength);

return Add(obstacle);
}

/// <summary>
/// Add an obstacle to the group, in the form of a list of vertices.
/// </summary>
/// <param name="m_vertices">An array of vertices</param>
/// <param name="vertices">An array of vertices</param>
/// <param name="inverseOrder">Whether or not the vertices should be added in reverse order</param>
/// <param name="maxSegmentLength">If > 0.0f, will subdivide segments larger than this threshold.</param>
/// <returns>The newly created Obstacle</returns>
public Obstacle Add(float3[] m_vertices, bool inverseOrder = false)
public Obstacle Add(float3[] vertices, bool inverseOrder = false, float maxSegmentLength = 10.0f)
{
Obstacle obstacle = Pool.Rent<Obstacle>();

int count = m_vertices.Length;
int count = vertices.Length;
if (!inverseOrder)
{
for (int i = 0; i < count; i++)
obstacle.Add(m_vertices[i]);
obstacle.Add(vertices[i]);
}
else
{
for (int i = count - 1; i >= 0; i--)
obstacle.Add(m_vertices[i]);
obstacle.Add(vertices[i]);
}

if (math.distancesq(obstacle.vertices.Last().pos, obstacle.vertices.First().pos) != 0.0f)
obstacle.Add(obstacle.vertices.First().pos); // Close obstacle

if (maxSegmentLength > 0.0f)
obstacle.Subdivide(maxSegmentLength);

return Add(obstacle);
}

Expand Down
37 changes: 30 additions & 7 deletions Samples~/Setup/ORCASetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ORCASetup : MonoBehaviour
public int agentCount = 50;
public float maxAgentRadius = 2f;
public float maxSpeed = 1f;
public float minSpeed = 1f;

[Header("Obstacles")]
public int obstacleCount = 100;
Expand Down Expand Up @@ -111,6 +112,25 @@ private void Start()

#endregion

#region create encompasing square boundary

float3[] squarePoints = new float3[] {
float3(min.x, min.y, 0f) * 1.2f,
float3(min.x, max.y, 0f)* 1.2f,
float3(max.x, max.y, 0f)* 1.2f,
float3(max.x, min.y, 0f)* 1.2f,
};

if (axis == AxisPair.XZ)
{
for (int i = 0; i < squarePoints.Length; i++)
squarePoints[i] = float3(squarePoints[i].x, 0f, squarePoints[i].y);
}

obstacles.Add(squarePoints, false, 10.0f);

#endregion

Random.InitState(seed + 10);

#region create dyanmic obstacles
Expand Down Expand Up @@ -158,7 +178,7 @@ private void Start()

for (int i = 0; i < agentCount; i++)
{
if(axis == AxisPair.XY)
if (axis == AxisPair.XY)
{
a = agents.Add((float3)transform.position + float3(Random.value, Random.value, 0f)) as IAgent;
}
Expand Down Expand Up @@ -188,7 +208,7 @@ private void Start()
else
{
r = raycasts.Add(float3(Random.Range(min.x, max.x), 0f, Random.Range(min.y, max.y))) as Raycast;
r.dir = normalize(float3(Random.Range(-1f, 1f),0f, Random.Range(-1f, 1f)));
r.dir = normalize(float3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f)));
}

r.distance = raycastDistance;
Expand Down Expand Up @@ -219,7 +239,7 @@ private void Update()

#if UNITY_EDITOR
//Agent body
if (axis == AxisPair.XY)
if (axis == AxisPair.XY)
{
Draw.Circle2D(agentPos, agent.radius, Color.green, 12);
Draw.Circle2D(agentPos, agent.radiusObst, Color.cyan.A(0.15f), 12);
Expand All @@ -236,8 +256,10 @@ private void Update()
Draw.Line(agentPos, agentPos + (normalize(agent.prefVelocity) * agent.radius), Color.grey);
#endif
//Update agent preferred velocity so it always tries to reach the "target" object
float agentSpeed = ((i + 1) * 0.5f);
agent.maxSpeed = agentSpeed;
float mspd = max(minSpeed + (i + 1) * 0.5f, maxSpeed);
float s = min(1f, distance(agent.pos, tr) / mspd);
float agentSpeed = mspd * s;
agent.maxSpeed = agentSpeed * s;
agent.prefVelocity = normalize(tr - agent.pos) * agentSpeed;

}
Expand All @@ -260,9 +282,10 @@ private void Update()
for (int j = 1, count = o.Count; j < count; j++)
{
Draw.Line(o[j - 1].pos, o[j].pos, staticObstacleColor);
Draw.Circle(o[j - 1].pos, 0.2f, Color.magenta, 6);
}
//Draw closing segment (simulation consider 2+ segments to be closed.)
if(!o.edge)
if (!o.edge)
Draw.Line(o[subCount - 1].pos, o[0].pos, staticObstacleColor);
}

Expand Down Expand Up @@ -292,7 +315,7 @@ private void Update()

Raycast r;
float rad = 0.2f;
for(int i = 0, count = raycasts.Count; i < count; i++)
for (int i = 0, count = raycasts.Count; i < count; i++)
{
r = raycasts[i] as Raycast;
Draw.Circle2D(r.pos, rad, Color.white, 3);
Expand Down
9 changes: 5 additions & 4 deletions Samples~/Setup/ORCASetup.unity
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ MonoBehaviour:
agentCount: 1000
maxAgentRadius: 2
maxSpeed: 1
obstacleCount: 25
minSpeed: 1
obstacleCount: 50
dynObstacleCount: 0
maxObstacleRadius: 5
maxObstacleRadius: 10
minObstacleEdgeCount: 3
maxObstacleEdgeCount: 12
min:
Expand All @@ -179,7 +180,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 311397813}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -17.8, y: 63.7, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down Expand Up @@ -353,7 +354,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1867022038}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 125.6, y: -98.4, z: 0}
m_LocalPosition: {x: 128.9, y: -129.9, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.nebukam.orca",
"version": "1.1.6",
"version": "1.1.7",
"displayName": "N:ORCA",
"description": "Optimal Reciprocal Collision Avoidance",
"unity": "2019.4",
Expand All @@ -27,7 +27,7 @@
"com.unity.mathematics": "1.2.1"
},
"gitDependencies": {
"com.nebukam.common": "https://github.com/Nebukam/com.nebukam.common.git#0.1.4",
"com.nebukam.common": "https://github.com/Nebukam/com.nebukam.common.git#0.1.5",
"com.nebukam.job-assist": "https://github.com/Nebukam/com.nebukam.job-assist.git#1.1.4"
},
"samples": [
Expand Down

0 comments on commit fdfe9bc

Please sign in to comment.