gpt4 book ai didi

java - Box2D 形状未渲染

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:18 25 4
gpt4 key购买 nike

我一直在查看其他一些线程,尽管我尝试了所有方法,但我在 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);


}

}

最佳答案

我将列出一些解决方案,因为代码片段中并非所有内容都清楚:

  1. 您确定 worldRender() 方法正在运行吗
    • 如果您使用 Game 和 Screen,请确保您的游戏 render() 方法调用 super(),否则您的 Screen render() 方法将不会运行;
  2. 如前所述,PPM 的值是否正确/它是什么?

这是否绘制:

// 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 的圆(确保这些点位于您的视口(viewport)中。

关于java - Box2D 形状未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46676797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com