Newbie, a few simple questions. (mushrooms)

edited November 2013 in Questions about Code

Hello!

I am making an array of 20 mushrooms and I want them to collide.

Here is the code that draws the mushroom:

 //body

 rect(x-20, y+3,40,40);

ellipseMode(CENTER);

ellipse(x2, y2,radius2, radius2);

 //head

   fill(c);

  ellipseMode (CENTER);

   ellipse (x, y, radius, radius);

As you see, each mushroom ends with an ellipse (I wanted to make the bottom round and found it easier than an arc).

So I can make the code for the collisions with the heads work, but I want the bottom ellipse to colide also when it touches the head of another mushroom. The bottom ellipse has of course different coordinates (x2=x however) and a smaller radius.

  1. Second question

I want to make the mushrooms dissapear when clicked upon, but I am totally confused about how to do it.

Below is my code:

   //array declaration

   Mushroom[] mushrooms = new Mushroom[20];

   void setup() {
   size(600,600);
   smooth();
   //array initialization
    for (int i = 0; i < mushrooms.length; i++) { 


   mushrooms[i] = new Mushroom(i);   } 

    }


    void draw() {
    background(150, 255, 2);

    for (int i = 0; i < mushrooms.length; i++) { 


    mushrooms[i].move(); 
    mushrooms[i].display(); 
    mushrooms[i].collide();


  } 

}

    class Mushroom {
        color c;
        float x;
        float y;
        float x2;
        float y2;
        float xspeed;
        float yspeed;
        float radius;
        float radius2;
        int id;


    Mushroom(int i) {

      c = color(random(0,255), random(0,255), random(0,255)); 

    //where the mushrooms start at
     x=int(random(100, 500));
     y=int(random(100,500));
     radius = 60;
     radius2 = 40;
     id=i;
     x2 = x;
     y2 = y+40;
   //mushroom speed
     xspeed = random(-5,5); 

     yspeed = random(-5,5); 



}


  void display() {

  noStroke();

  //body
  rect(x-20, y+3,40,40);
  ellipseMode(CENTER);
  ellipse(x2, y2,radius2, radius2);

  //head
  fill(c);
  ellipseMode (CENTER);
  ellipse (x, y, radius, radius);
  //dots on head
  fill (255);
  ellipse (x+3, y, 5, 5);
  ellipse (x+6, y-12, 5, 5);
  ellipse (x-15, y+4,7,7);
  ellipse (x+10,y-3,7,7);
  ellipse (x+9, y-9, 7, 7);
  ellipse (x+3, y-4, 4, 4);
  ellipse (x-10,y+3,7,7);
  ellipse (x-9, y-9, 7, 7);
  ellipse (x-3, y+4, 4, 4);
  }

   void move() { 



    if (x+30 > width || x-30 < 0) { 


      xspeed = xspeed * (-1); 


    } 


    if (y+65 > height || y-25 < 0) { 


      yspeed = yspeed * (-1); 


    } 


    x = x + xspeed; 


    y = y + yspeed; 

    x2 = x2+xspeed;
    y2 = y2+yspeed;


  } 




  void collide() {
    for (int i = id + 1; i < 20; i++) {
      float distx = mushrooms[i].x+mushrooms[i].xspeed - x-xspeed;
      float disty = mushrooms[i].y+mushrooms[i].yspeed - y-yspeed;
      float distance = sqrt(distx*distx + disty*disty);
      float minDist = 60;



      if (distance < minDist) {    
        float angle = atan2(disty, distx);
        float sine = sin(angle);
        float cosine = cos(angle); 

        float temp1x = cosine * distx + sine * disty;
        float temp1y = cosine * disty - sine * distx;
        float ax = cosine * xspeed + sine * yspeed;
        float ay = cosine * yspeed - sine * xspeed;

        xspeed = ax;                 
        yspeed = ay;

        ax = cosine * mushrooms[i].xspeed + sine * mushrooms[i].yspeed;
        ay = cosine * mushrooms[i].yspeed - sine * mushrooms[i].xspeed;

        mushrooms[i].xspeed = ax;          // change of the movement vector of the other object 
        mushrooms[i].yspeed = ay;





      }

    }
  }
}

Answers

  • edited November 2013

    When posting code, highlight it and hit CTRL+K to format it! [-(

    Got a similar concept online if you wanna check it:
    http://studio.processingtogether.com/sp/pad/export/ro.9V7R1wu55fOiN/latest

  • Thanks for the formatting tip. I am sorry, I will edit it.

    I will take a look at it, thank you.

    But I want the whole thing to dissapear, not just the head.

  • edited November 2013

    You could use box2D for collisions. And for the whole thing to disappear you could just make color equal to the background color, or change the location to off screen.

  • To make a mushroom dissapear when clicked on, use the the variables [mouseX](and mouseY to get the coordinates of the mouse, and then check to see if you are clicking on a mushroom. You also may want to look at mouseClicked()as well

  • A mushroom is made of two circles. Checking if a click happens in a circle is simple: check if the dist() between the mouse click and the center of the circle is below the radius of the circle. Take a look at the examples shipped with Processing.

    Here, you have to do two checks, one for the head, one for the foot.

    Circle collision work the same: check if the distance between the two centers is below the sum of the radii.

Sign In or Register to comment.