CMSC 110 (Introduction to Computing)

Spring 2016 - Section 01

Assignment #04

Due by 2:25 pm on Thursday, March 17, 2016

Task: Design an interactive composition using classes. You should design a class to model an object of your choice. Non-animated instances of this, or any additional objects of your choice, should be incoporated into the sketch where appropriate. You have 3 options for how to implement the the object interactions below:

Options (choose one):

  1. Click to place A new dynamic instance of this object should appear wherever the user clicks on the sketch, and each dynamic object should have a primary animation over time once created .  For example, clicking could generate a snowflake that falls, or a set of concentric circles that expand outward in a rainbow and then collapse inward.  The objects can layer on top of each other in any order. 
  2. Drag to move A set number of dynamic instances of this object should exist on your sketch. For instance 12 black and 12 red checkers next to a checkers board. Though an instance object won't move around the screen, its primary animation should be within the bounds of the object, for example, a face that frowns or smiles or a clock that ticks. Dragging the object around should move it, and could, optionally, change the animation as it's being dragged or as it enters different parts of the sketch.
  3. Colliding objects A set number of dynamic instances of this object can either all start on the sketch or be added by clicking. All objects should move around, and when the moving objects collide, their primary animation will kick in that makes it clear that there was a collision. For example, the objects could explode, split, or combine.

Requirements:

  1. A class that models an object
  2. A dynamic instance of said object has primary animation implemented based on one of the above options
  3. All objects (any option) must also have a secondary animation that spins the object.
  4. You may not use any additional global variables besides the ones defined in the template below.
  5. Includes proper header and adequate comments.
  6. Drawing must scale properly regardless of the size of the sketch.
  7. Sign your work, using a parameterized function.

To get you started, create a class for your object using the following template:

/** Your header comments go here to describe the class */
class MyObjectNameGoesHere {

  // the object's x-coordinate
  int x;
  
  // the object's y-coordinate
  int y;

  // a parameter for tracking the elapsed lifetime of the object, which can be used for animating it
  int t = 0;

  /** for option 2, track whether object is being dragged */
  boolean selected = false;


  /** Constructor
   * @param xCoordinate the initial x coordinate of the object
   * @param yCoordinate the initial y coordinate of the object
   */
  MyObjectNameGoesHere (int xCoordinate, int yCoordinate) {
    ...
  }

  /** Draw the object.  
   *  This function should be called every iteration of draw() to display the object.
   */
  void display() {
  
    // drawing code for your object, with an animation based on t
    ...
  }

  /** Advance the object's animation.  
   *  This function should be called every iteration of draw() to advance it's animation.
   */
  void step() {
    // increment the time counter
    t++;   
    ...
  }

  /** for option 2 */
  void mousePressed() {
    // if mouse position is inside of object
        // set selected to true;
        // optionally start the pressed animation
  }

  /** for option 2 */
  void mouseReleased() {
     // if mouse position is inside of object
       // set selected to false;
       // optionally start the normal animation

  }

  /** for option 2 */
  void mouseDragged() {
    // if selected is true
      // set position of object to mouse position
      // optionally start the dragged animation
  }

  /** for option 3 */
  void collided() {
    // when called start the collision animation. 
  }
  
  
}

You do not need to follow this template for your class exactly, it is merely a suggestion.  You must also create the controlling code that creates new objects based on where the user clicks and drawing code that displays all of the static and animated objects.  Again, here is a template to get you started:

// An array of all the dynamically created objects
MyObjectNameGoesHere[] myObjects;

// An index into the myObjects array to show where to save the next new object.  
// Note that this index cannot ever be allowed to exceed MAX_NUM_OBJECTS; 
// instead it should cycle from 0 to MAX_NUM_OBJECTS and then be reset to begin again at 0.
int idxMyObjects = 0;

/** Processing's setup function */
void setup() {
  size(500,500);
  // The maximum number of dynamic objects that can be displayed at once
  int MAX_NUM_OBJECTS = 20;
  myObjects = new MyObjectNameGoesHere[MAX_NUM_OBJECTS];

   // For Option 2 only
   // create each of your MAX_NUM_OBJECTS objects at a prespecified
   // location and store them into the myObjects array

  ... // these ellipses mean your own code will follow
}

/** The main drawing loop for Processing */
void draw() {
  background(255);  // NOTE:  you are welcome to change this background 
 
  // use a for loop to display all of the objects and advance their animations
  // NOTE:  When writing this loop, you will need to test each element 
  //        of the myObjects array to see if it is an object or is empty.  
  //        Use the following code to test for an object: 
  //        if (myObjects[i] != null) { ... }
  //        This example uses i as the loop variable; your implementation may 
  //        use something different.
  ...
}
The mouse interaction for option 1 and 3 could use something like the following:
/** for option 1 or option 3 */
void mousePressed() {
  // create a new object based on the location clicked 
  // and store it into myObjects[idxMyObjects]
  ...

  // increment the index into myObjects, keeping it in the range 0...myObjects.length
  // NOTE:  Make sure you understand how this line of code works!  
  // It is complete as-is.  You don't need to do anything else 
  // to make the idxMyObjects counter work correctly.
  
  idxMyObjects = (idxMyObjects+1) % myObjects.length;
}
The mouse interaction for option 2 could use something like the following:
/** for option 2 */
void mousePressed() {
// use a for loop to call mousePressed() on all of the objects
}

/** for option 2 */
void mouseReleased() {
// use a for loop to call mouseReleased() on all of the objects
}

/** for option 2 */
void mouseDragged() {
// use a for loop to call mouseDragged() on all of the objects
}

Be sure to note that you must fill in code wherever you are instructed, but you will certainly need to add other variables and functions as well.  Be creative and artistic! 

Hints:

What to produce:

What to Hand in: