A* Pathfinding Project
3.1.4
The A* Pathfinding Project for Unity 3D
|
Local Avoidance Simulator. More...
Classes | |
class | Worker |
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) |
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 | 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. | |
Properties | |
float | DeltaTime [get] |
float | DesiredDeltaTime [get, set] |
Time in seconds between each simulation step. | |
float | FrameDeltaTime [get] |
bool | Interpolation [get, set] |
Use Interpolation. | |
KDTree | KDTree [get] |
KDTree for this simulation. | |
bool | Multithreading [get] |
Is using multithreading. | |
float | PrevDeltaTime [get] |
Private Member Functions | |
~Simulator () | |
Terminates any worker threads. | |
void | CleanObstacles () |
void | ScheduleCleanObstacles () |
Private Attributes | |
List< Agent > | agents |
Agents in this simulation. | |
float | deltaTime |
float | desiredDeltaTime = 0.05f |
Inverse desired simulation fps. | |
bool | doCleanObstacles = false |
bool | doubleBuffering = true |
Use Double Buffering. | |
bool | doUpdateObstacles = false |
float | frameDeltaTime |
float[] | frameTimeBuffer = new float[5] |
int | frameTimeBufferIndex = 0 |
bool | interpolation = true |
Use Interpolation. | |
KDTree | kdTree |
KDTree for this simulation. | |
float | lastFrame = 0 |
float | lastStep = -99999 |
List< ObstacleVertex > | obstacles |
Obstacles in this simulation. | |
float | prevDeltaTime = 0 |
Worker[] | workers |
Worker threads. | |
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.
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. |
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 | ||
) |
Adds an obstacle described by the vertices.
ObstacleVertex AddObstacle | ( | Vector3 | a, |
Vector3 | b, | ||
float | height | ||
) |
Adds a line obstacle with a specified height.
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 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.
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 |
Inverse desired simulation fps.
|
private |
Use Double Buffering.
|
private |
Use Interpolation.
|
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.
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.