box2D collisions and chainshape failing

edited March 2017 in Library Questions

So... i was making "the nature of code chapter 5" that uses Pbox2D to work with physics.

Here are the tutorials : https://www.youtube.com/playlist?list=PLRqwX-V7Uu6akvoNKE4GAxf6ZeBYoJ4uh

Okay, so I found a weird bug.

Im using "chainshape" to display the surface as it was told in tutorial 5.6.

Everytime the particles touches the chainshape of the "surface " object" collisions just stop working.

I´ve even try just blending the examples with no code change and this is still happening.

It pop´s out a weird java error.

**Here´s the minimum code to test.Is the same code from tutorials : **

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.*;

Box2DProcessing box2d;
ArrayList<Particle> particles;

Surface wall;

void setup() {
  size(640, 360);
  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  box2d.listenForCollisions();
  particles = new ArrayList<Particle>();
  wall = new Surface();
}

void draw() {
  background(255);
  float sz = random(4, 8);
  particles.add(new Particle(mouseX, mouseY, sz));
  box2d.step();
  for (int i = particles.size()-1; i >= 0; i--) {
    Particle p = particles.get(i);
    p.display();
    if (p.done()) {
      particles.remove(i);
    }
  }
  wall.display();
}
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() == Particle.class && o2.getClass() == Particle.class) {
    Particle p1 = (Particle) o1;
    p1.change();
    Particle p2 = (Particle) o2;
    p2.change();
  }
  if (o1.getClass() == Surface.class && o2.getClass() == Particle.class) {
    Particle p2 = (Particle) o2;
    p2.change();
  }
}
void endContact(Contact cp) {
}
class Particle {
  Body body;
  float r;
  color col;
  Particle(float x, float y, float r_) {
    r = r_;
    makeBody(x, y, r);
    body.setUserData(this);
    col = color(127);
  }
  void killBody() {
    box2d.destroyBody(body);
  }
  void change() {
    col = color(255, 0, 0);
  }
  boolean done() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    if (pos.y > height+r*2) {
      killBody();
      return true;
    }
    return false;
  }
  void display() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    float a = body.getAngle();
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(a);
    fill(col);
    stroke(0);
    strokeWeight(2);
    ellipse(0, 0, r*2, r*2);
    line(0, 0, r, 0);
    popMatrix();
  }
  void makeBody(float x, float y, float r) {
    BodyDef bd = new BodyDef();
    bd.position = box2d.coordPixelsToWorld(x, y);
    bd.type = BodyType.DYNAMIC;
    body = box2d.createBody(bd);
    CircleShape cs = new CircleShape();
    cs.m_radius = box2d.scalarPixelsToWorld(r);
    FixtureDef fd = new FixtureDef();
    fd.shape = cs;
    fd.density = 1;
    fd.friction = 0.01;
    fd.restitution = 0.3;
    body.createFixture(fd);
    body.setAngularVelocity(random(-10, 10));
  }
}
class Surface {
  ArrayList<Vec2> surface;
  Surface() {
    surface = new ArrayList<Vec2>();
    surface.add(new Vec2(0, height/2));
    surface.add(new Vec2(width/2, height/2+50));
    surface.add(new Vec2(width, height/2));
    ChainShape chain = new ChainShape();
    Vec2[] vertices = new Vec2[surface.size()];
    for (int i = 0; i < vertices.length; i++) {
      vertices[i] = box2d.coordPixelsToWorld(surface.get(i));
    }
    chain.createChain(vertices, vertices.length);
    BodyDef bd = new BodyDef();
    Body body = box2d.world.createBody(bd);
    body.createFixture(chain, 1);
  }
  void display() {
    strokeWeight(1);
    stroke(0);
    fill(0);
    beginShape();
    for (Vec2 v: surface) {
      vertex(v.x, v.y);
    }
    vertex(width, height);
    vertex(0, height);
    endShape(CLOSE);
  }
}

**This is the error I get : **

Could not invoke the "beginContact()" method for some reason. java.lang.reflect.InvocationTargetException at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at shiffman.box2d.Box2DContactListener.beginContact(Box2DContactListener.java:54) at org.jbox2d.dynamics.contacts.Contact.update(Contact.java:330) at org.jbox2d.dynamics.World.solveTOI(World.java:1371) at org.jbox2d.dynamics.World.step(World.java:642) at shiffman.box2d.Box2DProcessing.step(Box2DProcessing.java:76) at shiffman.box2d.Box2DProcessing.step(Box2DProcessing.java:70) at debugforcollisionbox2djuliexample.draw(debugforcollisionbox2djuliexample.java:69) at processing.core.PApplet.handleDraw(PApplet.java:2412) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316) Caused by: java.lang.NullPointerException at debugforcollisionbox2djuliexample.beginContact(debugforcollisionbox2djuliexample.java:101) ... 13 more

You can look for the examples yourself, the idea is to combine "NOC_5_3_ChainShape" example and "NOC_9_3_CollisionListening" _example

Any suggestions? Ideas?

Answers

Sign In or Register to comment.