/************************************************************ /* Authors: Dr. Parson & (replace this parenthetical comment with STUDENT NAME) /* Creation Date: 9/6/2020 /* Due Date: 09/24/2020 /* Course: CSC220 Object-Oriented Multimedia Programming /* Professor Name: Dr. Parson /* Assignment: 1. /* Sketch name: CSC220F20AvatarClassInAvatarRoom /* This 2D sketch puts the mobile avatar in a mostly-immobile /* 2D room, where each wall or piece of furniture is a 2D avatar, /* and at least one furniture surface moves. It illustrates /* the use of a Java interface, inheritance, an abstract base class /* containing helper functions & fields, and also simple collision /* detection using bounding boxes. Initial draft September 1, 2018. *********************************************************/ /* KEYBOARD COMMANDS: /* 'b' toggles display of bounding boxes for debugging, initially on /* 'f' toggles freezing of display in draw() off / on. /* '~' applies shuffle() to each Avatar object, repositioning the mobile ones. /* '!' applies forceshuffle() to each Avatar object, repositioning all of them. */ // The primary GLOBAL VARIABLE is for the collection of Avatar objects. // All Avatar state variables go inside of Avatar-derived subclasses. Avatar avatars [] ; // An array holding multiple Avatar objects. int backgroundColor = 255 ; // Wraps from 255 back to 0. boolean showBoundingBox = true ; // toggle with 'b' key boolean isFrozen = false ; // toggle with 'f' key to freeze display void setup() { // setup() runs once when the sketch starts, initializes sketch state. // size(1000, 800); fullScreen(P2D); background(backgroundColor); avatars = new Avatar [ 50 ] ; avatars[0] = new Professor(width/4, height/4, 2, 3, 2); // See constructors in their classes below to interpret parameters. int cyan = color(0, 255, 255, 255); // Red, Green, Blue, Alpha // By positioning based on system variables *width* and *height*, as opposed to // using fixed location numbers, your sketch will work with any display size. avatars[1] = new Furniture(width/2, 5, width, 10, cyan); // 10 pixels wide boundary is impenetrable avatars[2] = new Furniture(width/2, height-5, width, 10, cyan); avatars[3] = new Furniture(5, height/2, 10, height, cyan); avatars[4] = new Furniture(width-5, height/2, 10, height, cyan); avatars[5] = new Professor(3*width/4, 3*height/4, 2, 1, 1); int magenta = color(255, 0, 255, 255); final int barlength = 200 ; avatars[6] = new Furniture(barlength/2, height/2, barlength, 10, magenta); // 10 pixels wide boundary is impenetrable avatars[7] = new Furniture(width-barlength/2, 2*height/3, barlength, 10, magenta); avatars[8] = new Furniture(width/3, barlength/2, 10, barlength, magenta); avatars[9] = new Furniture(width/3, height-barlength/2, 10, barlength, magenta); color orange = color(255,184,0,255); avatars[10] = new Paddle(width/2, height/2, barlength, 10, .5, orange); for (int i = 11 ; i < avatars.length ; i++) { avatars[i] = new Professor(int(random(0,width)), int(random(0, height)), round(random(1,5)), round(random(-5,-1)), .25); } rectMode(CENTER); // I make them CENTER by default. rectMode is otherwise CORNER. ellipseMode(CENTER); imageMode(CENTER); shapeMode(CENTER); textAlign(CENTER, CENTER); } void draw() { if (isFrozen) { return ; // toggle 'f' key to freeze/unfreeze display. } // draw() is run once every frameRate, every 60th of a sec by default. background(backgroundColor); // This erases the previous frame. rectMode(CENTER); ellipseMode(CENTER); imageMode(CENTER); shapeMode(CENTER); textAlign(CENTER, CENTER); // Display & move all avatars in a for loop. for (int i = 0 ; i < avatars.length ; i++) { // Reinitialze these modes in case an Avatar changed them. rectMode(CENTER); ellipseMode(CENTER); imageMode(CENTER); shapeMode(CENTER); textAlign(CENTER, CENTER); stroke(0); noFill(); strokeWeight(1); avatars[i].move(); // Move before display so the bounding boxes are correct. avatars[i].display(); } if (showBoundingBox) { // Do this in a separate loop so we can do the initial part once. rectMode(CORNER); noFill(); stroke(0); strokeWeight(1); for (Avatar avt : avatars) { // For testing bounding box int [] bb = avt.getBoundingBox(); rect(bb[0], bb[1], bb[2]-bb[0], bb[3] - bb[1]); } } rectMode(CENTER); // back to defaults ellipseMode(CENTER); imageMode(CENTER); shapeMode(CENTER); textAlign(CENTER, CENTER); } // KEYBOARD COMMANDS documented at top of this sketch. // System calls keyPressed when user presses a *key*. // Examples of control characters like arrows in a later example. void keyPressed() { if (key == 'b') { // toggle bounding boxes on/off showBoundingBox = ! showBoundingBox ; } else if (key == 'f') { isFrozen = ! isFrozen ; } else if (key == '~') { for (int i = 0 ; i < avatars.length ; i++) { avatars[i].shuffle(); } } else if (key == '!') { for (Avatar a : avatars) { a.forceshuffle(); } } } /** overlap checks whether two objects' bounding boxes overlap **/ boolean overlap(Avatar avatar1, Avatar avatar2) { int [] bb1 = avatar1.getBoundingBox(); int [] bb2 = avatar2.getBoundingBox(); // If bb1 is completely above, below, // left or right of bb2, we have an easy reject. if (bb1[0] > bb2[2] // bb1_left is right of bb2_right || bb1[1] > bb2[3] // bb1_top is below bb2_bottom, now reverse them || bb2[0] > bb1[2] // bb2_left is right of bb1_right || bb2[1] > bb1[3] // bb2_top is below bb1_bottom, now reverse them ) { return false ; } // In this case one contains the other or they overlap. return true ; } /** * An *interface* is a specification of methods (functions) that * subclasses must provide. It provides a means to specify requirements * that plug-in derived classes must provide. * This interface Avatar specifies functions for both mobile & immobile * objects that interact in this sketch. **/ interface Avatar { /** * Avatar-derived class must have one or more variable * data fields, at a minimum for the x,y location, * where 0,0 is the middle of the display area. **/ /** Derived classes provide a constructor that takes some parameters. **/ /** * Write a display function that starts like this: push(); translate(x, y); and ends like this: pop(); with all display code inside the function. Write this in your derived class, not here in Avatar. In addition to translate, the display() code in your class must use one or more of scale (with 1 or 2 arguments), rotate, shearX, or shearY. You can also manipulate variables for color & speed. See my example classes for ideas. **/ void display(); /** Write move() to update variable fields inside the object. * Write this in your derived class, not here in Avatar. **/ void move(); /** * getBoundingBox returns an array of 4 integers where elements * [0], [1] tell the upper left X,Y coordinates of the bounding * box, and [2], [3] tell the lower right X,Y. This function * always returns a rectangular bounding box that contains the * entire avatar. Coordinates are those in effect when display() or * move() are called from the draw() function, **/ int [] getBoundingBox(); /** Return the X coordinate of this avatar, center. **/ int getX(); /** Return the Y coordinate of this avatar's center. **/ int getY(); /** Randomize parts of a *mobile* object's space, including x,y location. **/ void shuffle() ; /** Randomize parts of *every* object's space, including x,y location. **/ void forceshuffle(); } /** * An abstract class provides helper functions and data fields required by * all subclasses. Abstract class CollisionDetector provides location and * scaling and rotation data fields that subclasses use. It also provides * helper functions, notably detectCollisions() for collision detection, that * are used by all subclasses. The keyword *protected* means that only subclasses * can use protected data & methods. The keyword *private* means that only the * defining class can use them, and *public* means that any class can use them. **/ abstract class CollisionDetector implements Avatar { protected int myx, myy ; // x,y location of this object protected float myscale ; // scale of this object, 1.0 for no scaling protected float speedX ; // speed of motion, negative for left. protected float speedY ; // speed of motion, negative for up. float myrot = 0.0 ; // subclasses may rotate & scale float rotspeed = 0.0, sclspeed = 0.0 ; // subclasses may change myscale, myrot in move(). // Testing shows that mobile shapes may push other mobile shapes // off of the screen, depending on order of collision detection. // Some Avatar classes may want their displays to wander around outside. // Data field xlimit and ylimit test for that. // See java.lang.Integer in https://docs.oracle.com/javase/8/docs/api/index.html protected int xlimitleft = Integer.MIN_VALUE ; // no limit by default protected int ylimittop = Integer.MIN_VALUE ; // no limit by default protected int xlimitright = Integer.MAX_VALUE ; // no limit by default protected int ylimitbottom = Integer.MAX_VALUE ; // no limit by default // The constructor initializes the data fields. CollisionDetector(int avx, int avy, float spdx, float spdy, float avscale, float scalespeed, float rotation, float rotatespeed) { myx = avx ; myy = avy ; speedX = spdx ; speedY = spdy ; myscale = avscale ; sclspeed = scalespeed ; myrot = rotation ; rotspeed = rotatespeed ; } void shuffle() { // default is to do nothing; override this in derived class. } void forceshuffle() { // default is to change location; add to this in derived class. myx = round(random(10, width-10)); // Put it somewhere on the display. myy = round(random(10, height-10)); } int getX() { return myx ; } int getY() { return myy ; } // Check this object against every other Avatar object for a collision. // Also make sure it doesn't wander outside the x and y limit values // set by the constructor. Putting detectCollisions() in this abstract class // eliminates the need to put it into multiple derived class move() functions, // which can simply call this function. protected void detectCollisions() { int [] mine = getBoundingBox(); for (Avatar a : avatars) { if (a == this) { continue ; // this avatar always overlaps with itself } int [] theirs = a.getBoundingBox(); if (overlap(this,a)) { if (mine[0] >= theirs[0] && mine[0] <= theirs[2]) { // my left side is within them, move to the right speedX = abs(speedX); myx += 2*speedX ; // jump away a little extra } else if (mine[2] >= theirs[0] && mine[2] <= theirs[2]) { // my right side is within them, move to the left speedX = - abs(speedX); myx += 2*speedX ; } // Above may have eliminated the overlap, check before proceeding. mine = getBoundingBox(); if (overlap(this,a)) { // Do equivalent check for vertical overlap. if (mine[1] >= theirs[1] && mine[1] <= theirs[3]) { speedY = abs(speedY); // my top, send it down myy += 2*speedY ; } else if (mine[3] >= theirs[1] && mine[3] <= theirs[3]) { speedY = - abs(speedY); // my bottom, send it up myy += 2*speedY ; } } } } // Testing shows that mobile shapes may push other mobile shapes // off of the screen or thru Avatars, depending on order of collision detection. // Some Avatar classes may want their displays to wander around outside the display. // Data fields xlimit and ylimit test for that. if (xlimitleft != Integer.MIN_VALUE && myx <= xlimitleft && speedX < 0) { speedX = - speedX ; myx = xlimitleft + 1 ; if (myscale >= 1) println("DEBUG WENT OFF LEFT " + speedX); // Too many print statements, restrict to the bigger Avatars. // I usually comment out print statements until I am sure the bug is gone. } if (xlimitright != Integer.MAX_VALUE && myx >= xlimitright && speedX > 0) { speedX = - speedX ; myx = xlimitright - 1 ; if (myscale >= 1) println("DEBUG WENT OFF RIGHT " + speedX); } if (ylimittop != Integer.MIN_VALUE && myy <= ylimittop && speedY < 0) { speedY = - speedY ; myy = ylimittop + 1 ; if (myscale >= 1) println("DEBUG WENT OFF TOP " + speedY); } if (ylimitbottom != Integer.MAX_VALUE && myy >= ylimitbottom && speedY > 0) { speedY = - speedY ; myy = ylimitbottom - 1 ; if (myscale >= 1) println("DEBUG WENT OFF BOTTOM " + speedY); } } } /** * Professor is my Avatar-derived class that displays & moves a mobile Professor. * You must write your own Avatar-derived class. You can delete class Professor * or use it to interact with your Avatar-derived class. **/ class Professor extends CollisionDetector { /* The data fields store the state of the Avatar. */ protected int legdist = 0 ; // You can initialize to a constant here. Professor(int avx, int avy, float spdx, float spdy, float avscale) { super(avx,avy,spdx,spdy,avscale,0,0,0); // Call the base class constructor to initialize its data fields, // then initialize this class' data fields. xlimitright = width ; ylimitbottom = height ; // limit off-screen motion to xlimitleft = 0 ; // one width or height off the display ylimittop = 0 ; // in either direction } void shuffle() { forceshuffle(); // always do it. } // The display() function simply draws the Avatar object. // The move() function updates the Avatar object's state. void display() { // Draw the avatar. push(); // STUDENT *MUST* use push() & translate first in display(). translate(myx, myy); scale(myscale); noStroke(); fill(240, 150, 150); ellipse(0, 0, 50, 40); // head, 0,0 is the conceptual center of the object. // An object rotates around its 0,0 point. quad(-5 , 0, 5 , 0, 10 , 40 , -10 , 40 ); // neck fill(0); // professor gown ellipse(0, 60 , 40 , 80 ); // torso stroke(0); // stick arms & legs strokeWeight(8); line(0, 60 , -20 -abs(10-legdist) , 120 ); // left leg line(0, 60 , 20 +abs(10-legdist) , 120 ); // right leg strokeWeight(5); line(0, 60 , -40 , 20 -2*abs(10-legdist) ); // left arm line(0, 60 , 40 , 20 +2*abs(10-legdist) ); // right arm strokeWeight(2); fill(0, 50, 255); ellipse(-10 , -5 , 10 , 10 ); // avatar's right side of glasses ellipse(10 , -5 , 10 , 10 ); // avatar's right side of glasses line(-5 , -5 , 5 , -5 ); // glasses connector line(-15 , -5 , -22 , -8 ); // left earpiece line(15 , -5 , 22 , -8 ); // right earpiece fill(0); ellipse(0, 1 , 5 , 5 ); // nose arc(0, 10 , 20 , 10 , 0, PI); // mouth quad(-30 , -15 , 30 , -15 , 15 , -30 , -35 , -30 ); pop(); // STUDENT *MUST* use pop() last in display(). } // The move() function updates the Avatar object's state. void move() { // get ready for movement in next frame. myx = round(myx + speedX) ; myy = round(myy + speedY) ; legdist = (legdist+1) % 20 ; detectCollisions(); } int [] getBoundingBox() { int [] result = new int[4]; result[0] = myx-round(40*myscale) ; // left extreme of left arm result[1] = myy - round(30*myscale); // top of hat result[2] = myx + round(myscale*max(20 +abs(10-legdist),40)); // max of right leg & arm result[3] = myy + round(120*myscale) ; // bottom of legs return result ; } } /** * Class Furniture implements immobile obstacles as rectangles. * It adds fields for object width, height, and color. **/ class Furniture extends CollisionDetector { /* The data fields store the state of the Avatar. */ protected int mywidth, myheight, mycolor ; // Save the the problems of writing a new display function // by implementing no-op rotation and scaling here, // subclasses can use them. // rot is in degrees. // The constructor initializes the data fields. Furniture(int avx, int avy, int w, int h, int c) { super(avx,avy,0,0,1.0,0,0,0); mywidth = w ; myheight = h ; mycolor = c ; } // The display() function simply draws the Avatar object. // The move() function updates the Avatar object's state. void display() { // Draw the avatar. push(); // STUDENT *MUST* use push() & translate first in display(). translate(myx, myy); if (myrot != 0.0) { rotate(radians(myrot)); } if (myscale != 1.0) { scale(myscale); } fill(mycolor); stroke(mycolor); strokeWeight(1); rect(0,0,mywidth,myheight); // 0,0 is the center of the object. pop(); // STUDENT *MUST* use pop() last in display(). } // The move() function updates the Avatar object's state. // Furniture is immobile, so move() does nothing. void move() { } int [] getBoundingBox() { int [] result = new int[4]; result[0] = myx-mywidth/2 ; // left extreme of rect result[1] = myy - myheight/2; // top of rect result[2] = myx + mywidth/2; // right of rect result[3] = myy + myheight/2; // bottom of rect return result ; } } /** * Paddle extends class Furniture into a mobile, rotating rectangle. **/ class Paddle extends Furniture { // Call base class constructor to initialize its fields, // then initialize fields added by this class (none presently), // and let limits on off-screen excursions. Paddle(int avx, int avy, int w, int h, float rotatespeed, int c) { super(avx, avy, w, h, c); rotspeed = rotatespeed ; xlimitright = 2 * width ; ylimitbottom = 2 * height ; xlimitleft = - width ; // one width or height off the display ylimittop = - height ; // in either direction } void move() { myrot += rotspeed ; while (myrot >= 360) { myrot -= 360 ; } while (myrot < 0) { myrot += 360; } super.move(); // in case it is ever implemented } int [] getBoundingBox() { float rads = radians(myrot); // radians float radius = max(mywidth, myheight)/2; int tiltedx = abs(round(cos(rads) * radius)) ; int tiltedy = abs(round(sin(rads) * radius)) ; int [] result = new int[4]; result[0] = myx-tiltedx ; // left extreme of left arm result[1] = myy - tiltedy; // top of hat result[2] = myx + tiltedx; // max of right leg & arm result[3] = myy + tiltedy; // bottom of legs return result ; } }