Class Simulator
Public
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 uses a sampling based algorithm with gradient descent to find the avoidance velocities.
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
Inner Types
Public Methods
)
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.
)
Add an agent at the specified position.
You can use the returned interface to read and write parameters and set for example radius and desired point to move to.
)
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.
Vector3 [] | vertices | |
float | height | |
bool | cycle=true | |
)
Adds an obstacle described by the vertices.
)
Adds a line obstacle with a specified height.
)
Adds an obstacle described by the vertices.
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!
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!
void
OnDestroy
()
Terminates all worker threads.
Warning
You must call this when you are done with the simulator, otherwise some threads can linger and lead to memory leaks.
void
RemoveAgent
(
)
Removes a specified agent from this simulation.
The agent can be added again later by using AddAgent.
void
RemoveObstacle
(
)
Removes the obstacle identified by the vertex.
This must be the same vertex as the one returned by the AddObstacle call.
Simulator
(
int | 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. |
bool | 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. |
MovementPlane | movementPlane | The plane that the movement happens in. XZ for 3D games, XY for 2D games. |
)
Create a new simulator.
Note
Will only have effect if using multithreading
void
Update
()
Should be called once per frame.
void
UpdateObstacle
(
ObstacleVertex | obstacle | Obstacle to update |
Vector3 [] | vertices | New vertices for the obstacle, must have at least the number of vertices in the original obstacle |
Matrix4x4 | matrix | Matrix to multiply vertices with before updating obstacle |
)
Updates the vertices of an 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.
Public Variables
float
DesiredDeltaTime
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.
MovementPlane
movementPlane = MovementPlane.XZ
Determines if the XY (2D) or XZ (3D) plane is used for movement.
bool
Multithreading
Is using multithreading.
Obstacles in this simulation.
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 necessary.
float
symmetryBreakingBias = 0.1f
Bias agents to pass each other on the right side.
If the desired velocity of an agent puts it on a collision course with another agent or an obstacle its desired velocity will be rotated this number of radians (1 radian is approximately 57°) to the right. This helps to break up symmetries and makes it possible to resolve some situations much faster.
When many agents have the same goal this can however have the side effect that the group clustered around the target point may as a whole start to spin around the target point.
Recommended values are in the range of 0 to 0.2.
If this value is negative, the agents will be biased towards passing each other on the left side instead.
Private/Protected Members
List<Agent>
agents
Agents in this simulation.
void
BlockUntilSimulationStepIsDone
()
Blocks until separate threads have finished with the current simulation step.
When double buffering is done, the simulation is performed in between frames.
void
CleanAndUpdateObstaclesIfNecessary
()
float
desiredDeltaTime = 0.05f
Inverse desired simulation fps.
bool
doCleanObstacles = false
bool
doubleBuffering = true
Use Double Buffering.
bool
doUpdateObstacles = false
void
ScheduleCleanObstacles
()
Deprecated Members
)
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.