Java 使用box2d导入json,我看不到导入的对象
我在呈现用Box2D编辑器创建的json对象时遇到问题。 我的问题是,即使物体的隔膜(根据网上找到的教程是正确的),它也不会出现在舞台上Java 使用box2d导入json,我看不到导入的对象,java,json,libgdx,box2d,physics-engine,Java,Json,Libgdx,Box2d,Physics Engine,我在呈现用Box2D编辑器创建的json对象时遇到问题。 我的问题是,即使物体的隔膜(根据网上找到的教程是正确的),它也不会出现在舞台上 private void createBottle() { // 0. Create a loader for the file saved from the editor. BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("ball.json"));
private void createBottle() {
// 0. Create a loader for the file saved from the editor.
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("ball.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.position.set(spaceShip.getX(), spaceShip.getY());
bd.type = BodyDef.BodyType.DynamicBody;
// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;
// 3. Create a Body, as usual.
bottleModel = world.createBody(bd);
// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(bottleModel, "test01", fd, 8);
}
在create方法中,我有以下内容:
createBottle();
在渲染方法中:
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
bottleSprite.draw(batch);
我不明白为什么没有添加,我看不到屏幕上。。。。我有其他项目与z位置分配。。。但没有什么不想出现的
我的目标是进入迷宫使用box2d编辑器,并将移动到一个带有虚拟操纵杆的命令对象中,这就是我正在做的游戏
如果有人想告诉我如何成功导入,这是您第一次使用带有导入json的外部编辑器
编辑
在将我的问题发布到这里之前,我也试着遵循教程。
然而,错误发生在Vector2。。。。此外,教程是明确的,但不工作。。。甚至在网上我也看到了其他一些他们说不起作用的例子。。。没有资源在运行吗?我还没有找到您可以尝试,正如这里所述: LibGDX绘制图像时参考图像的左下角。因此,如果将参考点保留在身体的左下角,则可以直接将图像放置在参考点的位置(由Box2d引擎使用getPosition()方法返回)。否则,如果更改了参考点的位置,则需要考虑它来绘制图像 因此,您需要将参考点存储在某个位置,以供以后使用:
bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();
最后,在渲染方法中,可以在参考点位置绘制图像,该位置由相对于图像左下角的局部坐标偏移:
public void render() {
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
}
代码中的问题似乎是您没有一个名为bottleModelOrigin的变量,至少在第一个示例中它是不可见的。尽管在这种情况下应该抛出NullPointerException