Reputation: 117
First of all, check this out (link): it's an example program taken from the Processing Box2D library on github, which displays a box at the center of the screen (it can be moved around with the mouse) while a cascade of little balls fall on it; whenever a ball hits the box, that ball turns red. I copypasted the four .pde files into a single .pde sketch, run it, and it works perfectly.
Now, onto my problem. I'm currently making a game in Processing: you have a ball, a player 1 and a player 2 (both of which can be moved around using the keyboard). Box2D is in charge of the physical interactions between each player and the ball, and I must say it handles them pretty well. Each of the three main objects has its own class. Now, I want stuff to happen as soon as player 1 makes contact with the ball. And that example code seems perfect for this scope, right? It works on my computer, after all.
So, I started copying the relevant parts:
I also added these two functions at the very bottom of my sketch:
void beginContact(Contact cp) {
Fixture f1 = cp.getFixtureA();
Fixture f2 = cp.getFixtureB();
Body b1 = f1.getBody();
Body b2 = f2.getBody();
Object o1 = b1.getUserData();
Object o2 = b2.getUserData();
if (o1.getClass() == Box.class) {
Particle p = (Particle) o2;
p.change();
}
else if (o2.getClass() == Box.class) {
Particle p = (Particle) o1;
p.change();
}
}
void endContact(Contact cp) {
}
Which I promptly changed into this (I basically renamed the classes, and substituted p.change(), the method that turned the balls red in that sketch, with what I want to happen when contact is being made):
(... same ...)
if (o1.getClass() == Player.class) {
Ball p = (Ball) o2;
//do stuff when contact happens
}
else if (o2.getClass() == Player.class) {
Ball p = (Ball) o1;
//do stuff when contact happens
}
}
void endContact(Contact cp) {
}
But guess what? I get a 'Could not invoke the "beginContact()" method for some reason' error! I don't think I'm lacking any crucial files or libraries, since that example worked fine in my computer and all I did was just copypaste and run the code.
I cannot paste here my whole code because it's huge, but I swear the Player class (player 1 class), the Enemy class (player 2 class) and the Ball class all have their fixtures, and there's literally nothing in my classes that differs in a substantial way from those from the example sketch. The bodies are all of dynamic type, the players are rectangular boxes like the box in the example, and the ball is a pure circle like the little balls that turned red there.
What is happening? Did I miss an important line from that code? Even though the example code runs perfectly without needing any additional files, I should mention that the console also prints an 'at shiffman.box2d.Box2DContactListener.beginContact(Box2DContactListener.java:54)' error: now, like I said, I don't need that Box2DContactListener.java file in my computer to run the example sketch... but anyway, if I read it up online (link), I can see this is what it's referring to:
public void beginContact(Contact c) {
if (beginMethod != null) {
try {
beginMethod.invoke(parent, new Object[] { c });
} catch (Exception e) {
System.out.println("Could not invoke the \"beginContact()\" method for some reason.");
e.printStackTrace();
beginMethod = null;
}
}
}
Do you have any idea what's going on here?
Upvotes: 0
Views: 579
Reputation: 26
Basically, before the if statement, you have to check if either of the objects is 'null'. In case it is, you just have to break. You can just simply paste this:
if (o1==null || o2==null)
return;
Upvotes: 1