AnyLogic
Expand
Font size

Creating custom car types

If you have cars of different types in your model (such as a car, truck, bus, and so on) with specific custom attributes (the speed, length, and so on) and you want them to look different at model runtime, you should create custom car types and generate cars of different car types in your model.

To define a custom car type

  1. Drag the Car Type  element from the Road Traffic Library palette into the graphical editor.

  2. The New agent wizard will open.
  3. On the first page of the wizard, specify Agent type name (for example, Bus).Click Next.

  4. Select animation for the cars of this type. If you want the cars to be animated both in 2D and 3D, select 3D and select a 3D animation shape from the list below. Otherwise, select 2D and select the figure suiting your needs from the list. If there is none, select None, you can draw the animation later. When you are done selecting the animation, click Next to proceed to the next step.

  5. Specify the parameters for this car type. For example, let us create a parameter for the vehicle’s speed. Click <add new...> in the list to create a new parameter.

  6. In the right pane of the wizard you will see the section where you can configure the parameter selected in the table on the left. Set the parameter’s name in the Parameter field: speed. Since this parameter defines speed, select Speed from the Type drop-down list below. Set the speed units for this parameter in the list on the right: kilometers per hour. Finally, specify the parameter’s Default value: 75.

  7. When you have finished, click Finish at the bottom of the wizard.

The graphical diagram of the created car type will open. You will see the chosen car animation there and the parameters you have defined in the Wizard.

Here you can modify the car animation. If you did not specify an animation shape for this car type in the Car Type Creation Wizard, you can add your custom 3D object or 2D figure to the car type diagram here, with the figure centered at point (0,0). This figure will be used in the model to animate cars of this particular type.

Car type properties

General

Name — The name of the car type.

Since AnyLogic generates Java class for each car type, you should follow Java naming guidelines and start the name with an uppercase letter.

Ignore — If selected, the car type is excluded from the model.

Parameters preview

The Parameters preview properties are available only if this car type has any parameters. Here you can rearrange the parameters of this car. Cars of this type will list parameters in their property view in this defined order.

Agent actions

On startup — The code to execute after all agents throughout the whole model are constructed, connected, and initialized, and before anything else is done. This is a place for some additional initialization of the car. The order of execution of On startup code of different agents is not guaranteed. e recommend that you do your car initialization not here, but in the On exit action property of the CarSource block that generates cars in your model.

On destroy — The code to execute when destroying a car of this type. This parameter is derived from the agent type and is rarely used for cars. For details, refer to the Agent article.

On arrival to target destination — This parameter is derived from the agent type and is not applied to the movement defined with a Road Traffic Library flowchart.

On before step — This parameter is derived from the agent type and is rarely used for cars. For details, refer to the Agent article.

On step — This parameter is derived from the agent type and is rarely used for cars. For details, refer to the Agent article.

Agent in flowcharts

Use in flowcharts as — Specifies the role for agents of this type in the flowcharts. To allow these these agents to be used as cars in the blocks of the Road Traffic Library, leave the Car option selected. This will give the agent of this type car-specific functions: getAverageSpeed(), getDistanceDriven(), getRoad(), and so on.

On enter flowchart block — The code to execute when this car enters the flowchart.

On exit flowchart block — The code to execute when this car exits the flowchart.

On seize resource — This parameter is derived from the agent type and is rarely used for cars.

On release resource — This parameter is derived from the agent type and is rarely used for cars.

Dimensions and movement

The parameters in this section are derived from the agent type and are rarely used for cars. For details, refer to the Agent article.Define the speed and length of the car in the Car properties section of the CarSource block that generates cars in your model.

Space and network

The parameters in this section are derived from the agent type and are rarely used for cars. For details, refer to the Agent article.

Advanced Java

The parameters in this section are derived from the agent type and are rarely used for cars. For details, refer to the Agent article.

Advanced

The parameters in this section are derived from the agent type and are rarely used for cars. For details, refer to the Agent article.

Generating cars of a custom type and accessing custom car attributes

To generate the cars of a custom type, select this type (for example, Bus) in the New car parameter of the corresponding CarSource block. Then the cars in that flowchart will be of that custom type, and you can use car in the flowchart blocks to explicitly address the additional functionality of that car type.

For example, you can use the previously specified speed parameter to set up the speed for Bus cars generated by CarSource. In both the Initial speed and Preferred speed properties of the CarSource block, type car.speed.

How can we improve this article?