Loading...
Logo
Processing Forum
Hello everyone. Im having trouble, as you can see. You see, I have a code that makes a single sphere drop down, and bounce back up, until finally stopping. Here:


float x = 750;   // x location of square
float y = 0;     // y location of square
float z = 0;     // z location of square
float speed = 0;   // speed of square


// A new variable, for gravity (i.e. acceleration).   
// We use a relatively small number (0.1) because this accelerations accumulates over time, increasing the speed.   
// Try changing this number to 2.0 and see what happens.
float gravity = 0.1;  

void setup() {
  size(1500,700, P3D);
  
}
//attempting to make a sphere in the location of each mouse press and make it fall down

void draw() {
 background(0);
 lights();
    noStroke();
translate(x,y,z);
sphere(10);

  

  // Add speed to location.
  y = y + speed;
  
  // Add gravity to speed.
  speed = speed + gravity;
  
  // If square reaches the bottom
  // Reverse speed
  if (y > height) {
    // Multiplying by -0.95 instead of -1 slows the square down each time it bounces (by decreasing speed).  
    // This is known as a "dampening" effect and is a more realistic simulation of the real world (without it, a ball would bounce forever).
    speed = speed * -0.95;  
  }
}

What I am trying to do is be able to move my mouse on the window, click, and a sphere would appear where I clicked, and dropped down like the rest. For this, I would like advice on two things. How do I create multiple objects (or in this case, spheres) on the window? And how do I find away to make a sphere on the mouseX and mouseY coordinates, but have it still drop down? My biggest trouble with that is keeping the x and y values it has right now, and start it off with that instead.

Thanks.

Replies(4)

hi, :)

Copy code
  1. ArrayList<Ball> balls = new ArrayList<Ball>();
  2. float gravity = 0.1;  




  3. void setup() {
  4.   size(1500, 700, P3D);
  5.   // start with one ball
  6.   balls.add( new Ball(width/2, 0, 0));
  7.   background(0);
  8. }
  9. //attempting to make a sphere in the location of each mouse press and make it fall down

  10. void draw() {
  11.   background(0);
  12.   lights();
  13.   for (Ball b:balls) {
  14.     b.update();
  15.     b.display();
  16.   }
  17. }

  18. void mousePressed() {
  19.   balls.add(new Ball( mouseX, mouseY, 0));
  20. }



  21. class Ball {
  22.   float x;
  23.   float y;   
  24.   float z;   
  25.   float speed;   


  26.   // a constructor:
  27.   Ball(float _x, float _y, float _z) {
  28.     x     = _x;
  29.     y     = _y;
  30.     z     = _z;
  31.     speed = 0;
  32.   }

  33.   void display() {
  34.     noStroke();
  35.     pushMatrix();
  36.     translate(x, y, z);
  37.     sphere(10);
  38.     popMatrix();
  39.   }

  40.   void update() {
  41.     y = y + speed;
  42.     speed = speed + gravity;
  43.     
  44.     if (y > height) {
  45.       speed = speed * -0.95;
  46.     }
  47.   }
  48. }

Got a bit side tracked and I see v.k. got here first.

Since I have created a solution I will post it anyway. Even though I created the code independantly of v.k. you will see that the solutions are very similar - obviously great minds think alike . I did take the liberty of modifying the update to give a more ealistic bounce and have balls of different sizes.

Copy code
  1. float x = 750;   // x location of square
  2. float y = 0;     // y location of square
  3. float z = 0;     // z location of square
  4. float speed = 0;   // speed of square
  5. int nbrBalls = 5;

  6. float gravity = 0.1; 
  7. Ball[] balls;

  8. void setup() {
  9.   size(1500, 700, P3D);
  10.   balls = new Ball[nbrBalls];
  11.   for (int i = 0; i < balls.length; i++) {
  12.     balls[i] = new Ball(random(30, width - 30), random(10, 100), 0, random(10, 20));
  13.   }
  14. }

  15. void draw() {
  16.   background(0);
  17.   lights();
  18.   noStroke();
  19.   for (int i = 0; i < balls.length; i++) {
  20.     balls[i].update();
  21.     balls[i].display();
  22.   }
  23. }

  24. class Ball {
  25.   float x, y, z;
  26.   float size;
  27.   float speed = 0;
  28.   float accel = 0.1;

  29.   Ball(float px, float py, float pz, float psize) {
  30.     x = px;
  31.     y = py;
  32.     z = pz;
  33.     size = psize;
  34.     accel = gravity;
  35.   }

  36.   void update() {
  37.     // Going down and ready to bounce?
  38.     if (y > height-size && accel > 0) {
  39.       speed *= 0.90;
  40.       accel *= -1;
  41.       println("Bounce " + accel);
  42.     }
  43.     // Going up and ready to go down
  44.     if (speed < 0 && accel < 0) {
  45.       speed = 0;
  46.       accel *= -1;
  47.     }
  48.     speed += accel;
  49.     if (accel >= 0)
  50.       y += speed;
  51.     else
  52.       y -= speed;
  53.   }

  54.   void display() {
  55.     pushMatrix();
  56.     translate(x, y, z);
  57.     sphere(size);
  58.     popMatrix();
  59.   }
  60. }

Thanks, both of you.

V.K. could you explain what you did that was different? As you can see, Im a bit of a newbie.
Well besides the improvements in animation that quarks has added in update(), the main difference is the use of ArrayList instead of an Array. An ArrayList can grow and shrink easier than an array,   I used it so you can add balls with mouse click, of course that could be done with an array also, but i prefer this way. Have a look in this article of wiki. And feel free to ask further if you like. Quarks, that's an honour being compared with you by yourself :)  I don't deserve.