在同一处理草图中使用 Box2D 和视频的问题

Issue using Box2D and video in the same Processing sketch

我正在尝试使用 Box2D for Processing 和 Video 库创建草图。
我有一些盒子四处移动并相互碰撞,我想用一些视频对这些盒子进行纹理处理。
但是,我一输入代码行就收到一条很长的错误消息,告诉处理我想使用哪个视频文件。

import processing.video.*;
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;


Box2DProcessing box2d;

int no_boxes = 7;

//All of our boxes
Box[] boxes = new Box[no_boxes];

//Boundry sides
Boundry[] boundry = new Boundry[4];

float angle = -PI/2;
float grav_rad = 30;

float boundr = 260;
float boundw = 30;

PVector direction = new PVector();

Movie mouth;


void setup() {
  size(800,800,P3D);
  frameRate(60);
  colorMode(HSB,360,100,100);
  noStroke();

  //Load the movie
  mouth = new Movie(this,"mouth1.mov");
  mouth.play();

  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();

  //Create the boxes
  for (int n=0;n<no_boxes; n++) {
    boxes[n] = new Box(random(200,600), random (200,600));
  }

  //Create the boundries
  boundry[0] = new Boundry(400+boundr,400,boundw,2*boundr+boundw);
  boundry[1] = new Boundry(400-boundr,400,boundw,2*boundr+boundw);
  boundry[2] = new Boundry(400,400+boundr,2*boundr+boundw, boundw);
  boundry[3] = new Boundry(400,400-boundr,2*boundr+boundw, boundw);



}

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


void draw() {

  background(200,50,80);
  image(mouth,0,0);

  angle+= radians(0.3);

  direction.x = grav_rad*cos(angle);
  direction.y = grav_rad*sin(angle);

  box2d.setGravity(-direction.x, direction.y);

  camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0-map(direction.x,-10,10,-1,1), 0-map(direction.y,-10,10,-1,1), 0);   //x, y, z -> need to change between x and y

  box2d.step();

  // Display all the boxes
  for (Box b: boxes) {
    b.display();
  }

  //display the boundries
  boundry[0].display();
  boundry[1].display();
  boundry[2].display();
  boundry[3].display();

}

这是我收到的错误消息:

finishLifecycleAction(com.jogamp.opengl.util.FPSAnimator$3): ++++++ 达到超时 ++++++ main-FPSAWTAnimator#00-Timer0
finishLifecycleAction(com.jogamp.opengl.util.FPSAnimator$3): OK false- pollPeriod 6, blocking true -> res false, 等待 1002/1000 - main-FPSAWTAnimator#00-Timer0
- com.jogamp.opengl.util.FPSAnimator[开始为真,动画为真,暂停为假,drawable 1,总计[dt 0,frames 0,fps 0.0],modeBits 1,init'ed true,animThread Thread[main-FPSAWTAnimator#00-Timer0- FPSAWTAnimator#00-Timer1,5,main], exclCtxThread false(null)]
[2]: com.jogamp.opengl.util.AnimatorBase.finishLifecycleAction(AnimatorBase.java:634)
[3]: com.jogamp.opengl.util.FPSAnimator.stop(FPSAnimator.java:326)
[4]: processing.opengl.PSurfaceJOGL.stopThread(PSurfaceJOGL.java:722)
[5]: processing.core.PApplet.dispose(PApplet.java:3830)
[6]: processing.core.PApplet.die(PApplet.java:3741)
[7]: processing.core.PApplet.die(PApplet.java:3751)
[8]:processing.video.Movie.initGStreamer(来源不明)
[9]:processing.video.Movie。(来源不明)
[10]: Rotating_mouth_boxes_100620.setup(Rotating_mouth_boxes_100620.java:58)
[11]: processing.core.PApplet.handleDraw(PApplet.java:2432)
[12]: processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:866)
[13]: jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692)
[14]: jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674)
[15]: jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443)
[16]: jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293)
[17]: jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
[18]: com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:782)
[19]: com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
[20]: com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:453)
[21]: com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
[22]: java.util.TimerThread.mainLoop(Timer.java:555)
[23]: java.util.TimerThread.run(Timer.java:505)

有什么想法吗?我已经成功地分别使用了 Box2D 和视频,但是将它们放在同一个草图中似乎是个问题。

我对此不是 100%,但看起来 P3D(OpenGL) 渲染器需要在一个时间范围内完成初始化,并且某些东西延迟太久以至于超时。

有一个镜头加载电影可能是个问题。

如果您可以摆脱默认的软件渲染器,请尝试一下:

size(800,800,JAVA2D);

并删除 camera()。希望这个视觉效果还是可以的。

目前我想不出一个干净的解决方案。

另一个解决方法是稍后在 P3D 渲染器准备好之后初始化影片。例如,您可以使用一个额外的布尔变量来跟踪电影加载是否已被触发,以确保只发生一次。

这是一个(未经测试的)示例:希望评论说明解决方法:

import processing.video.*;
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;


Box2DProcessing box2d;

int no_boxes = 7;

//All of our boxes
Box[] boxes = new Box[no_boxes];

//Boundry sides
Boundry[] boundry = new Boundry[4];

float angle = -PI/2;
float grav_rad = 30;

float boundr = 260;
float boundw = 30;

PVector direction = new PVector();

Movie mouth;

// keep track if movie load was called
boolean wasMovieLoadCalled = false;

void setup() {
  size(800,800,P3D);
  frameRate(60);
  colorMode(HSB,360,100,100);
  noStroke();

  // Initialize box2d physics and create the world
  box2d = new Box2DProcessing(this);
  box2d.createWorld();

  //Create the boxes
  for (int n=0;n<no_boxes; n++) {
    boxes[n] = new Box(random(200,600), random (200,600));
  }

  //Create the boundries
  boundry[0] = new Boundry(400+boundr,400,boundw,2*boundr+boundw);
  boundry[1] = new Boundry(400-boundr,400,boundw,2*boundr+boundw);
  boundry[2] = new Boundry(400,400+boundr,2*boundr+boundw, boundw);
  boundry[3] = new Boundry(400,400-boundr,2*boundr+boundw, boundw);

}

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


void draw() {
  // if load wasn't called
  if(wasMovieLoadCalled == false){

    //Load the movie
    mouth = new Movie(this,"mouth1.mov");
    mouth.play();

    // update the variable so load happens only once
    wasMovieLoadCalled = true;

  }

  background(200,50,80);

  // ensure the movie is loaded before trying to display it
  if(mouth != null){
    image(mouth,0,0);
  }


  angle+= radians(0.3);

  direction.x = grav_rad*cos(angle);
  direction.y = grav_rad*sin(angle);

  box2d.setGravity(-direction.x, direction.y);

  camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0-map(direction.x,-10,10,-1,1), 0-map(direction.y,-10,10,-1,1), 0);   //x, y, z -> need to change between x and y

  box2d.step();

  // Display all the boxes
  for (Box b: boxes) {
    b.display();
  }

  //display the boundries
  boundry[0].display();
  boundry[1].display();
  boundry[2].display();
  boundry[3].display();

}