Search code examples
javaprocessingbox2d

processing box2d assertion error


Here is my code:

import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
ArrayList<Box>boxes;
Box2DProcessing box2d;
void setup() {
  size(500, 500);
  box2d=new Box2DProcessing(this);
  box2d.createWorld();
  box2d.setGravity(0, -10);
  boxes=new ArrayList<Box>();
  boxes.add(new Box(100, 300, 100, 200, true, 0, 0, 0));
}
void draw() {
  background(255);
  box2d.step();
  for (Box b : boxes)b.display();
  //thing.display();
}
class Box{
  void display(){
    fill(100);
    noStroke();
    Vec2 pos = box2d.getBodyPixelCoord(body);
    float a = body.getAngle();
    pushMatrix();
    translate(pos.x,pos.y);
    rotate(-a);
    rect(0,0,10,10);
    popMatrix();
  }
  Body body;
  Box(float x,float y,float w,float h,boolean dynamic,float vx,float vy,float angVel){

    BodyDef bd=new BodyDef();
    Vec2 center=box2d.coordPixelsToWorld(x,y);
    bd.position.set(center);
    bd.fixedRotation=false;
    bd.linearDamping=0.8;
    bd.angularDamping=0.9;
    bd.bullet=false;
    if(dynamic)bd.type=BodyType.DYNAMIC;
    else bd.type=BodyType.STATIC;

    body=box2d.createBody(bd);
    body.setLinearVelocity(new Vec2(vx,vy));
    body.setAngularVelocity(angVel);

    PolygonShape ps=new PolygonShape();
    Vec2 size=box2d.coordPixelsToWorld(w,h);
    ps.setAsBox(size.x,size.y);

    FixtureDef fd=new FixtureDef();
    fd.shape=ps;
    fd.friction=0.3;
    fd.restitution=0.5;
    fd.density=1.0;

    body.createFixture(fd);
  }
}

I am getting an AssertionError when I call body.createFixture(fd).

I am using ProcessingBox2D and I am following http://natureofcode.com/book/chapter-5-physics-libraries/ tutorial. When I googled, I found that you cannot create a body during a step but that does not seem to be the problem.

EDIT: There is no stacktrace, it just says AssertionError. Link to screenshot here: https://imageshack.com/a/img922/1063/4DSsUz.png


Solution

  • ok. I messed up pretty big here, :). First of all, w and h ended up being negative, Box2D was probably asserting that w>0 and h>0.

    And also, I was always drawing a 10x10 rectangle no matter what so when I changed w and h it had no effect on what was being drawn.