+1 (972) 883-2091
ECSS 4.220, 800 W Campbell Rd, Richardson, TX 75083–0688, US

Implementing the “state” package

1. Implementing the “Posture” enumerator

The posture enumerator contains the list of animations that can be assigned to a vehicle agent during the simulation. This list corresponds to the actual list of animations that comes with the animated 3D model.

  1. Create a new “enum” inside the “state” package and name it “Posture”.
  2. Write the list of animation inside the “Posture” enum. For instance, the vehicle 3D model that we will use in this tutorial has the following set of animations:
    package edu.utdallas.mavs.traffic.simulation.sim.common.state;
    
    public enum Posture
    {
        backward_fast,
        backward_mid,
        backward_slow,
        forward_fast,
        forward_fast_left,
        forward_fast_right,
        forward_mid,
        forward_mid_left,
        forward_mid_right,
        forward_slow,
        forward_slow_left,
        forward_slow_right,
        zero_position
    }
    

2. Implementing the “VehicleAgentState” class

This class describes the vehicle agent’s state, in other words it describes the physical conditions of a vehicle agent. As illustrated in the below figure, the “VehicleAgentState” class extends the class “edu.utdallas.mavs.divas.core.sim.common.state.AgentState”. Also, it implements the interface “edu.utdallas.mavs.divas.core.sim.common.percept.VisionPerceptor”. The”VehicleAgentState” class contains the following attributes:

  1. “visibleDistance”: The distance that defines the range of a vehicle agent’s vision sensor.
  2. “currentTasks”: The set of tasks a vehicle agents currently performs.
  3. “posture”: The current posture {i.e., animation) of the vehicle agent./li>

The “VehicleAgentState” class provides a concrete implementation for the method “calculateVisibleRegion” defined in the “VisionPerceptor” interface. This method calculates the area of the traffic environment that can be seen by a vehicle agent using its vision sensor. Each vehicle agent in the simulation can have a different visible area. The calculation depends on the vehicle agent’s visible distance, its location, its heading, and its field of view. The figure below shows the calculated visible region for the vehicle v1 based on the aforementioned parameters.

To implement the “VehicleAgentState” class:

  1. Create a new class inside the “state” package and name it “VehicleAgentState”.
  2. Copy the code given below that describes the full implementation of a “VehicleAgentState” class.
   
package edu.utdallas.mavs.traffic.simulation.sim.common.state;

import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.util.List;

import com.jme3.math.Vector3f;

import edu.utdallas.mavs.divas.core.sim.agent.interaction.perception.sensors.vision.VisionAlgorithm;
import edu.utdallas.mavs.divas.core.sim.agent.task.Task;
import edu.utdallas.mavs.divas.core.sim.common.percept.VisionPerceptor;
import edu.utdallas.mavs.divas.core.sim.common.state.AgentState;

public class VehicleAgentState extends AgentState implements VisionPerceptor
{
    private static final long  serialVersionUID = 1L;


    /**
     * The visible distance the agent can perceive using its vision sense.
     */
    protected float            visibleDistance;

    /**
     * This agent's current tasks
     */
    protected List<Task>       currentTasks;
    
    /**
     * The current posture of the agent.
     */
    protected Posture         posture          = Posture.zero_position;

    @Override
    public Shape calculateVisibleRegion()
    {
        float visibleDistance = getVisibleDistance();
        Arc2D arc = new Arc2D.Float((getPosition().getX() - visibleDistance), (getPosition().getZ() - visibleDistance), (visibleDistance * 2), (visibleDistance * 2), 0, 0, Arc2D.PIE);

        Vector3f towards = getPosition().add(getHeading());

        arc.setAngleStart(new Point2D.Float(towards.getX(), towards.getZ()));
        // subtract by half the fov, this puts the heading at the center of the arc
        arc.setAngleStart(arc.getAngleStart() - getFOV() / 2);
        arc.setAngleExtent(getFOV());

        return arc;
    }

    @Override
    public float getFOV()
    {
        return 0;
    }

    @Override
    public float getVisibleDistance()
    {
        return visibleDistance;
    }

    @Override
    public void setFOV(float fov)
    {
        
    }

    @Override
    public void setVisibleDistance(float visibleDistance)
    {
        this.visibleDistance = visibleDistance;
    }

    @Override
    public void setVisionAlgorithm(VisionAlgorithm arg0)
    {
        // empty
    }

    @Override
    public float getVisionHeight()
    {
        return 0;
    }

    public List<Task> getCurrentTasks()
    {
        return currentTasks;
    }

    public void setCurrentTasks(List<Task> currentTasks)
    {
        this.currentTasks = currentTasks;
    }
    
    /**
     * Gets the posture of the agent.
     * 
     * @return The agent posture.
     */
    public Posture getPosture()
    {
        return posture;
    }

    /**
     * Changes the posture of the agent.
     * 
     * @param posture
     *        The agent posture.
     */
    public void setPosture(Posture posture)
    {
        this.posture = posture;
    }
}