Displaying a movie or gif as an "event" in a sketch
in
Programming Questions
•
4 months ago
Hi everybody
This is my first processing project (and first java project), so please bear with me if I'm making some really noob mistakes here.
Basically, the program I wrote sends circles flying around and if they collide with each other, they "explode". For the explosion I've tried to use a .mp4 file as a Movie object and a .gif file as a Gif object, but both seem to just play the first frame of the movie/gif (if that). If I just play the movie/gif using the image function in draw() the playback is fine.
My main question is, if I'm trying to display a movie/gif but the function call to display the movie is buried in a few layers of code what can I do to correctly play the movie when the two particles collide with each other?
Here is my code:
- import gifAnimation.*;
- import processing.video.*;
- PointSystem ps;
- String gifFileName = "explosion_animated_gif.gif";
- String movFileName = "explosion.mp4";
- Movie explosion;
- Gif gifExp;
- void setup(){
- size(500,500, P2D);
- frameRate(100);
- explosion = new Movie(this, movFileName);
- explosion.noLoop();
- explosion.speed(3);
- explosion.play();
- explosion.read();
- gifExp = new Gif(this, gifFileName);
- gifExp.loop();
- gifExp.play();
- ps = new PointSystem(10, gifExp);
- background(0);
- }
- void draw(){
- background(0);
- //image(gifExp, width/2 - gifExp.width/2, height/2 - gifExp.height/2);
- // // image(explosion, width - explosion.width/2, height - explosion.height/2);
- ps.run();
- //image(explosion, mouseX - 100 - 25, mouseY-25,50,50);
- //image(explosion, mouseX + 100 - 25, mouseY-25,50,50);
- println(frameRate);
- }
Here is the "Point" class, the objects that are flying around the screen:
- import java.util.Iterator;
- import processing.video.*;
- class Point {
- PVector velocity;
- PVector gravity = new PVector(0, 0.01);
- float speedx;
- float speedy;
- PVector direction;
- PVector location;
- float xpos;
- float ypos;
- float angle;
- // int lifespan;
- PShape p;
- boolean isAlive;
- boolean inCollision;
- Point() {
- xpos = random(width);
- ypos = random(height);
- location = new PVector(xpos, ypos);
- speedx = random(-1, 1);
- speedy = random(-1, 1);
- angle = random(TWO_PI);
- direction = new PVector(cos(angle), sin(angle));
- velocity = new PVector(direction.x*speedx, direction.y*speedy);
- // lifespan = int(random(1000));
- isAlive = true;
- inCollision = false;
- println("Hey you initialized a point!");
- println("Velocity : " + velocity);
- println("Location : " + location);
- }
- void burst(Gif animation) {
- image(animation, location.x-animation.width/2, location.y-animation.height/2);
- isAlive = false;
- }
- void burst(Movie animation) {
- // movieEvent(animation);
- image(animation, location.x-animation.width/2, location.y-animation.height/2, 50, 50);
- isAlive = false;
- }
- // void movieEvent(Movie m){
- // if(m.available()){
- // m.read();
- // }
- // }
- void display() {
- stroke(255);
- fill(0, 0, 255);
- ellipse(location.x, location.y, 6, 6);
- }
- void ckPos() {
- if (location.x < 0) {
- location.x = width;
- }
- else if (location.y < 0) {
- location.y = height;
- }
- else if(location.x > width){
- location.x = 0;
- }
- else if(location.y > height){
- location.y = 0;
- }
- }
- void update() {
- //velocity.add(gravity);
- location.add(velocity);
- ckPos();
- display();
- // lifespan--;
- // if (lifespan < 0) {
- // isAlive = false;
- // }
- }
- }
And here is the "PointSystem" class that choreographs the whole thing:
- import java.util.Iterator;
- import java.lang.Math.*;
- import gifAnimation.*;
- import processing.video.*;
- class PointSystem {
- int n;
- ArrayList<Point> points;
- Gif gifAnimation;
- Movie movAnimation;
- PointSystem(int n, Gif animation) {
- points = new ArrayList<Point>();
- for (int i=0;i<n;i++) {
- points.add(new Point());
- }
- gifAnimation = animation;
- println("The length of the ArrayList is : " + points.size());
- }
- PointSystem(int n, Movie animation) {
- points = new ArrayList<Point>();
- for (int i=0;i<n;i++) {
- points.add(new Point());
- }
- movAnimation = animation;
- println("The length of the ArrayList is : " + points.size());
- }
- void ckCollisions(double thresh) {
- //check distances using a double for loop, skip if already found to be in collision
- for (int i = 0; i < points.size(); i++) {
- Point point1 = points.get(i);
- if (point1.inCollision) {
- continue;
- }
- for (int j = 0; j < points.size(); j++) {
- if (i==j) {
- continue;
- }
- Point point2 = points.get(j);
- if (point2.inCollision) {
- continue;
- }
- double dist = pointEUDist(point1, point2);
- if (dist < thresh) {
- point1.inCollision = true;
- point2.inCollision = true;
- burst(point1, point2);
- }
- }
- }
- }
- void burst(Point p1, Point p2){
- float x = (p1.location.x+p2.location.x)/2;
- float y = (p1.location.y+p2.location.y)/2;
- if(gifAnimation!=null){
- image(gifAnimation, x-gifAnimation.width/2, y-gifAnimation.height/2, 100, 100);
- }
- else if(movAnimation!=null){
- image(movAnimation, x-100/2, y-100/2, 100, 100);
- }
- p1.isAlive = false;
- p2.isAlive = false;
- }
- double pointEUDist(Point p1, Point p2) {
- double dist = Math.sqrt( Math.pow((p1.location.x-p2.location.x), 2) + Math.pow((p1.location.y-p2.location.y), 2) );
- return dist;
- }
- void movieEvent(Movie m){
- if(m.available()){
- m.read();
- }
- }
- void run() {
- ckCollisions(10);
- Iterator<Point> it = points.iterator();
- while (it.hasNext ()) {
- Point p = it.next();
- // if (p.inCollision) {
- // if(gifAnimation != null){
- // p.burst(gifAnimation);
- // }
- // else if(movAnimation != null){
- // movieEvent(movAnimation);
- // p.burst(movAnimation);
- // }
- // }
- if (p.isAlive==false) {
- it.remove();
- }
- else {
- p.update();
- }
- }
- }
- }
1