Creating complex gravity bouncing objects

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 :slight_smile:

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);
  }
}
1 Like

Hey there,

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

    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 :smiley:

EnhancedLoop7

Thank you, nope nothing changed hehe.

fixed.

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);
  }
}
2 Likes

For collision, check this tutorial

For bouncing, check these:

https://processing.org/examples/bounce.html
https://processing.org/examples/bouncybubbles.html

Kf

1 Like

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

1 Like