Zombie[] zombies; // declaring a global variable // to hold an array of Zombie objects Human player; //creating a global variable for player of type Human P5gps gps; //instantiate a GPS class //number of zombies int zombieCount = 100; //start and finish position float start_x = 300; float start_y = 200; //ratios based on top-left, bottom right, 640 X 480 float xPixelVal = width/(-118.28242+118.28689); float yPixelVal = height/abs(34.01852-34.02499); boolean printText = false; void setup() { size(640, 480); //make video //framerate(30); gps = new P5gps(4800, "$GPRMC"); zombies = new Zombie[zombieCount]; // creating an (empty) array with capacity zombieCount for (int i = 0; i < zombieCount; i++) // iterating zombieCount times { zombies[i] = new Zombie(); // populating the array with Zombies zombies[i].x = random(0, width); zombies[i].y = random(0, height); zombies[i].w = 2; zombies[i].h = 2; } //create the player player = new Human(); player.x = start_x; player.y = start_y; player.w = 10; player.h = 10; } void loop() { //refresh background(150); //println((gps.locked() ? "Locked: Latitude" : "Unlocked: Latitude:") + gps.latitude() + " Longitude:" + gps.longitude() + " Velocity:" + gps.velocity() + " Heading:" + gps.velocity()); //delay(1000); //zombies do their thing for (int i = 0; i < zombieCount; i++) // iterating 33 times { zombies[i].hangOut(); } //human does theirs player.gpsPosition(); } void serialEvent() { gps.processNMEA((char)serial); } class Zombie { float x, y, w, h, tx, ty, delta_x, delta_y, xSpeed, ySpeed; void hunt() { delta_x = abs(x-tx); delta_y = abs(y-ty); if(delta_x>delta_y) { xSpeed = 0.5f; ySpeed = 0.5f*(y/x); } else{ ySpeed = 0.5f; xSpeed = 0.5f*(x/y); } drawZombie(); setTarget(player.x, player.y); if (tx>x) { x = x + xSpeed; } else { x = x - xSpeed; } if (ty>y) { y = y + ySpeed; } else { y = y - ySpeed; } } void hangOut() { //set the x,y target as the human setTarget(player.x, player.y); //calculate the delta x, y delta_x = tx-x; delta_y = ty-y; //determine distance from player based on hypoteneuse (c) float a_squared = sq(delta_x); float b_squared = sq(delta_y); float c = sqrt(a_squared+b_squared); //check distance (if we are close, then chase, if we are far, then don't if (c>80) { drawZombie(); //pick a direction to wander - 0 is west, 1 is east, 2 is north, 3 is south int walkDirection = (int)(random(4)); if (walkDirection == 0) { x--; } if (walkDirection == 1) { x++; } if (walkDirection == 2) { y--; } if (walkDirection == 3) { y++; } } else { hunt(); //chase = true; } } void setTarget(float xin, float yin) { tx = xin; ty = yin; } void drawZombie() { ellipseMode(CENTER_RADIUS); fill(150, 0, 0, 50); ellipse(x, y, w, h); } } class Human { float x; float y; float w; float h; float xSpeed = 1.5f; float ySpeed = 1.5f; float curr_x; float curr_y; void gpsPosition() { drawHuman(); curr_x = gps.latitude(); curr_y = gps.longitude(); x = xPixelVal*curr_x; y = yPixelVal*curr_y; print("x: "+x); print("y: "+y); } void drawHuman() { ellipseMode(CENTER_RADIUS); fill(0, 255, 255, 60); ellipse(x, y, w, h); } } // GPS code for Processing // Glen Murphy, glen@glenmurphy.com class P5gps { private boolean locked = false; private boolean available = false; private String latitude; // ddmm.mmmm private String longitude; // ddmm.mmmm private int north; private int east; private String knots; private String heading; private String altitude; // metres private String nmea; private String nmeaType; P5gps(int rate, String nmeaTypeIn) { beginSerial(rate); nmeaType = nmeaTypeIn; } void processNMEA(char nmeaIn) { nmea += nmeaIn; if(nmeaIn == '\n') { String[] dataBlock = nmea.split(","); if(dataBlock[0].equals(nmeaType)) { if(nmeaType.equals("$GPRMC")) { latitude = dataBlock[3]; north = (dataBlock[4].equals("N")) ? 1 : -1; longitude = dataBlock[5]; east = (dataBlock[4].equals("E")) ? 1 : -1; knots = dataBlock[7]; heading = dataBlock[8]; locked = dataBlock[2].equals("A") ? true : false; } else if (nmeaType.equals("$GPGGA")) { latitude = dataBlock[2]; north = (dataBlock[3].equals("N")) ? 1 : -1; longitude = dataBlock[4]; east = (dataBlock[5].equals("E")) ? 1 : -1; locked = (Integer.valueOf(dataBlock[6]).intValue() > 0) ? true : false; altitude = dataBlock[9]; } available = true; } nmea = ""; } } float minutesToDegrees(float dm) { return (int)(dm / 100) + (dm - ( (int)(dm/100) ) * 100) / 60; } float latitude() { if(!available) return 0; // convert from ddmm.mmmm to dd.dddddd return minutesToDegrees(Float.valueOf(latitude).floatValue()) * north; } float longitude() { if(!available) return 0; // convert from ddmm.mmmm to dd.dddddd return minutesToDegrees(Float.valueOf(longitude).floatValue()) * east; } float velocity() { if(!available) return 0; // return velocity in km/h return Float.valueOf(knots).floatValue() * 1.85; } float heading() { if(!available) return 0; return Float.valueOf(heading).floatValue(); } boolean locked() { return locked; } }