# Agent Movement

You make agent move to new location by calling one of its numerous movement functions.

The continuous space has the biggest number of functions that define movement. There are also specific functions for movement in GIS space and discrete space.

Check what specific features have agents when moving inside and outside of networks.

You can set the initial speed and rotation of agent animation and define agent reaction on reaching the destination.

### Functions

First, there are functions to obtain the current location and distance (these functions work when the agent is moving as well). Then there is a set of functions for relocating the agent and obtaining the related information:

moveTo() and moveToInTime() functions provide several notations, enabling defining target location in different ways:

You can define location as:

• X, Y coordinates: moveTo(double x, double y)
• X, Y, Z coordinates: moveTo(double x, double y, double z)
• Point: moveTo(Point location)
• Attractor: moveTo(Attractor attractor)
• Network node: moveTo(Node node)
• Network node and location inside the node – (x,y,z) point: moveTo(Node node, Point location)
Setting the initial location at the model startup
 Function Description void setXY(double x, double y) Sets the coordinates of the agent location. Should only be used to initialize the agent location. Assumes the agent is not moving. Parameters: x - the x coordinate of the location y - the y coordinate of the location

Obtaining the current location
 Function Description double getX() Returns the current (up-to-date) x coordinate of the agent in continuous space. double getY() Returns the current (up-to-date) y coordinate of the agent in continuous space. double getZ() Returns the current (up-to-date) z coordinate of the agent in continuous space Position getPosition() Returns the current (up-to-date) x, y (and z in case of 3D space) coordinate of the agent in continuous space and its orientation as well.

Movement
 Function Description void moveTo(double x, double y) Starts movement in the direction of the given target location in continuous 3D space. Parameters: x - the x coordinate of the target location y - the y coordinate of the target location void moveTo(double x, double y, double z) Starts movement in the direction of the given target location in continuous 3D. Parameters: x - the x coordinate of the target location y - the y coordinate of the target location z - the z coordinate of the target location void moveTo(Point location) Starts movement in the direction of the given target location in continuous 3D. Parameters: location - the target location void moveTo(Node node, Point location) Starts movement to the given network node. Parameters: node - the network node. location - (optional) location within node, may be null void moveTo(Attractor attractor) Starts movement to the given attractor. Parameters: attractor - the attractor void moveToNearestAgent (java.lang.Iterable agents) Starts movement to the nearest agent from the given collection. Stops any current movement. Parameters: agents - the collection of agents boolean isMoving() Tests if the agent is currently moving in continuous 3D space. Returns: true if the agent is moving, false otherwise double timeToArrival() Returns the time to arrival to the target location in continuous space, in model-time units. If the agent is not moving, returns 0. void stop() Stops the agent (if it was moving) and leaves it at the current location. On Arrival is not executed

Movement with specified trip time
 Function Description void moveToInTime(Point location, double tripTime) Starts movement in the direction of the given target location. Changes the speed of the agent in order to reach target in tripTime model time units. Parameters: location - the target location tripTime - the time of the movement trip void moveToNearestAgent (java.lang.Iterable agents, double tripTime) Starts movement to the nearest agent from the given collection. Stops any current movement. Changes the speed of the agent in order to reach target in tripTime model time units. Parameters: agents - the collection of agents tripTime - the time of the movement trip void moveToInTime(double x, double y, double tripTime) Starts movement in the direction of the given target location in continuous 3D or GIS space. Changes the speed of the agent in order to reach target in tripTime model time units. Parameters: x - the x coordinate of the target location y - the y coordinate of the target location tripTime - the time of the movement trip void moveToInTime(double x, double y, double z, double tripTime) Starts movement in the direction of the given target location in continuous 3D. Changes the speed of the agent in order to reach target in tripTime model time units. Parameters: x, y, z :  X-, Y-, Z- coordinates of the target location tripTime - the time of the movement trip void moveToInTime(Node node, Point location, double tripTime) Starts movement to the given network node. Changes the speed of the agent in order to reach target in tripTime model time units. Parameters: node - the network node. location - (optional) location within node, may be null tripTime - the time of the movement trip void moveToInTime(Attractor attractor, double tripTime) Starts movement to the given attractor. Changes the speed of the agent in order to reach target in tripTime model time units. Parameters: attractor - the attractor. tripTime - the time of the movement trip

