使用jbox2D库处理冲突?

时间:2014-07-07 06:33:26

标签: box2d processing jbox2d

我是Processing的新手,JBox2D的新用户,以及Stack Overflow的新用户,所以我非常感谢你能得到的任何帮助!

我正在尝试编写一个基于Shiffman的JBox2D / Processing教程的简单程序,您可以在其中单击以创建新球,然后拖动并释放鼠标以将其抛出。球可以在草图的边缘周围相互碰撞并形成一圈圆圈("键和#34;)。每次单击球对象时都会添加到数组列表中。

一切正常,直到你开始快速点击,在短时间内创造出许多球。我可以解释的最好的是,草图似乎“失去了它的物理性能......”球不再落下或反弹,它们彼此紧贴,只是悬停在草图的中间。我收到一条错误消息"无法调用" beginContact()"出于某种原因的方法"在sun.reflect.GeneratedMethodAccessor2.invoke(未知来源)中以" java.lang.reflect.InvocationTargetException开头的一长串错误列表...."等等等

任何帮助都会非常感激!!

我的代码:

import shiffman.box2d.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;
import org.jbox2d.dynamics.contacts.Contact;
import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;

Box2DProcessing box2d;

Spring spring;
ArrayList<Ball> balls;
ArrayList<Key> keys;

float colors[] = {
  40, 80, 120, 160, 200
};
float border=130;

void setup() {
  frameRate(30);
  colorMode(HSB);
  size(1280, 800);
  smooth();

  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  box2d.setGravity(0, -30);
  box2d.listenForCollisions();

  // Make the ball
  balls = new ArrayList<Ball>();
  spring = new Spring();

  //KEY PLACEMENT
  //top row
  float x = 0;
  float y = 0;
  int c = 0; //index of colors array
  float keysSize = 120;
  keys = new ArrayList<Key>();
  for (int i=0; i<11; i++) {
    keys.add(new Key(x, y, keysSize, colors[c]));
    x+=width/7;
    c+=1;
    if (c==colors.length) {c=0;}
  }
  //bottom row
  x = 0;
  y = height;
  for (int i=11; i<20; i++) {
    keys.add(new Key(x, y, keysSize, colors[c]));
    x+=width/7;
    c+=1;
    if (c==colors.length) {c=0;}
  }
  //sides
  x = 0;
  y = keysSize*1.2;
  for (int i=20; i<30; i++) {
    keys.add(new Key(x, y, keysSize, colors[c]));
    if (x==width) {x = 0; y+=keysSize*1.5;}
    else {x+=width;}
    c+=1;
    if (c==colors.length) {c=0;}
  }
} //END SETUP


void draw() {
  background(0);
  box2d.step();

  //DRAW KEYS
  for (int i=keys.size()-1; i>=0; i--) {
    Key k = keys.get(i);
    k.display();
  }

  //DRAW BALLS
  for (int i=balls.size()-1; i>=0; i--) {
    Ball b = balls.get(i);
    b.display();
    if (b.done()) {
      balls.remove(i);
    }
  }

  //DRAW SPRINGS
  spring.update(mouseX, mouseY);
  spring.display();
}//END DRAW

void beginContact(Contact cp) {
  // Get both fixtures
  Fixture f1 = cp.getFixtureA();
  Fixture f2 = cp.getFixtureB();
  // Get both bodies
  Body b1 = f1.getBody();
  Body b2 = f2.getBody();

  // Get our objects that reference these bodies
  Object o1 = b1.getUserData();
  Object o2 = b2.getUserData();

  if (o1.getClass() == Ball.class && o2.getClass() == Key.class) {
    Ball b = (Ball) o1;
    Key k = (Key) o2;
    k.hit();
  }
  else if (o2.getClass() == Ball.class && o1.getClass() == Key.class) {
    Ball b = (Ball) o2;
    Key k = (Key) o1;
    k.hit();
  }
  else if (o1.getClass() == Ball.class && o2.getClass() == Ball.class) {
    Ball b = (Ball) o1;
    Ball c = (Ball) o2;
  }
}

void endContact(Contact cp) {
}

void mousePressed() {

  if (mouseX>border && mouseX<width-border && mouseY>border && mouseY<height-border) {
    if (classes[index]=="Ball") {
      balls.add(new Ball(mouseX, mouseY));

      for (int i=balls.size()-1; i>=0; i--) {
        Ball b = balls.get(i);
        if (b.contains(mouseX, mouseY)) {
          spring.bind(mouseX, mouseY, b);
        }
      }
    }
  }
}

void mouseReleased() {
  for (int i=balls.size()-1; i>=0; i--) {
    Ball b = balls.get(i);
    b.hasLaunched = true;
  }
  spring.destroy();
}

1 个答案:

答案 0 :(得分:0)

听起来身体在彼此内部产生,这将导致问题。

您可以修改产卵算法,以便只在空白区域中生成新实体,或者可以限制创建新实体的频率。

谷歌搜索&#34; box2d在体内产卵&#34;返回一堆有希望的结果。