简体   繁体   中英

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

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.

I had trouble like this. I suggest you to export your project to open it in an professional IDE like Intellij Idea. My project in IDEA shows me full stack trace with trouble code in jbox2d library. I developed a video game. But my trouble appears only in the android version by loading a game round after the main menu. In the desktop version everything is OK. I fixed this error. The level loading was implemented in a separate thread. I changed the loading from the separate thread to the main game loop ( draw() function).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM