简体   繁体   English

如何将DebugDraw与纯JBox2D结合使用?

[英]How would I use DebugDraw with pure JBox2D?

Okay, so this is my code (this is just a test of using DebugDraw): 好的,这是我的代码(这只是使用DebugDraw的测试):

package test;
import org.jbox2d.callbacks.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

public class Main {
private static DebugDraw debugDraw;

public static DebugDraw getDebugDraw() {
    return debugDraw;
}
public static void main(String[] args) {
    Vec2  gravity = new Vec2(0,-10);
boolean doSleep = true;
World world = new World(gravity,doSleep);
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(0, -10);
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox(50,10);
groundBody.createFixture(groundBox, 0);

// Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(0, 4);
Body body = world.createBody(bodyDef);
PolygonShape dynamicBox = new PolygonShape();
dynamicBox.setAsBox(1, 1);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicBox;
fixtureDef.density=1;
fixtureDef.friction=0.3f;
body.createFixture(fixtureDef);

// Setup world
float timeStep = 1.0f/60.0f;
int velocityIterations = 6;
int positionIterations = 2;

// Run loop
for (int i = 0; i < 60; ++i)
{
    world.step(timeStep, velocityIterations, positionIterations);
    Vec2 position = body.getPosition();
    float angle = body.getAngle();
    debugDraw.setFlags(debugDraw.e_shapeBit);
    world.setDebugDraw(debugDraw);
    System.out.println(i+": X: "+position.x+" Y: "+position.y+" ANGLE: "+angle);
}

}
}

When I run this code, I get: 运行此代码时,我得到:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NullPointerException
at test.Main.main(Main.java:49)
Java Result: 1

Does anyone know what's causing this and what I should do? 有谁知道是什么原因造成的,我该怎么办? I have tried google searches, but all I can find is Slick2D, which I do not want to implant a whole library to test one simple test application. 我已经尝试过Google搜索,但是我只能找到Slick2D,我不想植入整个库来测试一个简单的测试应用程序。

I had the same problem with jbox2d. 我对jbox2d也有同样的问题。 It has a dependency on slf4j. 它与slf4j有关。 The easiest way to solve it is to do a maven clean install in the jbox2d root directory. 解决该问题的最简单方法是在jbox2d根目录中进行Maven全新安装。 You will then find the slf4j libraries in the .m2 directory (which itself should be in your home directory). 然后,您将在.m2目录(它应该在您的主目录中)中找到slf4j库。

You then need to add the slf4j library to your project build path and export its jar file together with the target project. 然后,您需要将slf4j库添加到项目构建路径,并将其jar文件与目标项目一起导出。

That's the theory. 那是理论。 In reality slf4j has a dependency itself on log4j which is not resolved by maven. 实际上,slf4j本身依赖于log4j,而maven无法解决该依赖关系。 So in the end I commented out all Log references in jbox2d, and did another maven clean install. 因此,最后我注释掉了jbox2d中的所有Log引用,并进行了另一个maven全新安装。 That solved the issue for me. 那为我解决了这个问题。

FYI, the slf4j dependency has been removed in the latest version, too many people were having trouble with it. 仅供参考,slf4j依赖项已在最新版本中删除,太多人对此感到麻烦。

The null pointer exception looks like you're doing something not quite right. 空指针异常看起来像您在做的事情不太正确。 Perhaps you should make a DebugDraw implementation? 也许您应该执行DebugDraw实现? It will be null otherwise. 否则将为null。

If you want to play around, it's best to just make a Testbed test. 如果您想玩转,最好只做一个Testbed测试。 Follow the wiki here 此处关注维基

"debugDraw" is null causing "debugDraw.setFlags(debugDraw.e_shapeBit)" to fail with an NPE. “ debugDraw”为空,导致“ debugDraw.setFlags(debugDraw.e_shapeBit)”因NPE而失败。

Create a class that extends DebugDraw and assign it to your static variable. 创建一个扩展DebugDraw的类,并将其分配给您的静态变量。 You need to implement the draw callbacks your self using whatever graphics lib you are using, eg Swing. 您需要使用所使用的任何图形库(例如,Swing)来实现自己的绘制回调。 But for starters you can also just write out log statements. 但是对于初学者来说,您也可以只写出日志语句。

Also, you should move the "debugDraw.setFlags(debugDraw.e_shapeBit)" and "world.setDebugDraw(debugDraw)" statements up before the gameloop, and instead put a "world.drawDebugData()" statement in the gameloop after the world step. 同样,您应该在游戏循环之前将“ debugDraw.setFlags(debugDraw.e_shapeBit)”和“ world.setDebugDraw(debugDraw)”语句上移,而在世界步之后将“ world.drawDebugData()”语句放入gameloop中。

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

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