// The Avatar Class // Code idea inspired by Non-orthogonal Reflection by Ira Greenberg // Found @ http://www.processing.org/learning/topics/reflection1.html // Code idea also inspired by Alasdair Turner's Swarm Algorithm // Found @ http://www.openprocessing.org/visuals/?visualID=7499 class Avatar { // Vectors for Avatar, location, velocity, acceleration // Float variable for maximum speed PVector avatar; PVector loc; PVector vel; PVector acc; float topspeed; // Constructor for the Avatar // Random start location within the sketch and maxspeed Avatar () { loc = new PVector (random(width), random(height)); vel = new PVector (0,0); topspeed = 2; } // Update function for the Avatar // Which causes the Avatar to follow the mouse around void update () { // Vector based on mouseX, mouseY PVector mouse = new PVector (mouseX, mouseY); // Vector for direction towards mouse vector, from the location vector // Subraction between the two vectors to get the direction towards mouse PVector dir = PVector.sub (mouse, loc); // Normalise the direction vector dir.normalize (); // Multiply the vector by below number, change this to have a little fun dir.mult (0.07); // Set acceleration in this vector direction acc = dir; // Add acceleration to velocity vel.add (acc); // Limit the velocity by our predefined topspeed vel.limit (topspeed); // Add velocity to our location loc.add (vel); // Make sure the Avatar stays inside the sketch and doesn't run off loc.x = constrain (loc.x, 0, width-1); loc.y = constrain (loc.y, 0, height-1); } // Display function for the Avatar // Get location as well as make the actual ellipse (the Avatar) void display () { noStroke (); fill (255); smooth (); avatar = loc.get(); float [] avatarArray = avatar.array (); ellipse (avatarArray [0], avatarArray [1], 10, 10); } }