无法让JBox2d Testbed工作

时间:2014-03-25 13:14:47

标签: jbox2d testbed

我已经按照本教程和测试平台启动了,但什么也没发生过。 GUI出现但测试从未运行,它只是坐在那里。测试平台从驱动程序类启动,然后添加测试平台测试。其他人有这个问题吗?

驱动程序类

public class Driver {
public static final String GRAVITY_SETTING = "Gravity";
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    TestbedModel model = new TestbedModel();         // create our model

    // add tests
    model.addCategory("My Tests");
    model.addTest(new MJWTest2());
    model.addTest(new VerticalStack());

    // add our custom setting "My Range Setting", with a default value of 10, between 0 and 20
    model.getSettings().addSetting(new TestbedSetting(GRAVITY_SETTING, SettingType.ENGINE, false));

    TestbedPanel panel = new TestPanelJ2D(model);    // create our testbed panel
    JFrame testbed = new TestbedFrame(model, panel, null); // put both into our testbed frame
    // etc
    testbed.setVisible(true);
    testbed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Testbed Test class

public class MJWTest2 extends TestbedTest {
    public static final String GRAVITY_SETTING = "Gravity";


    @Override
    public void initTest(boolean argDeserialized) {
        setTitle("Couple of Things Test");

        getWorld().setGravity(new Vec2());

        for (int i = 0; i < 2; i++) {
            PolygonShape polygonShape = new PolygonShape();
            polygonShape.setAsBox(1, 1);
            FixtureDef fix = new FixtureDef();
            fix.shape = polygonShape;

            BodyDef bodyDef = new BodyDef();
            bodyDef.type = BodyType.DYNAMIC;
            bodyDef.position.set(5 * i, 0);
            bodyDef.angle = (float) (Math.PI / 4 * i);
            bodyDef.allowSleep = false;
            Body body = getWorld().createBody(bodyDef);
            body.createFixture(fix);

            body.applyForce(new Vec2(-10000 * (i - 1), 0), new Vec2());
        }
    }
@Override
public void step(TestbedSettings settings) {
    super.step(settings); // make sure we update the engine!
    TestbedSetting gravity = settings.getSetting(GRAVITY_SETTING); // grab our setting
    if (gravity.enabled) {
      getWorld().setGravity(new Vec2(0, -9));
    }
    else {
      getWorld().setGravity(new Vec2());
    }
  }

    @Override
    public String getTestName() {
        return "Couple of Things";
    }
}

2 个答案:

答案 0 :(得分:1)

有一个更新到引擎没有更新维基。哎呦!抱歉。您需要创建一个控制器并启动,如下所示: https://github.com/dmurph/jbox2d/blob/master/jbox2d-testbed/src/main/java/org/jbox2d/testbed/framework/j2d/TestbedMain.java

答案 1 :(得分:0)

试试这个

public class MJWTest2 extends TestbedTest {

   @Override
   public void initTest(boolean argDeserialized) {
      setTitle("Couple of Things Test");

      getWorld().setGravity(new Vec2());

      for (int i = 0; i < 2; i++)
      {
//         CircleShape circleShape = new CircleShape();
//         circleShape.m_radius = 1;
//         Shape shape = circleShape;
         PolygonShape polygonShape = new PolygonShape();
         polygonShape.setAsBox(1, 1);
         Shape shape = polygonShape;

         BodyDef bodyDef = new BodyDef();
         bodyDef.type = BodyType.DYNAMIC;
         bodyDef.position.set(5 * i, 0);
         bodyDef.angle = (float) (Math.PI / 4 * i);
         bodyDef.allowSleep = false;
         Body body = getWorld().createBody(bodyDef);
         body.createFixture(shape, 5.0f);

         body.applyForce(new Vec2(-10000 * (i - 1), 0), new Vec2());
      }
   }

   /**
    * @see org.jbox2d.testbed.framework.TestbedTest#getTestName()
    */
   @Override
   public String getTestName() {
      return "Couple of Things";
   }
}

所谓的&#39;驱动程序类&#39;

public class App2 {

public static final String GRAVITY_SETTING = "Gravity";

public static void main(String[] args) {
    // TODO Auto-generated method stub
    TestbedModel model = new TestbedModel();         // create our model

    // add tests
    model.addCategory("My Tests");
    model.addTest(new MJWTest2());
    model.addTest(new VerticalStack());

    // add our custom setting "My Range Setting", with a default value of 10, between 0 and 20
    model.getSettings().addSetting(new TestbedSetting(GRAVITY_SETTING, SettingType.ENGINE, false));

    TestbedPanel panel = new TestPanelJ2D(model);    // create our testbed panel
    JFrame testbed = new TestbedFrame(model, panel); // put both into our testbed frame
    // etc
    testbed.setVisible(true);
    testbed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

对我来说很好..希望它也适合你..