how to do this game

edited January 2016 in Library Questions

Hi, im doing a project to university and i need some help , how i can collide the ellipses with the paddle that i draw and, how i can control an ellipse with mouse and she has to collide with the others and finally make a goal and their balls go out the goal and go back when she dont hit goal .

Their is the code, PLZ HELP ME

import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.effects.*; import ddf.minim.signals.*; import ddf.minim.spi.*; import ddf.minim.ugens.*;

AudioPlayer sound ; Minim minim; PImage background ; int totalBolas = 16; float contadorX = 10, y=10, passo=3; Bola[] Bolas = new Bola[totalBolas];

void setup() { size(800,400);

background = loadImage("pokemon1.jpg");

minim= new Minim(this); sound = minim.loadFile("106-the-road-to-viridian-city-from-palette.mp3"); sound.loop();

for (int i = 0; i < totalBolas; i++) { Bola bola = new Bola(); bola.x = random(width); bola.y = random(height); bola.vx = random(10) - 5; bola.vy = random(10) - 5; bola.raio = 14; Bolas[i] = bola; bola.cor = color(random(255), random(255), random(255)); } }

void draw() { background(background); fill(color(255, 0, 0)); for (int i = 0; i < totalBolas; i++) { Bolas[i].x += Bolas[i].vx; Bolas[i].y += Bolas[i].vy; checkwallcolison(Bolas[i]); Bolas[i].desenha(); } checkballcolision(); rectMode(CENTER); //rect(width/2,0,350,100); //rectMode(CENTER);

rectMode(CORNER); strokeWeight(1); fill (255,0,0); noStroke(); rect (contadorX,y+10,80,20); contadorX = contadorX+passo; if ((contadorX >= width-90) || (contadorX <=0)) passo = passo*(-1);

}

void checkballcolision() { for (int i = 0; i< totalBolas; i++) { for ( int j= i +1; j <totalBolas; j++) { float dx = Bolas[j].x - Bolas[i].x;//distancia de x float dy = Bolas[j].y - Bolas[i].y;//distancia de y float dist = sqrt(dxdx + dydy);//teorema de pitagoras if (dist<(Bolas[j].raio + Bolas[i].raio)) { //OS Circulos tocam-se float normalX = dx/dist; float normalY = dy/dist;

    //encontrar  ponto médio
    float midpointX = (Bolas[i].x + Bolas[j].x)/2;
    float midpointY = (Bolas[i].y + Bolas[j].y)/2;


    //bolas tocam-se e mexem-se
    Bolas[i].x = midpointX - Bolas[i].raio * normalX;
    Bolas[i].y = midpointY - Bolas[i].raio*  normalY;
    Bolas[j].x = midpointX + Bolas[i].raio * normalX;
    Bolas[j].y = midpointY + Bolas[i].raio*  normalY;

    float dVector = ( Bolas[i].vx-Bolas[j].vx) * normalX;
    dVector += (Bolas[i].vy - Bolas[j].vy) * normalY;

    float dvx = dVector * normalX;
    float dvy = dVector * normalY;

    Bolas[i].vx -= dvx;
    Bolas[i].vy -= dvy ;
    Bolas[j].vx += dvx;
    Bolas[j].vy += dvy;
  }
}

} } void checkwallcolison(Bola bola) {

if (bola.x < bola.raio) { bola.x = bola.raio; bola.vx *= -1 ; } if (bola.x > width - (bola.raio)) { bola.x = width - (bola.raio); bola.vx *= -1; }
//if (bola.y < bola.raio) { //bola.y = bola.raio; //bola.vy *= -1; //}
if (bola.y > height - (bola.raio)) { bola.y = height - (bola.raio); bola.vy *= -1; } }

class Bola { float x = 0; float y = 0; float vx = 0; float vy = 0; float raio= 5; color cor; void desenha() { fill(cor);
ellipse(x, y, raio * 2, raio * 2); } }

