java - Box2D 形状未渲染

标签 java libgdx box2d

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

相关文章:

java - 向现有 java 对象添加方法和值

java - com.mongodb.MongoSocketOpenException : Exception opening socket(MongoDB, docker )

java - Libgdx 不会在 requestRendering 上呈现

LibGDX And​​roid 游戏 - 在滚动屏幕上显示固定文本(即分数)

java - 破损的box2d主体,找到特定的固定装置,单独的固定装置

JavaFX:如何同时移动 2 个矩形?

java Apache POI Word 现有表格插入带有单元格样式和格式的行

java - 运动体中的碰撞检测 LIBGDX

java - 使用 Polygonshape 创建夹具时 Box2D 抛出错误

javascript - 使用 Crafty.js 和 Box2d 绘制一个简单的圆圈