How can I make ball bounce from a platform to another?

edited November 2017 in Questions about Code

I want that my ball jump from platform A to platform B. If do not touch the platform, falls.

    boolean salta = false;
    boolean baixa = false;
    boolean esquerra = false;
    boolean dreta = false;
    boolean stop=false; 
    //PImage img;


    float cercleX = 50;
    float cercleY = 50;
    float gravetat = 0.5;
    float posx = 0;
    float posy = 50;
    float vy = -1;
    float bounce = -0.5;

    //color c = get(0,0);

    //////////////////////////////////////////////////////////////////////////////////////

    void setup() {
      size (1280, 720);
      smooth();
      noStroke();
      //img = loadImage("background1-720.png");
    }

    void draw() {
      background (255);
      //image(img, 0, 0);



      //posx++;

      frameRate (70);
      fill(0);
      rect(0, 400, 500, 100);
      rect(600, 400, 500, 100);

      fill(255, 0, 255);
      ellipse(posx, posy, 20, 20);

      if (!stop) {
        vy += gravetat;
        posy += vy;
      }


      if (posy > height - 330) {

        vy = bounce * abs(vy);

        if (abs(vy)<0.281) {
          vy=0.0;

          stop=true;
        }
      }

      // booleans 
      if (salta) {
        posy--;
        stop=false; 
        vy=-5;
      }

      if (baixa) {
        posy++;
      }

      if (esquerra) {
        posx--;
      }

      if (dreta) {
        posx++;
      }
    }

    void keyPressed() {
      if (keyCode == UP) {
        salta = true;
      } else if (keyCode == DOWN) {
        baixa = true;
      } else if (keyCode == LEFT) {
        esquerra = true;
      } else if (keyCode == RIGHT) {
        dreta = true;
      }
    }

    void keyReleased() {
      if (keyCode == UP) {
        salta = false;
      } else if (keyCode == DOWN) {
        baixa = false;
      } else if (keyCode == LEFT) {
        esquerra = false;
      } else if (keyCode == RIGHT) {
        dreta = false;
      }
    }



    //

Answers

  • On line 50, you always make the ball bounce back up if its y position is 330 from the bottom edge of the sketch. ALWAYS. You do not ALWAYS want this to happen - sometimes it should keep falling.

    Make the bouncing also depend on the ball's x position! HINT: What other conditions should you add to your conditional statement?

  • Maybe with the colour? But how can I say to the code that position is determined by colour?

  • Just use the X position. You know where the gap is, the start and the end of it, don't bounce if X is within the gap.

  • Okay, but imagine there are random platforms in y position and x position

  • Okay, but imagine you know the positions of those platforms too...

    (Checking the colour is a valid solution, yes. How would you do that?)

  • I do not know! I just think it's a way of doing it. But honestly I have no idea how ...

  • Well, work out where the ball will be at the end of the current step and the use get() to find the colour at this new position...

  • Hey there again! I created a void for the ball & the platform. I don't know how to make ball falls when it's not on the platform.. can someone help me please?

        boolean salta = false;
        boolean baixa = false;
        boolean esquerra = false;
        boolean dreta = false;
        boolean stop=false; 
        //PImage img;
    
    
        float cercleX = 50;
        float cercleY = 50;
        float gravetat = 0.5;
        float posx_a = 0; // posició pilota en x
        float posy_a = 50; // posició pilota en y
        float posx_b =100;
        float posy_b = 300;
        float vy = -4;
        //float vx  1;
        float bounce = -0.5;
    
        //color c = get(0,0);
    
        //////////////////////////////////////////////////////////////////////////////////////
    
        void setup() {
          size (1280, 720);
          smooth();
          noStroke();
          //img = loadImage("background1-720.png");
        }
    
        void draw() {
          background (255);
          //image(img, 0, 0);
    
          posx_a++;
    
          frameRate (70);
    
          pilota();
          plataforma();
        }
    
    
        void pilota() {
    
      fill(255, 0, 255);
      ellipse(posx_a, posy_a, 20, 20);
    
      if (!stop) {
        vy += gravetat;
        posy_a += vy;
      }
    
    
      if (posy_a > posy_b-10) {
    
        vy = bounce * abs(vy);
    
       // if (posy_a > posx_b -10) {
        //  posy_a = posy_b-100;
    
          if (abs(vy)<0.281) {
            vy=0.0;
    
            stop=true;
          }
        }
    
        // booleans 
        if (salta) {
          posy_a--;
          stop=false; 
          vy=-5;
        }
    
        if (baixa) {
          posy_a++;
        }
    
        if (esquerra) {
          posx_a--;
        }
    
        if (dreta) {
          posx_a++;
        }
      }
    
    
    
      void keyPressed() {
        if (keyCode == UP) {
          salta = true;
        } else if (keyCode == DOWN) {
          baixa = true;
        } else if (keyCode == LEFT) {
          esquerra = true;
        } else if (keyCode == RIGHT) {
          dreta = true;
        }
      }
    
      void keyReleased() {
        if (keyCode == UP) {
          salta = false;
        } else if (keyCode == DOWN) {
          baixa = false;
        } else if (keyCode == LEFT) {
          esquerra = false;
        } else if (keyCode == RIGHT) {
          dreta = false;
        }
      }
    
      void plataforma() {   fill(0);
          rect(posx_b, posy_b, 100, 100);
    
    }
    
  •  if ((!(posx_b == posx_a))&&(!(posy_b ==posy_a))) {//QUAN NO SON IGUALS LES POS X DE LA PILOTA I LA BARRA NI LA DE LES Y, LA PILOTA S'ENVA A VALL
        posx_a = (5000);
        posy_a = 6000;
    
Sign In or Register to comment.