我是新來的Processing,JBox2D的新手,以及Stack Overflow的新手,所以我能得到的任何幫助都將非常感謝!jbox2D庫處理中的衝突?
我正在嘗試編寫一個基於Shiffman的JBox2D/Processing教程的簡單程序,您可以在其中單擊以創建一個新球,然後拖動並釋放鼠標以將其扔出。球可以相互碰撞並在草圖邊緣附近形成一圈圈(「鍵」)。每次你點擊一個球對象被添加到數組列表。
一切工作正常,直到你開始快速點擊,在很短的時間內創建許多球。我可以解釋的最好的情況是素描似乎「失去了它的物理學」......球不再掉落或跳動,它們互相緊貼,只是停留在素描中間。 「出於某種原因,無法調用」beginContact()方法「的錯誤消息中包含一長串以」java.lang.reflect.InvocationTargetException at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) ....「等等等等
任何幫助將非常感激!
我的代碼:
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();
}