Redo the draw again
in
Programming Questions
•
3 years ago
I confuse how to put a function to a mouse click so if i click the mouse it will start produce the ball and checking the collision again. I tried timer function but it didnt work. Thank you so much before it.
here's my code sorry for the comment, just ignore it
Cheers
- /*Designed
- ~~~~~~~~
- Shan Markus 07/09/2010
- second modified 03/10/2010
- Modified
- ~~~~~~~~
- */
- // ---------------------------------------------------------------------------- Initialisation
- // Global (i.e. PApplet-wide) constants and variables
- float xPosOut = width/16; // xposition for outside canvas
- float yPosOut = height/16; // yposition for outside canvas
- float xPosIn = xPosOut + 100; // xposition for inside canvas
- float yPosIn = yPosOut + 100; // yposition for inside canvas
- float xOut = width/1.15; // width for outside canvas
- float yOut = height/1.2; // height for outside canvas
- float xIn = xOut - 200; // width for inside canvas
- float yIn = yOut - 200; // height for inside canvas
- float xspeed = 2.8; // Speed of the shape
- float yspeed = 2.2; // Speed of the shape
- int xdirection = 1; // Left or Right
- int ydirection = 1; // Top to Bottom
- ArrayList smallballs;
- ArrayList testsmallballs;
- int ballWidth = 15;
- PFont fontlifedeath;
- int x =120; //x-coordinate of ellipse moving around sketch edge.
- int y =115; //y-coordinate of ellipse moving around sketch edge.
- int speed = 10;
- int state;
- Timer timer;
- Ball[] balls = {
- new Ball(900, 600, 60),
- new Ball(700, 400, 60)
- };
- PVector[] vels = {
- new PVector(random(-5.0,5.0), random(-5.0,5.0)),
- new PVector(random(-5.0,5.0), random(-5.0,5.0))
- };
- // ----------------------------------------------------------------------------- Setup
- void setup() {
- background(0);
- int x = 1500;
- int y = 800;
- size(x,y);
- smallballs = new ArrayList();
- testsmallballs = new ArrayList();
- timer = new Timer(200);
- frameRate(50);
- }
- // ------------------------------------------------------------------------------ Main methods
- void draw() {
- Canvas canvas = new Canvas(); // initiate the canvas
- canvas.CanvasDraw(); // draw the canvas
- canvas.CanvasPattern(); // draw the canvas pattern
- fill(255);
- // Drawing a ball that collide each other (Adapted from Ira Greenberg)
- for (int i=0; i< 2; i++) {
- balls[i].x += vels[i].x;
- balls[i].y += vels[i].y;
- ellipse(balls[i].x, balls[i].y, balls[i].r*2, balls[i].r*2);
- checkBoundaryCollision(balls[i], vels[i]);
- }
- mycheckObjectCollison(balls, vels);
- }
- // check the ball collision with each others
- void mycheckObjectCollison(Ball[] b, PVector[] v){
- PVector bVect = new PVector();
- bVect.x = b[1].x - b[0].x;
- bVect.y = b[1].y - b[0].y;
- PVector stop = new PVector();
- stop.x = 0;
- stop.y = 0;
- float bVectMag = sqrt(bVect.x * bVect.x + bVect.y * bVect.y);
- if(bVectMag < b[0].r + b[1].r){
- for(int k =0; k < 2; k ++){
- v[k].x= 0;
- v[k].y= 0;
- }
- Canvas canvas = new Canvas(); // initiate the canvas
- canvas.CanvasDraw(); // draw the canvas
- canvas.CanvasPattern(); // draw the canvas pattern
- smallballs.add(new thisBall(b[0].x,b[0].y, ballWidth));
- testsmallballs.add(new thisBall(b[0].x,b[0].y, ballWidth));
- for (int i = smallballs.size()-1; i >= 0; i--) {
- thisBall smallball = (thisBall) smallballs.get(i);
- thisBall testball = (thisBall) testsmallballs.get(i);
- smallball.Rightmove();
- testball.Leftmove();
- smallball.display();
- testball.display();
- if (smallball.finished() || testball.finished()) {
- smallballs.remove(i);
- testsmallballs.remove(i);
- timer.start();
- }
- }
- }
- }
- // check the ball collision with the wall
- void checkBoundaryCollision(Ball ball, PVector vel) {
- if (ball.x > ((width/1.15))-ball.r) {
- ball.x = ((width/1.15))-ball.r;
- vel.x *= -1;
- }
- else if (ball.x < ((width/16) + 160)) {
- ball.x = ((width/16) + 160);
- vel.x *= -1;
- }
- else if (ball.y > ((height/1.2)-30)-ball.r) {
- ball.y = ((height/1.2)-30)-ball.r;
- vel.y *= -1;
- }
- else if (ball.y < ((height/16) + 175)) {
- ball.y = ((height/16) + 175);
- vel.y *= -1;
- }
- }
- //mouse interaction, not implemented yet
- void mousePressed () {
- }
- // ----------------------------------------------------------------------------------- Classes
- // Ball class
- class Ball {
- float x, y, r, m;
- // default Ball constructor
- Ball() {
- }
- Ball(float x, float y, float r) {
- this.x = x;
- this.y = y;
- this.r = r;
- m = r*.1;
- }
- }
- // Canvas class
- public class Canvas {
- int xPosOut = 90;
- int yPosOut = 70;
- int xPosIn = xPosOut + 100;
- int yPosIn = yPosOut + 100;
- float xOut = width/1.15;
- float yOut = height/1.2;
- float xIn = xOut - 200;
- float yIn = yOut - 200;
- // draw the canvas
- void CanvasDraw() {
- rect(xPosOut, yPosOut, xOut, yOut);
- //rect(xPosIn, yPosIn, xIn, yIn);
- }
- // draw canvas pattern
- void CanvasPattern() {
- // Canvas top pattern
- for(float i = 70; i <= 120.0; i+= 10){
- smooth();
- noFill();
- bezier(90, i, 300, 220, 500, 100, 750,i);
- noFill();
- }
- for(float i = 120; i <= 170.0; i+=10){
- bezier(90, i, 500, 100, 300, 40, 750, i);
- noFill();
- }
- for(float i = 70; i <= 120.0; i+= 10){
- smooth();
- noFill();
- bezier(750, i, 900, 50, 1000, 240, 1395, i);
- noFill();
- }
- for(float i = 120; i <= 170.0; i+=10){
- bezier(750, i, 800, 145, 1200, 10, 1395, i);
- noFill();
- }
- // Canvas Below pattern
- for(float i = 636; i <= 686.0; i+= 10){
- smooth();
- noFill();
- bezier(750, i, 1050, (636+150), 1250, 666, 1395,i);
- noFill();
- }
- for(float i = 686; i <= 746.0; i+=10){
- bezier(750, i, 1250, 666, 1050, 606, 1395, i);
- noFill();
- }
- for(float i = 636; i <= 686.0; i+= 10){
- smooth();
- noFill();
- bezier(90, i, 150, 616, 250, 806, 750,i);
- noFill();
- }
- for(float i = 686; i <= 746.0; i+=10){
- bezier(90, i, 50, 711,450, 576, 750, i);
- noFill();
- }
- // Canvas left pattern
- for(float i = 140; i <= 190.0; i+= 10){
- smooth();
- noFill();
- bezier(i, 70, 90, 310, 200, 500, i, 750);
- noFill();
- }
- for(float i = 90; i <= 140.0; i+=10){
- bezier(i, 70, 200, 400, 90, 310, i, 750);
- noFill();
- }
- // Canvas right pattern
- for(float i = 1290; i <= 1340; i+=10){
- bezier(i, 70, 1295, 310, 1405, 400, i, 750);
- }
- for(float i = 1340; i <= 1400; i+=10){
- bezier(i, 70, 1410, 500, 1295, 310, i, 750);
- }
- }
- }
- class thisBall {
- float x;
- float y;
- float speed;
- float gravity;
- float w;
- float life = 255;
- thisBall(float tempX, float tempY, float tempW) {
- x = tempX;
- y = tempY;
- w = tempW;
- speed = 0;
- gravity = 0.1;
- }
- void Rightmove() {
- // Add gravity to speed
- speed = speed + gravity;
- // Add speed to y location
- y = y + random(1,5);
- x = x + random(1,5);
- // If square reaches the bottom
- // Reverse speed
- if (y > 630) {
- // Dampening
- speed = speed * -1;
- y = 630;
- }else if (x > width/1.15){
- x = x - 2;
- } else if (x < (width/16)){
- x = x + 2;
- }
- }
- void Leftmove() {
- // Add gravity to speed
- speed = speed + gravity;
- // Add speed to y location
- y = y + random(1,5);
- x = x - random(1,5);
- // If square reaches the bottom
- // Reverse speed
- if (y > 630) {
- // Dampening
- speed = speed * -1;
- y = 630;
- } else if (x > width/1.15){
- x = x - 2;
- } else if (x < (width/16)){
- x = x + 2;
- }
- }
- boolean finished() {
- // Balls fade out
- life-=4;
- if (life < 0) {
- return true;
- } else {
- return false;
- }
- }
- void display() {
- // Display the circle
- fill(0,life);
- //stroke(0,life);
- ellipse(x,y,w,w);
- }
- }
1
