Background Information

Grid World is a simulation of simple reflex agents.  Each agent is placed in a walled environment, and is able to move north, south, east, or west depending on what it percepts in its surroundings.  The goal is to allow the agent to be able to follow the wall in its environment.


Task to Complete

Develop a program that simulates Braitenberg’s Vehicles.  This program must:

  • be presented using graphics
  • simulate at least three of the vehicles
  • be interactive with the user

August 25th, 2016 – Program Framework

  • Create basic graphics framework for the simulation
    • JFrame for the simulation window (extended by the main class)
    • JPanel for the display of the vehicles
    • MouseMotionListener and MouseListener to track the location of the mouse
  • Create design for vehicles (using shapes on Microsoft Word, and snipping as .PNG)
  • Create abstract Vehicle class, with the following attributes/variables:
    • integer variables carXcarY to store location of the vehicle
    • long variables prevTimeelapsedTime to keep track of passed time (used in vehicle motion)
    • boolean variable inBounds to keep track of whether or not the vehicle is within range of the heat source to be affected
    • Image reference variable img to store the image of the vehicle to be displayed
    • integer variables imgWidthimgHeight to store the width and height of the image
    • Graphics2D reference variable g that takes in the graphics object from the main class on which to draw the vehicle
    • JPanel reference variable panel that stores the JPanel object used in the main class, to use in the Vehicle class as the ImageObserver
  • Create the constructor of the Vehicle abstract class, that:
    • initializes carXcarYJPanel from parameters
    • reads the image file, given the file name, and initializes img
    • initializes imgWidthimgHeight, based on the image
    • sets prevTime to the current time (System.nanoTime();)
  • Create the methods of the Vehicle class:
    • abstract methods detect()act()draw() to be implemented by specific vehicles
    • update() method that:
      • takes in the current mouse locations from the main method
      • takes in the Graphics2D object on which the vehicle will be drawn
      • calls the detect()act(), and draw() methods in that order
      • updates prevTime after all processes are complete

August 29th, 2016 – Simple Vehicle

  • Create the constructor of the main class (called Environment) that:
    • sets up the graphics discussed above
    • creates an ArrayList of Vehicle objects, called vehicles
    • runs an infinite while loop to update and display the vehicles
  • Create the while loop in the Environment constructor that:
    • creates a new BufferedImage object to display
    • loops through vehicles and calls the update method of each, passing in the the Graphics of the BufferedImage for the Vehicle to draw on and mouseX and mouseY
    • calls a mouseDisplay method to display an image of a candle instead of the original mouse cursor and to draw a “heat range” circle around the candle
  • Create the SimpleVehicle class to simulate the first vehicle that:
    • has the double instance variables distance to store the distance away from the mouse; vel to store the velocity of the vehicle; and speedFactor as a constant used later to determine the speed of the vehicle on the display
    • has a constructor that calls the superclass constructor and initializes these instance variables with its parameters
    • implements the detect() method that:
      • subtracts the current time by prevTime to find elapsedTime
      • calculates distance with carXcarYmouseX, and mouseY
    • implements the act() method that:
      • uses the inverse square law to calculate vel from distance
      • increments carX by vel*elapsedTime*speedFactor
      • updates sensorX based on the new carX
    • implements the draw() method that:
      • uses the drawImage method to display the image of the vehicle on the graphics object at position carX and carY

August 31th, 2016 – Fear Vehicle

  • Create the FearVehicle class to simulate the Fear Vehicle, that:
    • has the the integer instance variables leftSensorXleftSensorYrightSensorXrightSensorY to store the location of the sensor
    • has the double instance variables leftVelrightVelleftDistancerightDistance to store the velocities of the wheels and the distances from the respective sensors to the mouse
    • has the double instance variables anglespeedFactor, and fearFactor to store how much the vehicle has rotated by from the horizontal and the constants for how fast the vehicle goes and how sensitive it is to heat
    • has a constructor that calls the superclass constructor and initializes these instance variables with its parameters
    • implements the detect() method that:
      • also calculates elapsedTime as described above
      • calculates leftDistance and rightDistance
    • implements the act() method that:
      • calculates leftVel and rightVel using inverse square law
      • finds the difference of leftVel and rightVel and divides it by the axle length of the vehicle (essentially the height of the image) to get angle
      • finds the average of leftVel and rightVel, and incrementing carX and carY by the cosine and sine values of angle multiplied by that average value
      • uses trigonometry to find the new rotated location of the sensors, and updates leftSensorXleftSensorYrightSensorX, and rightSensorY
    • implements the draw() method that:
      • creates a new AffineTransform object to use to rotate the image
      • use this AffineTransform object and the graphics drawImage method to display the vehicle on the screen with the intended rotation
  • Note on the progress:
    • Until now, the programming process has been relatively simple, but many obstacles were encountered today.
      • First of all, I had to do some reseearch and use physics to find a suitable formula for calculating the rotation angle given the velocities of the individual wheels.
      • Next, finding a way to calculate the new sensor locations after the rotation also took some time.
      • Also, I had to do some research to learn how to use AffineTransform to rotate an image on the screen.
    • However, now that FearVehicle has been figured out, adapting its code to create the other vehicles will be relatively easy.

September 2nd, 2016 – Other Vehicles and User Interaction

  • Create the AggressionVehicle from FearVehicle by changing the left/right wheels around (using leftDistance to calculate rightVel and vice versa)
  • Create the LoveVehicle from FearVehicle by using “square law” instead of inverse square law (e.g. leftVel = speedFactor*leftDistance*leftDistance)
  • Create the ExplorerVehicle from LoveVehicle by again, changing the left/right wheels and their velocities around
  • Fine tuning the constants used for each vehicle (e.g. speedFactorfearFactorloveFactor, etc.) so that the vehicles move as smoothly as possible
  • Adding the “Stealth Mode” feature that allows the user to move the heat source (candle) while the vehicle still sees it in the same position by holding any mouse button down.  This was achieved through:
    • adding a boolean steathMode variable to keep track of whether or not the mouse is being held down; this will be used to make further decisions (e.g. display, vehicle movement, etc.)
    • adding variables holdX and holdY to store mouseX and mouseY when the mouse is held down, and displaying the candle at this position
    • displaying text using the drawString method of the Graphics class to show whether or not Stealth Mode is on
  • Adding KeyListener to the Environment class to allow for more user interaction and flexibility in the program such as:
    • pressing the ‘S’, ‘F’, ‘A’, ‘L’, ‘E’ keys to create the Simple, Fear, Aggression, Love, and Explorer vehicles, respectively
    • pressing the ‘R’ key to clear all vehicles
    • pressing the ‘I’ key to display the count of each vehicle type
    • pressing the arrow keys (up/down) to increase/decrease to heat range of the candle (a larger range will allow the vehicle to detect it from a further distance)
    • pressing the ‘Page Up’ and ‘Page Down” keys to also increase/decrease the heat range, but at a faster rate
  • At this point, the program is pretty much complete.

 

Advertisements