# How to make repeating 3D ball on Y-coordinate?

**URL:** <https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435>\
**Category:** Coding Questions\
**Created:** [May 30, 2020, 8:23pm UTC](https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435 "2020-05-30T20:23:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![processingart](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@processingart](https://discourse.processing.org/u/processingart)\
**Post date:** [May 30, 2020, 8:23pm UTC](https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435/1 "2020-05-30T20:23:40Z")

</div>

Hello, I was making an animation on processing. Then, I confused about on a problem. I prepare a simple code for you. Also, I think the question can helpfull for beginners. 💞 🥰 🤗

```auto
PShape ball;
float ballSpeed = random(0.5,1);
float ballSize = random(5,10);
float x = 200, y = 300, z = 0;

void setup(){
size(400,400,P3D);
noStroke(); // That's for "not see details on sphere
ball = createShape(SPHERE, ballSize);
}

void draw(){
  background(0);
  translate(x, y, z); // rotate the ball
  println(y); // checking the y coordinates
  fill(255);
  shape(ball);
  y -= ballSpeed; // the ball can go to top
}

```

So, I want to make a condition for if the ball reach the 100, the ball should start from the first position and then again it should go to 100 again. I tried many if contions but couldn’t figure out it.  
If you help me I will be happy. ❤  
Thanks. 😌

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [May 30, 2020, 8:44pm UTC](https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435/2 "2020-05-30T20:44:26Z")

</div>

Have you tried storing the value of ball y starting position in its own variable?

- if ball y current position is less than or equal to 100
- then set ball y current position equal to ball y starting position

---

<div class="post-metadata">

**Author:** ![processingart](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@processingart](https://discourse.processing.org/u/processingart)\
**Post date:** [May 30, 2020, 9:06pm UTC](https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435/3 "2020-05-30T21:06:14Z")

</div>

Thank you @slow_izzm I found the solution. Your idea is true.  
Let me write down the code. It can helpfull for beginners.

```auto
y -= ballSpeed;
if (y <= 100) {
   y = 300;
} 

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 31, 2020, 12:20pm UTC](https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435/4 "2020-05-31T12:20:10Z")

</div>

Hello

and welcome to the forum!

Great to have you here!

note that a sphere looks better with `lights()`

here is a **red** one going back and forth (into the screen) - Z-axis

Warmest regards,

Chrisir

```auto
PShape ball;
float ballSpeed = -random(2.5, 3);
float ballSize = random(25, 35);
float x, y, z; 

void setup() {
  size(1400, 900, P3D);

  x = width/2-150; 
  y = 860-ballSize;
  z = 150;

  noStroke(); // That's for "not see details / lines on sphere"
  ball = createShape(SPHERE, ballSize);
  ball.setFill(color(0, 255, 0));
}

void draw() {
  background(0);
  lights(); 
  floor();

  translate(x, y, z); // rotate the ball
  println(z); // checking the y coordinates
  shape(ball);
  z += ballSpeed; // the ball can go to top

  if (z <= -440) {
    ballSpeed = abs(ballSpeed) ; // always pos
  }

  if (z > 0) {
    ballSpeed = - abs(ballSpeed); // always neg
  }
}//draw()

// ------------------------------------------------------------------------------------

// tab Floor 
// Floor: CheckeredFloor 

void floor () {
  noStroke();

  for (int i = 0; i < 30; i = i+1) {
    for (int j = 0; j < 30; j = j+1) {

      // % is modulo, meaning rest of division 
      if (i%2 == 0) { 
        if (j%2 == 0) { 
          fill (255, 0, 0);
        } else 
        {
          fill ( 103 );
        }
      } else {
        if (j%2 == 0) { 
          fill ( 103 );
        } else 
        {
          fill (255, 0, 0);
        }
      } // if

      pushMatrix();
      translate ( 80*i-610, 860, 80*j-940 );
      box ( 80, 7, 80); // one cell / tile 
      popMatrix();
    } // for
  } // for
}//floor
// ---------------------------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![processingart](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@processingart](https://discourse.processing.org/u/processingart)\
**Post date:** [May 31, 2020, 12:23pm UTC](https://discourse.processing.org/t/how-to-make-repeating-3d-ball-on-y-coordinate/21435/5 "2020-05-31T12:23:51Z")

</div>

Thank you @Chrisir🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏 🙏
