我一直在寻找其他一些线程,尽管我尝试了所有的东西,但我在box2d中创建的形状都没有渲染。这非常离奇,我希望你们能提供解决方案。
public class worldRender {
fighterGame game;
PlayScreen renderGame;
private Viewport gamePort = new StretchViewport(1020 / game.PPM,760 / game.PPM);
World world = new World(new Vector2(0,-10), true);
Box2DDebugRenderer b2dr = new Box2DDebugRenderer();
private OrthographicCamera gameCam = new OrthographicCamera();
BodyDef bDef = new BodyDef();
public Body b2body;
FixtureDef fixtureDef = new FixtureDef();
ShapeRenderer shapeRender;
public worldRender() {
gameCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
gameCam.position.set(1020/2, 760/2, 0);
}
public worldRender(float dt) {
gameCam.update();
world.step(1/60f, 6, 2);
b2dr.render(world, gameCam.combined);
bodyRender();
}
public void bodyRender() {
BodyDef bdef = new BodyDef();
bdef.position.set(0.0f / game.PPM,4.0f / game.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
fdef.friction = 0.25f;
CircleShape shape = new CircleShape();
shape.setRadius(5);
fdef.shape = shape;
fdef.density = 1.0f;
b2body.createFixture(fdef);
}
}
答案 0 :(得分:0)
我要列出几个解决方案,因为片段中的所有内容都不清楚:
这画得:
// First we create a body definition
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;
// Set our body's starting position in the world
bodyDef.position.set(50, 50);
// Create our body in the world using our body definition
Body body = world.createBody(bodyDef);
// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(10f);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
// Create our fixture and attach it to the body
Fixture fixture = body.createFixture(fixtureDef);
// Remember to dispose of any shapes after you're done with them!
// BodyDef and FixtureDef don't need disposing, but shapes do.
circle.dispose();
这应该在x = 10,y = 10处画一个半径为10的圆(确保这些点在你的视口中。
答案 1 :(得分:0)
我建议稍微清理你的代码,并研究一些可能解决你的问题的教程。但是,让我们给你一些提示,让你在路上:
我怀疑你每帧都在创建一个worldRender对象。 Java是一种垃圾收集语言,这样做会严重影响您的性能。尽可能多地保留对象。在我的大型游戏中,我创建了每个渲染和游戏逻辑滴答的0个对象旁边。瞄准。
最后,可能会解决您的问题:用于渲染box2ddebugrenderer(“dbrndr”)的相机将屏幕像素作为单位。 dbrndr使用米作为渲染单位。你需要给dbrndr自己的相机以米为单位。您当前的方法将在左下角以0/4像素绘制一个10像素宽的圆圈。
你是否用重力创造了你的世界?如果是的话,圆圈会立即掉出你的屏幕......是的,你做到了。
你可能实际上甚至在开始后在左下角看到你的圆圈,因为你在做box2d逻辑之前进行了渲染。
请处置()您创建的所有对象,否则他们占用的内存不会被释放。