Instant movement (jumping)
 Function Description void jumpTo(double x, double y) Instantly moves the agent to a given location in continuous 3D  space. Terminates any movement. Doesn't call "on arrival" code. Parameters: x - the x coordinate of the new location y - the y coordinate of the new location void jumpTo(double x, double y, double z) Instantly moves the agent to a given location. Terminates any movement. Doesn't call "on arrival" code. Parameters: x - the x coordinate of the new location y - the y coordinate of the new location z - the z coordinate of the new location void jumpTo(Node node, Point location) Instantly moves the agent to a given network location. Terminates any movement. Doesn't call "on arrival" code. Parameters: location - the new location void jumpTo(Point location) Instantly moves the agent to a given location without. Terminates any movement. Doesn't call "on arrival" code. Parameters: node - the network node location - the new location

Obtaining the target coordinates
 Function Description double getTargetX() Returns the x of the target location if moving, otherwise current x in continuous 3D space space. double getTargetY() Returns the y of the target location if moving, otherwise current y in continuous 3D space space. double getTargetZ() Returns the z of the target location if moving, otherwise current z in continuous space.

Distance calculation
 Function Description double distanceTo(Agent other) Calculates the distance from this agent to another one in continuous 3D space. Parameters: other - another agent double distanceTo(double x, double y) Calculates the distance from this agent to a given point in continuous 3D space. Parameters: x - the x coordinate of the point y - the y coordinate of the point double distanceTo(double x, double y, double z) Calculates the distance from this agent to a given point (x,y,z) in continuous space. Parameters: x - the x coordinate of the point y - the y coordinate of the point z - the z coordinate of the point double distanceTo(Point p) Calculates the distance from this agent to a given point (x,y,z) in continuous space. Parameters: p - the point

Speed
 Function Description double getSpeed() Returns the current value of the agent speed in continuous space (measured in pixels per model-time-unit). Note that nonzero speed does not mean the agent is moving - it starts moving only after moveTo is called. void setSpeed(double v) Changes speed of the agent in continuous space (measured in pixels per model-time-unit). If the agent is moving, it continues moving with the new speed. Note that nonzero speed does not mean the agent is moving - it starts moving only after moveTo() is called. Parameters: v - the new speed

Rotation
 Function Description double getRotation() Returns the current rotation angle of the agent (in radians) in continuous 3D space  (measured from animation point (1, 0), CW around (0, 0) point). If rotation is not frozen, the rotation changes each time the agent starts moving and also changes multiple times during movement in GIS space. void setRotation(double rotation) Sets the rotation angle (in radians) of the agent animation in continuous 3D space. Depending on automatic rotation setting, this rotation value may be overridden during the next call of moveTo() or during current movement (if any exists). Parameters: rotation - the rotation angle of the agent in radians (measured from animation point (1, 0), CW around (0, 0) point) void setAutomaticHorizontalRotation (boolean yes) Tells agent to rotate automatically during movements. Parameter: yes - true - agent will be rotated during movement, false - rotation will not be changed boolean isAutomaticHorizontalRotation() Returns true if agent is set to be rotated during movement, false otherwise. void setVerticalRotation (double rotation) Sets the vertical rotation (angle in radians), along Z-axis of the agent animation in 3D space. Depending on automatic vertical rotation setting, this rotation value may be overridden during the next call of moveTo() or during current movement (if any exists) along path/polyline. Parameters: rotation - the vertical rotation angle of the agent in radians double getVerticalRotation() Returns the current vertical rotation angle of the agent in 3D space in radians around Y axis (CW from Z to X). If automatic vertical rotation mode is set, the rotation changes each time the agent starts moving and also changes multiple times during movement along path/polyline. void setAutomaticVerticalRotation (boolean yes) Tells agent to rotate automatically (in vertical direction, along Z-axis) during movement in 3D. Parameters: yes - true - agent will be rotated during movement (e.g. box travelling on conveyor slope), false - rotation will not be changed (e.g. passenger going upstairs) boolean isAutomaticVerticalRotation() Returns true if agent is set to be rotated in 3D (in vertical direction, along Z-axis) during movement (e.g. box travelling on conveyor slope), false otherwise.

There are also specific functions for movement in GIS space and discrete space. To set the initial speed and rotation of the agent
1. Open the properties of the agent type by clicking it in the Projects view.
2. Open the Dimensions and movement section of the properties.
3. In the Initial speed field, specify the initial speed of agents of this type (in meters per second).
4. If you want the agent animation shape to be rotated forward the movement heading, select the checkboxes below. To define the reaction on reaching the destination

1. Open the Agent actions section of the agent type's properties
2. Type Java code defining the action in the On arrival to target location field.

Although AnyLogic directly supports movement at a constant speed and zero acceleration, you can model acceleration/deceleration by changing the speed at the appropriate time moments. You can use statechart with states like Static, Slow, Medium, Fast, etc. and timeout transitions to control the motion.