NavigationAgent2D
A 2D agent used to pathfind to a position while avoiding obstacles.
NavigationAgent2D.swift:26class NavigationAgent2D
A 2D agent used to pathfind to a position while avoiding static and dynamic obstacles. The calculation can be used by the parent node to dynamically move it along the path. Requires navigation data to work correctly.
Dynamic obstacles are avoided using RVO collision avoidance. Avoidance is computed before physics, so the pathfinding information can be used safely in the physics step.
This object emits the following signals:
Superclasses
class Node
Base class for all scene objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
class Signal1
Signal support.
class Signal2
Signal support.
class Signal3
Signal support.
Type members
Instance members
var avoidanceEnabled: Bool
If
true
the agent is registered for an RVO avoidance callback on theNavigationServer2D
. Whenvelocity
is used and the processing is completed asafe_velocity
Vector2 is received with a signal connection to [signal velocity_computed]. Avoidance processing with many registered agents has a significant performance cost and should only be enabled on agents that currently require it.var avoidanceLayers: UInt32
A bitfield determining the avoidance layers for this NavigationAgent. Other agents with a matching bit on the
avoidanceMask
will avoid this agent.var avoidanceMask: UInt32
A bitfield determining what other avoidance agents and obstacles this NavigationAgent will avoid when a bit matches at least one of their
avoidanceLayers
.var avoidancePriority: Double
The agent does not adjust the velocity for other agents that would match the
avoidanceMask
but have a loweravoidancePriority
. This in turn makes the other agents with lower priority adjust their velocities even more to avoid collision with this agent.var debugEnabled: Bool
If
true
shows debug visuals for this agent.var debugPathCustomColor: Color
If
debugUseCustom
istrue
uses this color for this agent instead of global color.var debugPathCustomLineWidth: Double
If
debugUseCustom
istrue
uses this line width for rendering paths for this agent instead of global line width.var debugPathCustomPointSize: Double
If
debugUseCustom
istrue
uses this rasterized point size for rendering path points for this agent instead of global point size.var debugUseCustom: Bool
If
true
uses the defineddebugPathCustomColor
for this agent instead of global color.var linkReached: Signal2
Notifies when a navigation link has been reached.
var maxNeighbors: Int32
The maximum number of neighbors for the agent to consider.
var maxSpeed: Double
The maximum speed that an agent can move.
var navigationFinished: SimpleSignal
Emitted once per loaded path when the agent internal navigation path index reaches the last index of the loaded path array. The agent internal navigation path index can be received with
getCurrentNavigationPathIndex
.var navigationLayers: UInt32
A bitfield determining which navigation layers of navigation regions this agent will use to calculate a path. Changing it during runtime will clear the current navigation path and generate a new one, according to the new navigation layers.
var neighborDistance: Double
The distance to search for other agents.
var pathChanged: SimpleSignal
Emitted when the agent had to update the loaded path:
var pathDesiredDistance: Double
The distance threshold before a path point is considered to be reached. This allows agents to not have to hit a path point on the path exactly, but only to reach its general area. If this value is set too high, the NavigationAgent will skip points on the path, which can lead to leaving the navigation mesh. If this value is set too low, the NavigationAgent will be stuck in a repath loop because it will constantly overshoot or undershoot the distance to the next point on each physics frame update.
var pathMaxDistance: Double
The maximum distance the agent is allowed away from the ideal path to the final position. This can happen due to trying to avoid collisions. When the maximum distance is exceeded, it recalculates the ideal path.
var pathMetadataFlags: NavigationPathQueryParameters2D.PathMetadataFlags
Additional information to return with the navigation path.
var pathPostprocessing: NavigationPathQueryParameters2D.PathPostProcessing
The path postprocessing applied to the raw path corridor found by the
pathfindingAlgorithm
.var pathfindingAlgorithm: NavigationPathQueryParameters2D.PathfindingAlgorithm
The pathfinding algorithm used in the path query.
var radius: Double
The radius of the avoidance agent. This is the “body” of the avoidance agent and not the avoidance maneuver starting radius (which is controlled by
neighborDistance
).var targetDesiredDistance: Double
The distance threshold before the final target point is considered to be reached. This allows agents to not have to hit the point of the final target exactly, but only to reach its general area. If this value is set too low, the NavigationAgent will be stuck in a repath loop because it will constantly overshoot or undershoot the distance to the final target point on each physics frame update.
var targetPosition: Vector2
If set, a new navigation path from the current agent position to the
targetPosition
is requested from the NavigationServer.var targetReached: SimpleSignal
Emitted once per loaded path when the agent’s global position is the first time within
targetDesiredDistance
to thetargetPosition
.var timeHorizonAgents: Double
The minimal amount of time for which this agent’s velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but less freedom in choosing its velocities. A too high value will slow down agents movement considerably. Must be positive.
var timeHorizonObstacles: Double
The minimal amount of time for which this agent’s velocities, that are computed with the collision avoidance algorithm, are safe with respect to static avoidance obstacles. The larger the number, the sooner the agent will respond to static avoidance obstacles, but less freedom in choosing its velocities. A too high value will slow down agents movement considerably. Must be positive.
var velocity: Vector2
Sets the new wanted velocity for the agent. The avoidance simulation will try to fulfill this velocity if possible but will modify it to avoid collision with other agents and obstacles. When an agent is teleported to a new position, use
setVelocityForced(velocity:)
as well to reset the internal simulation velocity.var velocityComputed: Signal3
Notifies when the collision avoidance velocity is calculated. Emitted when
velocity
is set. Only emitted whenavoidanceEnabled
is true.var waypointReached: Signal1
Notifies when a waypoint along the path has been reached.
func distanceToTarget(
) -> Double Returns the distance to the target position, using the agent’s global position. The user must set
targetPosition
in order for this to be accurate.func getAvoidanceLayerValue(layerNumber: Int32
) -> Bool Returns whether or not the specified layer of the
avoidanceLayers
bitmask is enabled, given alayerNumber
between 1 and 32.func getAvoidanceMaskValue(maskNumber: Int32
) -> Bool Returns whether or not the specified mask of the
avoidanceMask
bitmask is enabled, given amaskNumber
between 1 and 32.func getCurrentNavigationPath(
) -> PackedVector2Array Returns this agent’s current path from start to finish in global coordinates. The path only updates when the target position is changed or the agent requires a repath. The path array is not intended to be used in direct path movement as the agent has its own internal path logic that would get corrupted by changing the path array manually. Use the intended
getNextPathPosition
once every physics frame to receive the next path point for the agents movement as this function also updates the internal path logic.func getCurrentNavigationPathIndex(
) -> Int32 Returns which index the agent is currently on in the navigation path’s
PackedVector2Array
.func getCurrentNavigationResult(
) -> NavigationPathQueryResult2D? Returns the path query result for the path the agent is currently following.
func getFinalPosition(
) -> Vector2 Returns the reachable final position of the current navigation path in global coordinates. This position can change if the agent needs to update the navigation path which makes the agent emit the [signal path_changed] signal.
func getNavigationLayerValue(layerNumber: Int32
) -> Bool Returns whether or not the specified layer of the
navigationLayers
bitmask is enabled, given alayerNumber
between 1 and 32.func getNavigationMap(
) -> RID Returns the
RID
of the navigation map for this NavigationAgent node. This function returns always the map set on the NavigationAgent node and not the map of the abstract agent on the NavigationServer. If the agent map is changed directly with the NavigationServer API the NavigationAgent node will not be aware of the map change. UsesetNavigationMap(_:)
to change the navigation map for the NavigationAgent and also update the agent on the NavigationServer.func getNextPathPosition(
) -> Vector2 Returns the next position in global coordinates that can be moved to, making sure that there are no static objects in the way. If the agent does not have a navigation path, it will return the position of the agent’s parent. The use of this function once every physics frame is required to update the internal path logic of the NavigationAgent.
func getRid(
) -> RID Returns the
RID
of this agent on theNavigationServer2D
.func isNavigationFinished(
) -> Bool Returns
true
if the end of the currently loaded navigation path has been reached.func isTargetReachable(
) -> Bool Returns
true
ifgetFinalPosition
is withintargetDesiredDistance
of thetargetPosition
.func isTargetReached(
) -> Bool Returns true if
targetPosition
is reached. It may not always be possible to reach the target position. It should always be possible to reach the final position though. SeegetFinalPosition
.func setAvoidanceLayerValue(layerNumber: Int32, value: Bool
) Based on
value
, enables or disables the specified layer in theavoidanceLayers
bitmask, given alayerNumber
between 1 and 32.func setAvoidanceMaskValue(maskNumber: Int32, value: Bool
) Based on
value
, enables or disables the specified mask in theavoidanceMask
bitmask, given amaskNumber
between 1 and 32.func setNavigationLayerValue(layerNumber: Int32, value: Bool
) Based on
value
, enables or disables the specified layer in thenavigationLayers
bitmask, given alayerNumber
between 1 and 32.func setNavigationMap(RID
) Sets the
RID
of the navigation map this NavigationAgent node should use and also updates theagent
on the NavigationServer.func setVelocityForced(velocity: Vector2
) Replaces the internal velocity in the collision avoidance simulation with
velocity
. When an agent is teleported to a new position this function should be used in the same frame. If called frequently this function can get agents stuck.