Displaying a movie (.mp4 file) in a sketch
in
Core Library Questions
•
3 months ago
Hi Everyone!
I wrote a simple particle simulation program that has blue circles flying around the screen and colliding into each other. When two circles collide I want to display an "explosion" movie (currently implemented as a Movie object) at the location of the collision.
However, I can't get the full movie to display when the two circles collide - sometimes I don't see anything, and sometimes only a few random frames play. Has anyone done this before or know if there is a very specific way I need to display movie data? This is my first project so please bear with me.
Here is the "test" class, basically the main function in the program:
- import processing.video.*;
- import gifAnimation.*;
- PointSystem ps;
- String gifFileName = "explosion_animated_gif.gif";
- String movFileName = "explosion.mp4";
- Movie explosion;
- Gif gifExp;
- int counter = 0;
- void setup(){
- size(displayWidth/2,displayHeight/2);
- frameRate(100);
- explosion = new Movie(this, movFileName);
- explosion.loop();
- explosion.play();
- println(explosion.duration());
- ps = new PointSystem(40, explosion);
- }
- void draw(){
- background(0);
- ps.run();
- }
- void movieEvent(Movie m){
- m.read();
- }
- 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;
- 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() {
- location.add(velocity);
- ckPos();
- display();
- }
- }
Here is the "PointSystem" class, which choreographs the simulation :
- import java.util.Iterator;
- import java.lang.Math.*;
- import gifAnimation.*;
- import processing.video.*;
- import java.util.concurrent.*;
- class PointSystem {
- int n;
- ArrayList<Point> points;
- Gif gifAnimation;
- Movie movAnimation;
- ExecutorService exec = Executors.newCachedThreadPool();
- 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());
- }
- PVector[] ckCollisions(double thresh) {
- //check distances using a double for loop, skip if already found to be in collision
- PVector[] outArray = {
- };
- 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;
- point1.isAlive = false;
- point2.inCollision = true;
- point2.isAlive = false;
- float[] xarr = {
- point1.location.x, point2.location.x
- };
- float[] yarr = {
- point1.location.y, point2.location.y
- };
- float xpos = mean(xarr);
- float ypos = mean(yarr);
- PVector curVec = new PVector(xpos, ypos);
- outArray = (PVector[]) append(outArray, curVec);
- }
- }
- }
- return(outArray);
- }
- float mean(float[] nums) {
- float runningTotal = 0;
- for (int i=0; i<nums.length; i++) {
- runningTotal += nums[i];
- }
- return (runningTotal/nums.length);
- }
- 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-10/2, y-10/2, 10, 10);
- }
- else if (movAnimation!=null) {
- }
- 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() {
- PVector[] expLocations = ckCollisions(10);
- if (expLocations.length > 0) {
- for (int i=0; i<expLocations.length; i++) {
- exec.execute(new Explosion(movAnimation, expLocations[i].x, expLocations[i].y, 100, 100));
- }
- }
- Iterator<Point> it = points.iterator();
- while (it.hasNext ()) {
- Point p = it.next();
- if (p.isAlive==false) {
- it.remove();
- }
- else {
- p.update();
- }
- }
- }
- }
Here is the "Explosion" class. Since I couldn't get the explosions to display properly using standard serial execution, I implemented this class as an extension of the "Thread" class with a Runnable implementation. However the concurrent programming doesn't solve my problem, nor does it seem to effect the way the program was running before. :
- import processing.video.*;
- import java.util.concurrent.*;
- class Explosion extends Thread implements Runnable {
- Movie animation;
- float x;
- float y;
- float h;
- float w;
- boolean running;
- Explosion(Movie animation_,float x_, float y_, float w_, float h_){
- animation = animation_;
- x = x_;
- y = y_;
- w = w_;
- h = h_;
- }
- void start(){
- running = true;
- println("Animating an explosion");
- }
- void run(){
- image(animation, x - w/2, y - h/2, w, h);
- println("Exploding at : " + x + " " + y);
- }
- void movieEvent(){
- animation.read();
- }
- }
1