//Michael Chang //Interactive Media 157A //Solution to INPUT assignment int begin; // Time the event/game begins boolean active; // Flag for event/game status boolean done; // Flag for "game over" BImage beginImage; // Start with this image BImage endImage; // End with this image BImage winImage; //BSound bounceSound; //BSound hitSound; //BSound floorSound; position lastMouse; vector mouseMotion; boolean mp=false; float conversionArc=180/PI; float padMod=15; ball[] ballList; int numberBalls=5000; int ballSize=2; boolean gameOver=false; boolean added=false; int gameOverTime=0; float bgColor=0; boolean firstTime=true; boolean attract=false; //int soundTimer=0; vector gravity; //gravity factor that affects all objects void setup() { size(200, 400); active = true; // Don't begin with action done = false; // The event/game has not finished ballList=new ball[numberBalls]; // bounceSound=loadSound("Pong-Public_D-145.wav"); // hitSound=loadSound("poise-xrikazen-77.wav"); // floorSound=loadSound("ayuigo-xrikazen-80.wav"); generateBalls(ballList); gravity=new vector(0,0,270,.6); lastMouse=new position(mouseX,mouseY); mouseMotion=new vector(0,0,0,0); // framerate(70); // noSmooth(); // smooth(); // background(0); } void loop() { background(0); // if(soundTimer<20) // soundTimer+=1; // else // { // soundTimer=0; // stop(bounceSound); // } if(gameOver) { gameOverTime+=1; if(gameOverTime>70) { active=false; // done=false; } } // active=true; // For testing purposes! if(active == true) { eventGame(); // Run the event/game // timer(); // Time the event/game added=false; } else { if(!firstTime) { if(!gameOver) { if(!added) { ballList=new ball[numberBalls]; generateBalls(ballList); // stop(bounceSound); } added=true; bgColor=0; winScreen(); // Show the "end" screen } else { if(!added) { ballList=new ball[numberBalls]; generateBalls(ballList); // stop(bounceSound); } added=true; bgColor=0; endScreen(); // Show the "end" screen } } else { if(!added) { ballList=new ball[numberBalls]; generateBalls(ballList); // stop(bounceSound); } gameOver=false; gameOverTime=0; added=true; bgColor=0; beginScreen(); // Show the "first" screen } } lastMouse.set(mouseX,mouseY); } void generateBalls(ball[] b) { for(int i=0;i begin + 10000) { active = false; done = true; } noStroke(); fill(255); rect(0, height-5, width, 5); fill(0); rect(0, height-5, (curTime-begin)/25.0, 5); } // Displays when the game/event begins void beginScreen() { } // Displays when the 10 seconds are over void endScreen() { } void winScreen() { } class ball { position location; vector velocity; boolean grounded; boolean ballHit; float radius; float bounceFactor; vector bounceVelocity; component[] compList; int cMod; ball(float x,float y,float r) { location=new position(x,y); velocity=new vector(location,90,5); radius=r; grounded=false; bounceVelocity=new vector(velocity); compList=new component[37]; buildComponents(); bounceFactor=.6; //any bounce less than .5 is ugly cMod=int(random(-15,100)); } void buildComponents() { float ang=0; for(int i=0;i0) // { // } if(dist(location.x,location.y,mouseX,mouseY)=0)) { vector mouseMod=new vector(mouseMotion.p,mouseMotion.a+random(-4,4),mouseMotion.m+random(-.5,.5)); velocity.m*=.5; velocity=velocity.add(mouseMod); // } //play(hitSound); ballHit=true; } else if(!grounded) { vector mouseMod=new vector(mouseMotion.p,mouseMotion.a+random(-4,4),mouseMotion.m+random(-.5,.5)); velocity.m*=.5; velocity=velocity.add(mouseMod); // } ballHit=true; } } velocity.a=normalizeHeading(velocity.a); location=location.displace(velocity.a,velocity.m); if(attract) { vector attractForce=new vector(mouseMotion.p,getHeading(velocity.p,mouseMotion.p),2+random(-.5,.5)); velocity=velocity.add(attractForce); } if(!collision()) { // if(!grounded) // { velocity.p=location; gravityPull(); // } } else { // if(!grounded) // { handleCollision(); velocity.p=location; // play(bounceSound,100,300); // } } // updateComponents(); // if(grounded) // location.y=height-radius; } void gravityPull() { // velocity.m+=gravity; velocity=velocity.add(gravity); } boolean collision() { if(location.y+(radius)>height-1) { // gameOver=true; bgColor+=20; return true; } if(location.x-radius<0) return true; if(location.x+radius>width) return true; return false; } void handleCollision() { if(location.y+(radius)>height-1) { if(velocity.m>1) { velocity.m*=bounceFactor; if(velocity.a>=270) velocity.a=360-velocity.a; else if(velocity.a<270&&velocity.a>180) velocity.a=360-velocity.a; // velocity=velocity.add(360-velocity.a,velocity.m*.5); } else { velocity.m=0; grounded=true; } } if(location.x-radius<0) { velocity.m*=bounceFactor; if(velocity.a<180&&velocity.a>=90) velocity.a=180-velocity.a; else if(velocity.a<270&&velocity.a>180) velocity.a=180-velocity.a; location.x=radius; } else if(location.x+radius>=width) { velocity.m*=bounceFactor; if(velocity.a>=0&&velocity.a<90) velocity.a=180-velocity.a; else if(velocity.a>=270&&velocity.a<360) velocity.a=180-velocity.a; location.x=width-radius; } velocity.a+=random(-1,1); // velocity.p.y=height-(radius); } void draw() { // noStroke(); // fill(200*velocity.m/3,150,250-55*velocity.m); if(velocity.a<50&&velocity.a>40) { // float a=abs(velocity.a-45); float a=abs(velocity.a-45)*10; float c=a*a+205-(velocity.p.y/(height)*180); stroke(c,c,cMod+c); } else { float c=205-(velocity.p.y/(height)*180); stroke(c,c,cMod+c); } // ellipse(location.x,location.y,radius,radius); point(location.x,location.y); // drawEllipseBall(); // drawCurveBall(); // drawIndicator(); // drawVector(velocity); // drawGravityVector(); // drawMotionVector(); } void drawEllipseBall() { push(); smooth(); noStroke(); colorMode(RGB,255,255,255); ellipseMode(CENTER_DIAMETER); if(!ballHit) fill(255); else fill(255,0,0); translate(velocity.p.x,velocity.p.y); rotate(radians(360-velocity.a)); // fill(255,0,0); // ellipse(radius,0,6,6); scale(1+(velocity.m/32),1-(velocity.m/40)); ellipse(0,0,radius*2,radius*2); pop(); } void drawCurveBall() { colorMode(RGB,255,255,255); stroke(0); // noStroke(); fill(0); beginShape(POLYGON); curveVertex(compList[0].displaceDistance.endPoint().x,compList[0].displaceDistance.endPoint().y); for(int i=0;iv.a) displaceDistance.m=50*(360-v.a)/distance.a; else displaceDistance.m=50*(distance.a)/(360-v.a); // displaceDistance.m=25.0*v.a/distance.a; // displaceDistance.m=v.m; // parentLocation=v.p; // location=parentLocation.displace(angle,distance); // distance } // void draw() // { // } } class position { float x,y; position(){} position(float tx,float ty) { x=(tx); y=(ty); } position(position p) { x=p.x; y=p.y; } position displace(float angle,float magnitude) { position newP=new position(x,y); newP.x+=(cos(radians(angle))*magnitude); newP.y-=(sin(radians(angle))*magnitude); return newP; } void set(float tx,float ty) { x=(tx); y=(ty); } } class vector { float a,m; position p; vector(){} vector(float tx,float ty,float angle,float magnitude) { p=new position(tx,ty); a=angle; m=magnitude; } vector(position pos,float angle,float magnitude) { p=new position(pos); a=angle; m=magnitude; } vector(vector v) { p=new position(v.p); a=v.a; m=v.m; } position endPoint() { return displace(p,a,m); } vector add(vector v) { /* float d=sin(degrees(a))*m; float displacement=d-v.m; position newEndpoint=new position(endPoint()); newEndpoint.y-=displacement/2; float newMagnitude=dist(p.x,p.y,newEndpoint.x,newEndpoint.y); float newAngle=getHeading(p,newEndpoint);*/ position newEnd=new position(endPoint().displace(v.a,v.m)); float newAngle=getHeading(p,newEnd); vector newVector=new vector(p,newAngle,dist(p.x,p.y,newEnd.x,newEnd.y)); return newVector; } vector add(float angle,float magnitude) { vector v=new vector(p,angle,magnitude); position newEnd=new position(endPoint().displace(v.a,v.m)); float newAngle=getHeading(p,newEnd); vector newVector=new vector(p,newAngle,dist(p.x,p.y,newEnd.x,newEnd.y)); return newVector; } vector sub(vector v) { return v; } void setStart(position np) { p=np; } void setEnd(position np) { a=getHeading(p,np); m=dist(p.x,p.y,np.x,np.y); } void setEnd(float px,float py) { position np=new position(px,py); a=getHeading(p,np); m=dist(p.x,p.y,np.x,np.y); } } position displace(position p,float angle,float magnitude) { position newP=new position(p); // angle=atan2(my-y,mx-x); // angle=-1*angle*180/PI; // if(my>y) // angle=360-(angle*-1); newP.x+=(cos(radians(angle))*magnitude); newP.y-=(sin(radians(angle))*magnitude); return newP; } float getHeading(position p1,position p2) //gets the absolute heading between one position and another relative to the first { if(p1.x==p2.x&&p1.y==p2.y) return 0; float angle=float(atan2(p2.y-p1.y,p2.x-p1.x)); angle=degrees(angle); if(p1.y>p2.y) angle*=-1; else angle=360-angle; return angle; } float normalizeHeading(float ang) { while(ang >= 360)ang -= 360; while(ang <= 0)ang += 360; return ang; } void drawVector(vector v) { colorMode(RGB,255,255,255); stroke(255); noFill(); ellipseMode(CENTER_DIAMETER); ellipse(v.p.x,v.p.y,5,5); position e=v.endPoint(); line(v.p.x,v.p.y,e.x,e.y); } void drawVector(vector v,int r,int g,int b) { colorMode(RGB,255,255,255); stroke(r,g,b); noFill(); ellipseMode(CENTER_DIAMETER); ellipse(v.p.x,v.p.y,5,5); position e=v.endPoint(); line(v.p.x,v.p.y,e.x,e.y); }