# Creating complex gravity bouncing objects

**URL:** <https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162>\
**Category:** Coding Questions\
**Created:** [September 1, 2018, 12:54am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162 "2018-09-01T00:54:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tefa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tefa/32/1181_2.png) [@tefa](https://discourse.processing.org/u/tefa)\
**Post date:** [September 1, 2018, 12:54am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162/1 "2018-09-01T00:54:52Z")

</div>

Hello Processors, please help me figure what to do. I want video objects to bounce off the window edges and collide between them. The issue I am having is that I want the objects to have random sizes. this is messing everything up. Thank you for your time 🙂

```auto
import processing.video.*;
Movie myMovie;

int numVideos = 10;
Video[] videos = new Video[numVideos];

float groundY = 580;

void setup() {
  size (640, 640);
  myMovie = new Movie(this, "fogata.mp4");
  myMovie.loop();
  for (int i = 0; i < numVideos; i++) {
    videos[i] = new Video(4096/10, 2160/10);
  }
}

void draw() {
  background(64);
  stroke(255);
  line(0, groundY, width, groundY);
  //imageMode(CENTER);
  for (Video video : videos) {
    video.move();
    video.collide();
    video.display();
  }
}

void movieEvent(Movie m) {
  m.read();
}

class Video {

  float rectW, rectH;
  float rectSpeedY;
  float rectSpeedX = 1;
  float rectY = 0;
  float rectX = random(15)*50;
  float s = random(1);

  Video (float w, float h) {
    rectW = w;
    rectH = h;
  }
  void move() {
    rectY += rectSpeedY;
    if (rectY+s + rectH+s > groundY) {
      rectY = groundY - rectH*s;
      //bounce
      rectSpeedY *= -1 ;
    } else {
      rectSpeedY++;
    }
  }
  void collide() {

  }
  
  void display() { 
    image(myMovie, rectX, rectY, rectW*s, rectH*s);
  }
}

```

---

<div class="post-metadata">

**Author:** ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)\
**Post date:** [September 1, 2018, 1:16am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162/2 "2018-09-01T01:16:53Z")

</div>

Hey there,

Maybe you can try displaying your videos before you move them?

```auto
    video.display();
    video.move();
    video.collide();

```

Maybe that will help on your variables updating in time when they get re-assigned to a new random value, if that makes sense?

That’s all I can think of for now, hope that helps though 😃

EnhancedLoop7

---

<div class="post-metadata">

**Author:** ![tefa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tefa/32/1181_2.png) [@tefa](https://discourse.processing.org/u/tefa)\
**Post date:** [September 1, 2018, 2:03am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162/3 "2018-09-01T02:03:30Z")

</div>

Thank you, nope nothing changed hehe.

---

<div class="post-metadata">

**Author:** ![tefa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tefa/32/1181_2.png) [@tefa](https://discourse.processing.org/u/tefa)\
**Post date:** [September 1, 2018, 3:13am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162/4 "2018-09-01T03:13:17Z")

</div>

fixed.

```auto
import processing.video.*;
Movie myMovie;

int numVideos = 5;
Video v1, v2, v3, v4, v5;

float groundY = 580;
float s = random(1);

void setup() {
  size (640, 640);
  myMovie = new Movie(this, "fogata.mp4");
  myMovie.loop();
  v1 = new Video(4096/20, 2160/20);
  v2 = new Video(4096/30, 2160/30);
  v3 = new Video(4096/10, 2160/10);
  v4 = new Video(4096/45, 2160/45);
  v5 = new Video(4096/25, 2160/25);
}

void draw() {
  background(64);
  stroke(255);
  line(0, groundY, width, groundY);
  v1.display();
  v1.move();
  v1.collide();
  v2.display();
  v2.move();
  v2.collide();
  v3.display();
  v3.move();
  v3.collide();
  v4.display();
  v4.move();
  v4.collide();
  v5.display();
  v5.move();
  v5.collide();
}
void movieEvent(Movie m) {
  m.read();
}
class Video {
  float rectW, rectH;
  float rectSpeedY;
  float rectSpeedX = 1;
  float rectY = 5;
  float rectX = map(width, (random(15)*50), 650, 0, 0);

  Video (float w, float h) {
    rectW = w;
    rectH = h;
  }

  void move() {
    rectX = rectX+1*rectSpeedX;
    rectY = rectY+1*rectSpeedY;

    if ((rectX <=0) || (rectX+rectW>= width)) {
      rectSpeedX *= -1;
    }
    if ((rectY <= 0) || (rectY +rectH*s>= height-60)) {
      rectSpeedY *= -1;
    }

    rectY += rectSpeedY;
    if (rectY+s + rectH+s >= groundY) {
      rectY = groundY - rectH;
      //bounce
      rectSpeedY *= -1 ;
    } else {
      rectSpeedY++;
    }
  }

  void collide() {
  }

  void display() {
    image(myMovie, rectX, rectY, rectW, rectH);
  }
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [September 1, 2018, 3:22am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162/5 "2018-09-01T03:22:06Z")

</div>

For collision, check [this tutorial](http://www.jeffreythompson.org/collision-detection/index.php)

For bouncing, check these:

> [Bounce / Examples / Processing.org](https://processing.org/examples/bounce.html)  
> [Bouncy Bubbles / Examples / Processing.org](https://processing.org/examples/bouncybubbles.html)

Kf

---

<div class="post-metadata">

**Author:** ![tefa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tefa/32/1181_2.png) [@tefa](https://discourse.processing.org/u/tefa)\
**Post date:** [September 1, 2018, 3:23am UTC](https://discourse.processing.org/t/creating-complex-gravity-bouncing-objects/3162/6 "2018-09-01T03:23:22Z")

</div>

I have thank you. this tutorial [https://happycoding.io/tutorials/processing/collision-detection](https://happycoding.io/tutorials/processing/collision-detection) is the best
