/************************************************************ /* Authors: Dr. Parson & (replace this parenthetical comment with STUDENT NAME) /* PShapeRecursive3D is a spring 2019 sabbatical update to avatarClassIn3DRoom below. /* Creation Date: 11/14/2016 /* Due Date: 10/13/2018 /* Course: CSC220 Object-Oriented Multimedia Programming /* Professor Name: Dr. Parson /* Assignment: 2. /* Sketch name: avatarClassIn3DRoom, enhanced from avatarClassIn2_5DRoom. /* avatarClassIn3DRoom makes all Avatar-derived classes 3D. /* avatarClassIn2_5DRoom made the Professor Avatar 3D. /* avatarClassIn2DRoom added a 3D-camera() point-of-view (POV) to the 2D Avatar world of /* Assigment 1, with commands to move the camera() and rotate the world. /* SUMMARY OF STUDENT REQUIREMENTS: /* Save a working copy of this sketch as avatarClassIn3DRoom WITH YOUR NAME at the top. /* 1. 50% Completely replace my Professor class with your class that uses 3D boxes & spheres; /* it may also use some 2D shapes. Surfaces must adjoin; you could have a free-floating /* body part like a halo, but generally the Avatar must be a contiguous 3D image. /* I will deduct at least 10 points for a random collection of shapes that don't /* look like anything. Make sure to document your class with comments. /* getBoundingBox() now returns 6 values per comments in interface Avatar. /* 2. 25% Rewrite function makeCustomPShape() to make your own custom 3D PShape used by /* my existing class VectorAvatar. Build your PShape using the vertex() function /* on one or more planar faces of the PShape. /* I will award 10% bonus points if your code successfully textures a PShape planar face. /* HINT: To texture, make an individual planar PShape 2D instead of 3D -- no Z coordinates -- /* and use 3D translations & rotations to put it into place in a GROUP, /* similar to my sketch makeCustomPShape(). SEE also: /* http://faculty.kutztown.edu/parson/spring2017/LittleBoxes2017Parson.txt /* http://faculty.kutztown.edu/parson/spring2017/Window250x250.png /* 3. 15% Modify VectorAvatar.getBoundingBox() to enclose your displayed PShape /* correctly. Other enhancements to VectorAvatar are optional. /* 4. 10% Change any constructor calls in setup() to match your constructor. /* Do not change classes Furniture or Paddle for this assignment. /* Test it and make sure it works. /* *********************************************************/ /* KEYBOARD COMMANDS: /* 'b' toggles display of bounding boxes for debugging, initially on /* 'f' toggles freezing of display in draw() off / on. /* 'v' toggles isImmobile to inhibit/enable calls to Avatar.move(); /* 'm' toggles issmear mode for no-erase painting. /* 'k' toggles stroke on the shapes /* '~' applies shuffle() to each Avatar object, repositioning the mobile ones. /* '!' applies forceshuffle() to each Avatar object, repositioning all of them. /* 'p' sets perspective projection; 'o' sets orthographic * 'u' when held down moves camera up in Z direction slowly * 'U' when held down moves camera up in Z direction quickly * 'd' when held down moves camera down in Z direction slowly * 'D' when held down moves camera down in Z direction quickly * 'n' when held down moves camera up in Y direction slowly * 'N' when held down moves camera up in Y direction quickly * 's' when held down moves camera down in Y direction slowly * 'S' when held down moves camera down in Y direction quickly * 'e' when held down moves camera right in X direction slowly * 'E' when held down moves camera right in X direction quickly * 'w' when held down moves camera left in X direction slowly * 'W' when held down moves camera left in X direction quickly * 'x' when held down rotates image positive degrees around x * 'X' when held down rotates image negative degrees around x * 'y' when held down rotates image positive degrees around y * 'Y' when held down rotates image negative degrees around y * 'z' when held down rotates image positive degrees around z * 'Z' when held down rotates image negative degrees around z * 'R' resets to original camera point of view * SPACE BAR held down moves camera x,y to mouseX*2-width, mouseY*2-height */ // GLOBALS ADDED FOR 3D FURNITURE: int minimumZ, maximumZ ; // initialized in setup. // Assignment 1 GLOBAL VARIABLES are 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 = 0 ; // Wraps from 255 back to 0. boolean showBoundingBox = true ; // toggle with 'b' key boolean isFrozen = false ; // toggle with 'f' key to freeze display boolean isImmobile = false ; // toggle with 'f' key to freeze display // Added 9/15/2018 for camera manipulation: float xeye, yeye, zeye ; // locations of the camera's eye in 3D space // Next 3 variables rotate the world from the camera's point of view. float worldxrotate = 0.0, worldyrotate = 0.0, worldzrotate = 0.0 ; // Some basic symbolic constants. final float degree = radians(1.0), around = radians(360.0); boolean isortho = false ; // 'o' sets to true, 'p' to false for perspective boolean issmear = false ; boolean isstroke = true ; void setup() { // setup() runs once when the sketch starts, initializes sketch state. // size(1000, 800, P3D); fullScreen(P3D); background(backgroundColor); maximumZ = height / 2 ; // front of the scene minimumZ = - height / 2 ; // back of the scene avatars = new Avatar [ 5 ] ; // 1 3D vector avatar for now. avatars[0] = new VectorAvatar(width/2,height/2, 0, 3, 4, round(random(2,5)), 0.5, 600, 600); avatars[1] = new VectorAvatar(width/3,height/3, 0, 3, 4, round(random(2,5)), 0.5, 600, 500); avatars[2] = new VectorAvatar(2*width/3,height/3, 0, 3, 4, round(random(2,5)), 0.5, 600, 250); avatars[3] = new VectorAvatar(width/3,2*height/3, 0, 3, 4, round(random(2,5)), 0.5, 600, 100); avatars[4] = new VectorAvatar(2*width/3,2*height/3, 0, 3, 4, round(random(2,5)), 0.5, 600, 50); rectMode(CENTER); // I make them CENTER by default. rectMode is otherwise CORNER. ellipseMode(CENTER); imageMode(CENTER); shapeMode(CENTER); textAlign(CENTER, CENTER); // Added 9/15/2018, put the camera above the middle of the scene: xeye = width / 2 ; yeye = height / 2 ; zeye = (height*2) /* / tan(PI*30.0 / 180.0) */ ; } void draw() { // draw() is run once every frameRate, every 60th of a sec by default. if (isFrozen) { return ; } directionalLight(200, 200, 200, 0, 0, -1); ambientLight(55, 55, 55); lightSpecular(55,55,55); //shininess(4.0); if (isortho) { ortho(); } else { perspective(); } if (! issmear) { background(backgroundColor); // This erases the previous frame. } else { // EXPERIMENTAL: /* fill(backgroundColor, backgroundColor, backgroundColor, 1); noStroke(); rectMode(CORNER); shapeMode(CENTER); // rect(0,0,width,height); pushMatrix() ; translate(width/2, height/2, signum(zeye-minimumZ) * height*2); scale(4, 4, 1); box(width*4); popMatrix(); */ } // END EXPERIMENTAL */ rectMode(CENTER); ellipseMode(CENTER); imageMode(CENTER); shapeMode(CENTER); textAlign(CENTER, CENTER); moveCameraRotateWorldKeys(); // CAMERA ADDITION 9/15/2018, holding key repeats its action // 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); if (! isImmobile) { 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(255); strokeWeight(1); /* THIS WAS THE 2D CODE 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]); } */ for (Avatar avt : avatars) { // For testing bounding box int [] bb = avt.getBoundingBox(); line(bb[0], bb[1], bb[2], bb[3], bb[1], bb[2]); // across back top line(bb[0], bb[4], bb[2], bb[3], bb[4], bb[2]); // across back bottom line(bb[0], bb[1], bb[5], bb[3], bb[1], bb[5]); // across front top line(bb[0], bb[4], bb[5], bb[3], bb[4], bb[5]); // across front bottom line(bb[0], bb[1], bb[2], bb[0], bb[4], bb[2]); // down back left line(bb[3], bb[1], bb[2], bb[3], bb[4], bb[2]); // down back right line(bb[0], bb[1], bb[5], bb[0], bb[4], bb[5]); // down front left line(bb[3], bb[1], bb[5], bb[3], bb[4], bb[5]); // down front right line(bb[0], bb[1], bb[2], bb[0], bb[1], bb[5]); // into top left line(bb[3], bb[1], bb[2], bb[3], bb[1], bb[5]); // into top right line(bb[0], bb[4], bb[2], bb[0], bb[4], bb[5]); // into bottom left line(bb[3], bb[4], bb[2], bb[3], bb[4], bb[5]); // into bottom right } } 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 == 'v') { isImmobile = ! isImmobile ; } else if (key == 'k') { isstroke = ! isstroke ; } else if (key == '~') { for (int i = 0 ; i < avatars.length ; i++) { avatars[i].shuffle(); } } else if (key == '!') { for (Avatar a : avatars) { a.forceshuffle(); } } else if (key == 'R') { // Reset POV to starting point. xeye = width / 2 ; yeye = height / 2 ; zeye = (height*2) /* / tan(PI*30.0 / 180.0) */ ; worldxrotate = worldyrotate = worldzrotate = 0.0 ; } else if (key == 'o') { isortho = true ; } else if (key == 'p') { isortho = false ; } else if (key == 'm') { issmear = ! issmear ; } } /** 3D 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[3] // bb1_left is right of bb2_right || bb1[1] > bb2[4] // bb1_top is below bb2_bottom || bb1[2] > bb2[5] // bb1_back is front of bb2_front || bb2[0] > bb1[3] // bb2_left is right of bb1_right || bb2[1] > bb1[4] // bb2_top is below bb1_bottom || bb2[2] > bb1[5] // bb2_back is front of bb1_front ) { return false ; } // In this case one contains the other or they overlap. return true ; } /** Return 1 for non-negative num, -1 for negative. **/ int signum(float num) { return ((num >= 0) ? 1 : -1); } // Added 9/15/2018 to move camera and rotate world when these keys are held down. void moveCameraRotateWorldKeys() { if (keyPressed) { if (key == 'u') { zeye += 10 ; // println("DEBUG u " + zeye + ", minZ: " + minimumZ + ", maxZ: " + maximumZ); } else if (key == 'U') { zeye += 100 ; // println("DEBUG U " + zeye + ", minZ: " + minimumZ + ", maxZ: " + maximumZ); } else if (key == 'd') { zeye -= 10 ; // println("DEBUG d " + zeye + ", minZ: " + minimumZ + ", maxZ: " + maximumZ); } else if (key == 'D') { zeye -= 100 ; // println("DEBUG D " + zeye + ", minZ: " + minimumZ + ", maxZ: " + maximumZ); } else if (key == 'n') { yeye -= 1 ; } else if (key == 'N') { yeye -= 10 ; } else if (key == 's') { yeye += 1 ; } else if (key == 'S') { yeye += 10 ; } else if (key == 'w') { xeye -= 1 ; } else if (key == 'W') { xeye -= 10 ; } else if (key == 'e') { xeye += 1 ; } else if (key == 'E') { xeye += 10 ; } else if (key == 'x') { worldxrotate += degree ; if (worldxrotate >= around) { worldxrotate = 0 ; } } else if (key == 'X') { worldxrotate -= degree ; if (worldxrotate < -around) { worldxrotate = 0 ; } } else if (key == 'y') { worldyrotate += degree ; if (worldyrotate >= around) { worldyrotate = 0 ; } } else if (key == 'Y') { worldyrotate -= degree ; if (worldyrotate < -around) { worldyrotate = 0 ; } } else if (key == 'z') { worldzrotate += degree ; if (worldzrotate >= around) { worldzrotate = 0 ; } } else if (key == 'Z') { worldzrotate -= degree ; if (worldzrotate < -around) { worldzrotate = 0 ; } } else if (mousePressed && key == ' ') { xeye = mouseX ; yeye = mouseY ; } } // Make sure 6th parameter -- focus in the Z direction -- is far, far away // towards the horizon. Otherwise, ortho() does not work. camera(xeye, yeye, zeye, xeye, yeye, zeye-signum(zeye-minimumZ)*maximumZ*2 , 0,1,0); if (worldxrotate != 0 || worldyrotate != 0 || worldzrotate != 0) { translate(width/2, height/2, 0); // rotate from the middle of the world if (worldxrotate != 0) { rotateX(worldxrotate); } if (worldyrotate != 0) { rotateY(worldyrotate); } if (worldzrotate != 0) { rotateZ(worldzrotate); } translate(-width/2, -height/2, 0); // Apply the inverse of the above translate. // Do not use pushMatrix()-popMatrix() instead of the inverse translate, // because popMatrix() would discard the rotations. } } /** * 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. Added 3D getBoundingBox() for assn2. **/ 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: pushMatrix(); translate(x, y); and ends like this: popMatrix(); 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 6 integers where elements * [0],[1],[2] have the minimum X,Y,Z coordinates respectively, and * [3],[4],[5] have the maximum X,Y,Z coordinates respectively. * This function always returns a cuboid 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(); /** Return the Z coordinate of this avatar's center. **/ int getZ(); /** Randomize parts of a *mobile* object's space, including x,y,z location. **/ void shuffle() ; /** Randomize parts of *every* object's space, including x,y,z 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, myz ; // x,y,z 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. protected float speedZ ; // speed of motion, negative for away from front. float myrotZ = 0.0 ; // subclasses may rotate & scale in other dimensions float rotZspeed = 0.0, sclspeed = 0.0 ; // subclasses may change myscale, myrotZ 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 protected int zlimitmin = minimumZ ; // default for drawing protected int zlimitmax = maximumZ ; // default for drawing // The constructor initializes the data fields. CollisionDetector(int avx, int avy, int avz, float spdx, float spdy, float spdz, float avscale, float scalespeed, float rotation, float rotatespeed) { myx = avx ; myy = avy ; myz = avz ; speedX = spdx ; speedY = spdy ; speedZ = spdz ; myscale = avscale ; sclspeed = scalespeed ; myrotZ = rotation ; rotZspeed = 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)); myz = round(random(minimumZ/4, maximumZ/4)); // don't go too far out } int getX() { return myx ; } int getY() { return myy ; } int getZ() { return myz ; } // 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[3]) { // my left side is within them, move to the right speedX = abs(speedX); myx += 2*speedX ; // jump away a little extra } else if (mine[3] >= theirs[0] && mine[3] <= theirs[3]) { // 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[4]) { speedY = abs(speedY); // my top, send it down myy += 2*speedY ; } else if (mine[4] >= theirs[1] && mine[4] <= theirs[4]) { speedY = - abs(speedY); // my bottom, send it up myy += 2*speedY ; } } // Z test added for assignment 2 3D. mine = getBoundingBox(); if (overlap(this,a)) { // Do equivalent check for vertical overlap. if (mine[2] >= theirs[2] && mine[2] <= theirs[5]) { speedZ = abs(speedZ); myz += 2*speedZ ; } else if (mine[5] >= theirs[2] && mine[5] <= theirs[5]) { speedZ = - abs(speedZ); myz += 2*speedZ ; } } } } // 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); } if (myz <= zlimitmin && speedZ < 0) { speedZ = - speedZ ; myz = zlimitmin + 1 ; // if (myscale >= 1) println("DEBUG WENT OFF BACK " + speedY); } if (myz >= zlimitmax && speedZ > 0) { speedZ = - speedZ ; myz = zlimitmax - 1 ; // if (myscale >= 1) println("DEBUG WENT OFF FRONT " + speedY); } } }