A* Pathfinding Project
3.8.5
The A* Pathfinding Project for Unity 3D
|
Local Avoidance Simulator. More...
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.
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.
Classes | |
class | Worker |
class | WorkerContext |
Public Types | |
enum | SamplingAlgorithm { AdaptiveSampling, GradientDescent } |
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< Agent > | GetAgents () |
Get a list of all agents. | |
List< ObstacleVertex > | GetObstacles () |
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< ObstacleVertex > | obstacles |
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< Agent > | agents |
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. | |
enum SamplingAlgorithm |
Simulator | ( | int | workers, |
bool | doubleBuffering | ||
) |
Create a new simulator.
workers | Use 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. |
doubleBuffering | Use 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. |
|
private |
Terminates any worker threads.
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.
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.
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.
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.
|
private |
|
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.
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.
void OnDestroy | ( | ) |
void RemoveAgent | ( | IAgent | agent | ) |
Removes a specified agent from this simulation.
The agent can be added again later by using AddAgent.
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.
|
private |
void Update | ( | ) |
Should be called once per frame.
void UpdateObstacle | ( | ObstacleVertex | obstacle, |
Vector3[] | vertices, | ||
Matrix4x4 | matrix | ||
) |
Updates the vertices of an obstacle.
obstacle | Obstacle to update |
vertices | New vertices for the obstacle, must have at least the number of vertices in the original obstacle |
matrix | Matrix 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.
|
private |
Agents in this simulation.
SamplingAlgorithm algorithm = SamplingAlgorithm.AdaptiveSampling |
What sampling algorithm to use.
|
private |
|
private |
|
private |
Inverse desired simulation fps.
|
private |
|
private |
Use Double Buffering.
|
private |
|
private |
Use Interpolation.
|
private |
KDTree for this simulation.
|
private |
|
private |
List<ObstacleVertex> obstacles |
Obstacles in this simulation.
|
private |
|
private |
|
private |
float qualityCutoff = 0.05f |
float stepScale = 1.5f |
|
private |
|
get |
|
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.
|
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.
|
get |
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.
|
get |
Is using multithreading.
|
getset |
|
get |
|
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.
|
getset |