Skip to content

AssertionError occurred on box2d's step method #19

Description

@helloscoopa

When i add some JointPairs to the world there is a Assertion Error comming from the box2d's step method that called inside draw loop. here is the full code i've tried to understand by nature of code examples.

MainProgram.pde

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<Boundary> boundaries;
ArrayList<JointPair> pairs;


void setup() {
  size(640, 360);

  box2d = new Box2DProcessing(this);
  box2d.createWorld();

  pairs = new ArrayList<JointPair>();
  boundaries = new ArrayList<Boundary>();

  boundaries.add(new Boundary(width/4, height-5, width/2-50, 10));
  boundaries.add(new Boundary(3*width/4, height-50, width/2-50, 10));
}

void draw() {
  background(255);

  box2d.step();

  for (Boundary bounds : boundaries) {
    bounds.display();
  }

  for (int i=pairs.size()-1; i>0; i--) {
    pairs.get(i).display();
    if (pairs.get(i).isPairOffScreen()) {
      pairs.remove(i);
    }
  }
}

void mousePressed() {
  JointPair p = new JointPair(mouseX, mouseY);
  pairs.add(p);
}

Boundary.pde

class Boundary{
  float x,y,w,h;
  
  Body body;
  
  Boundary(float _x, float _y, float _w, float _h){
    x = _x;
    y = _y;
    w = _w;
    h = _h;
    
    //Define the shape
    PolygonShape ps = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(w/2);
    float box2dH = box2d.scalarPixelsToWorld(h/2);
    ps.setAsBox(box2dW,box2dH);
    
    //create body definition
    BodyDef bd = new BodyDef();
    bd.type = BodyType.STATIC;
    bd.position.set(box2d.coordPixelsToWorld(x,y));
    body = box2d.createBody(bd);
    
    //put all together
    body.createFixture(ps,1);
  }
  
  void display() {
    fill(0);
    stroke(0);
    rectMode(CENTER);
    rect(x,y,w,h);
  }

}

Box.pde

class Box {
  Body body;
  float w, h;

  Box(float x, float y) {
    w = 10;
    h = 10;

    makeBody(new Vec2(x, y), w, h);
  }

  void display() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    float a = body.getAngle();

    rectMode(CENTER);
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(-a);
    fill(127);
    stroke(0);
    strokeWeight(2);
    rect(0, 0, w, h);
    popMatrix();
  }

  boolean isOffScreen() {
    Vec2 pos = box2d.getBodyPixelCoord(body);

    if (pos.x > width || pos.x < 0 || pos.y > height || pos.y <0) {
      killBody();
      return true;
    } else {
      return false;
    }
  }
  
  void killBody(){
    box2d.destroyBody(body);
  }

  void makeBody(Vec2 center, float _w, float _h) {

    //create shape here
    PolygonShape ps = new PolygonShape();
    float box2dW = box2d.scalarPixelsToWorld(_w/2);
    float box2dH = box2d.scalarPixelsToWorld(_h/2);
    ps.setAsBox(box2dW, box2dH);

    //create fixture definition
    FixtureDef fd = new FixtureDef();
    fd.shape = ps;


    fd.density = 1;
    fd.friction = 0.3;
    fd.restitution = 0.5;

    //create body definition 
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DYNAMIC;
    bd.position.set(box2d.coordPixelsToWorld(center));

    body = box2d.createBody(bd);
    body.createFixture(fd);

    //body.setLinearVelocity(new Vec2(random(-5,5), random(2,5)));
    //body.setAngularVelocity(random(-5,5));
  }
}

JointPair.pde

class JointPair {
  Box b1, b2;

  float len;

  JointPair(float x, float y) {
    len = 50;

    b1 = new Box(x, y);
    b2 = new Box(x+5, y+5);

    DistanceJointDef djd = new DistanceJointDef();
    djd.bodyA = b1.body;
    djd.bodyB = b2.body;

    djd.length = box2d.scalarPixelsToWorld(len);

    djd.frequencyHz = 3; 
    djd.dampingRatio = 0.1;

    DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd);
  }

  void display(){
    Vec2 box1 = box2d.getBodyPixelCoord(b1.body);
    Vec2 box2 = box2d.getBodyPixelCoord(b2.body);
    
    stroke(0);
    strokeWeight(2);
    line(box1.x,box1.y,box2.x,box2.y);
    
    b1.display();
    b2.display();
    
  }
  
  boolean isPairOffScreen(){
    if(b1.isOffScreen() && b2.isOffScreen()){
      b1.killBody();
      b2.killBody();
      return true;
    }else{
      return false;
    }
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions