A* Pathfinding Project  3.6
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
Simulator Class Reference

Local Avoidance Simulator. More...

Detailed Description

Local Avoidance Simulator.

This class handles local avoidance simulation for a number of agents using Reciprocal Velocity Obstacles (RVO) and Optimal Reciprocal Collision Avoidance (ORCA).

This class will handle calculation of velocities from desired velocities supplied by a script. It is, however, not responsible for moving any objects in a Unity Scene. For that there are other scripts (see below).

Obstacles can be added and removed from the simulation, agents can also be added and removed at any time.

See Also
RVOSimulator RVOAgent Pathfinding.RVO.IAgent

The implementation is based on the RVO2 Library (http://gamma.cs.unc.edu/RVO2/) extended with many new features.

You will most likely mostly use the wrapper class RVOSimulator.

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here

Classes

class  Worker
 
class  WorkerContext
 

Public Types

enum  SamplingAlgorithm { AdaptiveSampling, GradientDecent }
 

Public Member Functions

 Simulator (int workers, bool doubleBuffering)
 Create a new simulator.
 
IAgent AddAgent (IAgent agent)
 Add a previously removed agent to the simulation.
 
IAgent AddAgent (Vector3 position)
 Add an agent at the specified position.
 
ObstacleVertex AddObstacle (ObstacleVertex v)
 Adds a previously removed obstacle.
 
ObstacleVertex AddObstacle (Vector3[] vertices, float height)
 Adds an obstacle described by the vertices.
 
ObstacleVertex AddObstacle (Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer=RVOLayer.DefaultObstacle)
 Adds an obstacle described by the vertices.
 
ObstacleVertex AddObstacle (Vector3 a, Vector3 b, float height)
 Adds a line obstacle with a specified height.
 
void ClearAgents ()
 Removes all agents from the simulation.
 
List< AgentGetAgents ()
 Get a list of all agents.
 
List< ObstacleVertexGetObstacles ()
 Get a list of all obstacles.
 
void OnDestroy ()
 
void RemoveAgent (IAgent agent)
 Removes a specified agent from this simulation.
 
void RemoveObstacle (ObstacleVertex v)
 Removes the obstacle identified by the vertex.
 
void Update ()
 Should be called once per frame.
 
void UpdateObstacle (ObstacleVertex obstacle, Vector3[] vertices, Matrix4x4 matrix)
 Updates the vertices of an obstacle.
 
void UpdateObstacles ()
 Rebuilds the obstacle tree at next simulation frame.
 

Public Attributes

SamplingAlgorithm algorithm = SamplingAlgorithm.AdaptiveSampling
 What sampling algorithm to use.
 
List< ObstacleVertexobstacles
 Obstacles in this simulation.
 
float qualityCutoff = 0.05f
 
float stepScale = 1.5f
 

Properties

float DeltaTime [get]
 
float DesiredDeltaTime [get, set]
 Time in seconds between each simulation step.
 
bool Interpolation [get, set]
 Use Interpolation.
 
KDTree KDTree [get]
 KDTree for this simulation.
 
bool Multithreading [get]
 Is using multithreading.
 
bool Oversampling [get, set]
 
float PrevDeltaTime [get]
 
RVOQuadtree Quadtree [get]
 Quadtree for this simulation.
 
float WallThickness [get, set]
 

Private Member Functions

 ~Simulator ()
 Terminates any worker threads.
 
void BuildQuadtree ()
 
void CleanObstacles ()
 
void ScheduleCleanObstacles ()
 

Private Attributes

List< Agentagents
 Agents in this simulation.
 
WorkerContext coroutineWorkerContext = new WorkerContext()
 
float deltaTime
 
float desiredDeltaTime = 0.05f
 Inverse desired simulation fps.
 
bool doCleanObstacles = false
 
bool doubleBuffering = true
 Use Double Buffering.
 
bool doUpdateObstacles = false
 
bool interpolation = true
 Use Interpolation.
 
KDTree kdTree
 KDTree for this simulation.
 
float lastStep = -99999
 
float lastStepInterpolationReference = -9999
 
bool oversampling = false
 
float prevDeltaTime = 0
 
RVOQuadtree quadtree = new RVOQuadtree()
 
float wallThickness = 1
 
Worker[] workers
 Worker threads.
 

Member Enumeration Documentation

Enumerator:
AdaptiveSampling 
GradientDecent 

Constructor & Destructor Documentation

Simulator ( int  workers,
bool  doubleBuffering 
)

Create a new simulator.

Parameters
workersUse the specified number of worker threads.
When the number zero is specified, no multithreading will be used. A good number is the number of cores on the machine.
doubleBufferingUse Double Buffering for calculations. Testing done with 5000 agents and 0.1 desired delta time showed that with double buffering enabled the game ran at 50 fps for most frames, dropping to 10 fps during calculation frames. But without double buffering it ran at around 10 fps all the time.
This will let threads calculate while the game progresses instead of waiting for the calculations to finish.
Note
Will only have effect if using multithreading
See Also
Multithreading
~Simulator ( )
private

Terminates any worker threads.

Member Function Documentation

IAgent AddAgent ( IAgent  agent)

Add a previously removed agent to the simulation.

An agent can only be in one simulation at a time, any attempt to add an agent to two simulations or multiple times to the same simulation will result in an exception being thrown.

See Also
RemoveAgent
IAgent AddAgent ( Vector3  position)

Add an agent at the specified position.

You can use the returned interface to read several parameters such as position and velocity and set for example radius and desired velocity.

See Also
RemoveAgent
ObstacleVertex AddObstacle ( ObstacleVertex  v)

Adds a previously removed obstacle.

This does not check if the obstacle is already added to the simulation, so please do not add an obstacle multiple times.

It is assumed that this is a valid obstacle.

ObstacleVertex AddObstacle ( Vector3[]  vertices,
float  height 
)

Adds an obstacle described by the vertices.

See Also
RemoveObstacle
ObstacleVertex AddObstacle ( Vector3[]  vertices,
float  height,
Matrix4x4  matrix,
RVOLayer  layer = RVOLayer.DefaultObstacle 
)

Adds an obstacle described by the vertices.

See Also
RemoveObstacle
ObstacleVertex AddObstacle ( Vector3  a,
Vector3  b,
float  height 
)

Adds a line obstacle with a specified height.

See Also
RemoveObstacle
void BuildQuadtree ( )
private
void CleanObstacles ( )
private
void ClearAgents ( )

Removes all agents from the simulation.

List<Agent> GetAgents ( )

Get a list of all agents.

This is an internal list. I'm not going to be restrictive so you may access it since it is better for performance but please do not modify it since that can cause errors in the simulation.

Warning
Do not modify this list!
List<ObstacleVertex> GetObstacles ( )

Get a list of all obstacles.

This is a list of obstacle vertices. Each vertex is part of a doubly linked list loop forming an obstacle polygon.

Warning
Do not modify this list!
See Also
AddObstacle
RemoveObstacle
void OnDestroy ( )
void RemoveAgent ( IAgent  agent)

Removes a specified agent from this simulation.

The agent can be added again later by using AddAgent.

See Also
AddAgent(IAgent)
ClearAgents
void RemoveObstacle ( ObstacleVertex  v)

Removes the obstacle identified by the vertex.

This must be the same vertex as the one returned by the AddObstacle call.

See Also
AddObstacle
void ScheduleCleanObstacles ( )
private
void Update ( )

Should be called once per frame.

void UpdateObstacle ( ObstacleVertex  obstacle,
Vector3[]  vertices,
Matrix4x4  matrix 
)

Updates the vertices of an obstacle.

Parameters
obstacleObstacle to update
verticesNew vertices for the obstacle, must have at least the number of vertices in the original obstacle
matrixMatrix to multiply vertices with before updating obstacle

The number of vertices in an obstacle cannot be changed, existing vertices can only be moved.

void UpdateObstacles ( )

Rebuilds the obstacle tree at next simulation frame.

Add and remove obstacle functions call this automatically.

Member Data Documentation

List<Agent> agents
private

Agents in this simulation.

SamplingAlgorithm algorithm = SamplingAlgorithm.AdaptiveSampling
WorkerContext coroutineWorkerContext = new WorkerContext()
private
float deltaTime
private
float desiredDeltaTime = 0.05f
private

Inverse desired simulation fps.

See Also
DesiredDeltaTime
bool doCleanObstacles = false
private
bool doubleBuffering = true
private

Use Double Buffering.

See Also
DoubleBuffering
bool doUpdateObstacles = false
private
bool interpolation = true
private

Use Interpolation.

See Also
Interpolation
KDTree kdTree
private

KDTree for this simulation.

float lastStep = -99999
private
float lastStepInterpolationReference = -9999
private
List<ObstacleVertex> obstacles

Obstacles in this simulation.

bool oversampling = false
private
float prevDeltaTime = 0
private
RVOQuadtree quadtree = new RVOQuadtree()
private
float qualityCutoff = 0.05f
float stepScale = 1.5f
float wallThickness = 1
private
Worker [] workers
private

Worker threads.

Property Documentation

float DeltaTime
get
float DesiredDeltaTime
getset

Time in seconds between each simulation step.

This is the desired delta time, the simulation will never run at a higher fps than the rate at which the Update function is called.

bool Interpolation
getset

Use Interpolation.

If interpolation is enabled, agent positions will be interpolated on frames when no rvo calculation is done. This has a very small overhead, but usually yields much smoother looking movement.

KDTree for this simulation.

Used internally by the simulation. Please only read from this tree, do not rebuild it since that can interfere with the simulation. It is rebuilt when needed.

bool Multithreading
get

Is using multithreading.

bool Oversampling
getset
float PrevDeltaTime
get
RVOQuadtree Quadtree
get

Quadtree for this simulation.

Used internally by the simulation to perform fast neighbour lookups for each agent. Please only read from this tree, do not rebuild it since that can interfere with the simulation. It is rebuilt when needed.

float WallThickness
getset

The documentation for this class was generated from the following file: