简体   繁体   English

处理 box2d 断言错误

[英]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) .我在调用body.createFixture(fd)时收到 AssertionError。

I am using ProcessingBox2D and I am following http://natureofcode.com/book/chapter-5-physics-libraries/ tutorial.我正在使用 ProcessingBox2D,我正在关注http://natureofcode.com/book/chapter-5-physics-libraries/教程。 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.编辑:没有堆栈跟踪,它只是说 AssertionError。 Link to screenshot here: https://imageshack.com/a/img922/1063/4DSsUz.png链接到这里的截图: 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.首先,w 和 h 最终为负,Box2D 可能断言 w>0 且 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.而且,无论如何,我总是绘制一个 10x10 的矩形,所以当我更改 w 和 h 时,它对绘制的内容没有影响。

I had trouble like this.我遇到了这样的麻烦。 I suggest you to export your project to open it in an professional IDE like Intellij Idea.我建议您导出您的项目以在像 Intellij Idea 这样的专业 IDE 中打开它。 My project in IDEA shows me full stack trace with trouble code in jbox2d library.我在 IDEA 中的项目向我展示了 jbox2d 库中带有故障代码的完整堆栈跟踪。 I developed a video game.我开发了一个视频游戏。 But my trouble appears only in the android version by loading a game round after the main menu.但是我的麻烦只出现在 android 版本中,在主菜单之后加载一个游戏。 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).我将加载从单独的线程更改为主游戏循环( draw()函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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