Answers

  • edited January 2016

    Go back

    Edit your post please.

    Format the code right:

    • Empty line before and after the sketch/ code in your post

    • Highlight the code

    • Click the small C with the mouse

    Looks better:

    if (bola.x < bola.raio) { 
      bola.x = bola.raio; 
      bola.vx *= -1 ;
    } 
    if (bola.x > width - (bola.raio)) { 
      bola.x = width - (bola.raio); 
      bola.vx *= -1;
    }
    
  • you have a lot of different questions!!!

    she has to collide with the others

    do you mean one ball with the other balls?

    see here :

    https://www.processing.org/examples/bouncybubbles.html

    just remove the gravity from the example

    how i can collide the ellipses with the paddle that i draw

    check the ellipse against the paddle :

    you need to check each ball against the paddle

    so do it in the for-loop:

    when ball hits paddle reflect ball

    when ball misses paddle : ball dies

    if(Bolas[i].x>contadorX  && Bolas[i].x<contadorX+80  && 
    Bolas[i].y<40) 
        // reflect 
        else 
        //ball dies ; 
    

    how i can control an ellipse with mouse

    what!?

    you want to control the paddle, not the mouse, right?

    finally make a goal and their balls go out the goal and go back when she dont hit goal

    err.... wait....

    they go out when they miss the paddle, right?

    And then the score gets decreased (because it's a failure).

    ok, and when balls hits goal, score gets increased (because it's a success).

    The goal is similar to paddle with if (see above). But the goal does not move. Or it could move as well ;-)

  • // ball
    int x = 40; 
    int y = 40;
    
    // ball's speed
    int xs = 3; 
    int ys = 3;
    
    void setup() { 
      size(600, 600);
      rectMode(CENTER); //!!!!!!!!!!!!!!!!!!!!!!!!
    }
    
    void draw() { 
      background(0, 200, 0); 
    
      // -----------
    
      //Paddle
      rect(mouseX, height - 20, 100, 10);  
    
      ellipse(x, y, 20, 20); //Ball
    
      // -----------
    
      // move ball 
      x+=xs;
      y+=ys;
    
      // -----------
    
      //if x is touching the left of the screen
      if (x < 10) { 
        xs = xs * -1;
      } 
    
      //if x is touching the right of the screen
      if (x > width-20) { 
        xs = xs * -1;
      } 
    
      //if y is touching the top of the screen
      if (y < 10) { 
        ys = ys * -1;
      } 
    
      // if the ball is on the paddle's y axis
      //if the ball is on the paddle's x axis
      if (y > height - 35 ) { 
        if (x > mouseX - 60 && x < mouseX + 60) { 
          ys = abs(ys) * - 1;
        }
      }
    }
    //
    
  • your sketch

    import ddf.minim.*; 
    import ddf.minim.analysis.*; 
    import ddf.minim.effects.*; 
    import ddf.minim.signals.*; 
    import ddf.minim.spi.*; 
    import ddf.minim.ugens.*;
    
    AudioPlayer sound ; 
    Minim minim; 
    PImage background ; 
    int totalBolas = 16; 
    
    float contadorX = 10, y=10, passo=3; 
    
    Bola[] Bolas = new Bola[totalBolas];
    
    void setup() { 
      size(800, 400);
    
      //  background = loadImage("pokemon1.jpg");
    
      minim= new Minim(this); 
      //sound = minim.loadFile("106-the-road-to-viridian-city-from-palette.mp3"); 
      //sound.loop();
    
      for (int i = 0; i < totalBolas; i++) { 
        Bola bola = new Bola(); 
        bola.x = random(width); 
        bola.y = random(height); 
        bola.vx = random(10) - 5; 
        bola.vy = random(10) - 5; 
        bola.raio = 14; 
        Bolas[i] = bola; 
        bola.cor = color(random(255), random(255), random(255));
      }
    }
    
    void draw() { 
      //background(background);
      background(0);
      fill(color(255, 0, 0)); 
      for (int i = 0; i < totalBolas; i++) { 
        Bolas[i].x += Bolas[i].vx; 
        Bolas[i].y += Bolas[i].vy; 
        checkwallcolison(Bolas[i]); 
        Bolas[i].desenha();
      } 
      checkballcolision(); 
      rectMode(CENTER); //rect(width/2,0,350,100); //rectMode(CENTER);
    
      rectMode(CORNER); 
      strokeWeight(1); 
      fill (255, 0, 0); 
      noStroke(); 
      rect (contadorX, y+10, 80, 20); 
      contadorX = contadorX+passo; 
      if ((contadorX >= width-90) || (contadorX <=0)) passo = passo*(-1);
    }
    
    void checkballcolision() { 
      for (int i = 0; i< totalBolas; i++) { 
        for ( int j= i +1; j <totalBolas; j++) { 
          float dx = Bolas[j].x - Bolas[i].x;
          //distancia de x
          float dy = Bolas[j].y - Bolas[i].y;
          //distancia de y 
    
          //teorema de pitagoras
          float dist = sqrt(dx*dx + dy*dy); 
    
          if (dist<(Bolas[j].raio + Bolas[i].raio)) {
    
            //OS Circulos tocam-se 
            float normalX = dx/dist; 
            float normalY = dy/dist;
    
    
            //encontrar  ponto médio
            float midpointX = (Bolas[i].x + Bolas[j].x)/2;
            float midpointY = (Bolas[i].y + Bolas[j].y)/2;
    
    
            //bolas tocam-se e mexem-se
            Bolas[i].x = midpointX - Bolas[i].raio * normalX;
            Bolas[i].y = midpointY - Bolas[i].raio*  normalY;
            Bolas[j].x = midpointX + Bolas[i].raio * normalX;
            Bolas[j].y = midpointY + Bolas[i].raio*  normalY;
    
            float dVector = ( Bolas[i].vx-Bolas[j].vx) * normalX;
            dVector += (Bolas[i].vy - Bolas[j].vy) * normalY;
    
            float dvx = dVector * normalX;
            float dvy = dVector * normalY;
    
            Bolas[i].vx -= dvx;
            Bolas[i].vy -= dvy ;
            Bolas[j].vx += dvx;
            Bolas[j].vy += dvy;
          }
        }
      }
    } 
    
    void checkwallcolison(Bola bola) {
    
      if (bola.x < bola.raio) { 
        bola.x = bola.raio; 
        bola.vx *= -1 ;
      } 
      if (bola.x > width - (bola.raio)) { 
        bola.x = width - (bola.raio); 
        bola.vx *= -1;
      }
      //if (bola.y < bola.raio) { //bola.y = bola.raio; //bola.vy *= -1; //}
      if (bola.y > height - (bola.raio)) { 
        bola.y = height - (bola.raio); 
        bola.vy *= -1;
      }
    }
    
    // ============================================
    
    class Bola { 
      float x = 0; 
      float y = 0; 
      float vx = 0; 
      float vy = 0; 
      float raio= 5; 
      color cor; 
      void desenha() { 
        fill(cor);
        ellipse(x, y, raio * 2, raio * 2);
      }
    }
    //
    
Sign In or Register to comment.