Box2DBody没有渲染LibGDX

时间:2016-07-08 16:13:06

标签: java libgdx

我正在尝试渲染一个体,但它无法正常工作。其他身体渲染,但这不会。这是Core类中的代码。

@Override
public void create () {

    cam = new OrthographicCamera();
    cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    b2dCam = new OrthographicCamera();
    b2dCam.setToOrtho(false, Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);
    world = new World(new Vector2(0, -9.81f), true);

    rend = new Box2DDebugRenderer();

    batch = new SpriteBatch();


    entity = new CircleEntity(world, 100, 200, 10, BodyType.DynamicBody);
    entity.initBodyVariables(.1f, .1f, .1f);
    //entity.attachSprite("badlogic.jpg");
    BodyFactory.createBox(world, -1, 1f, 10000, .01f, BodyType.StaticBody, 1, 0, 0, "");

    RopeJoint j = new RopeJoint(200,300, world);
    j.create(4, 30);
}

    @Override
public void render () {
    b2dCam.update();
    cam.update();
    world.step(1/60f, 6, 2);
    entity.update();
    batch.setProjectionMatrix(b2dCam.combined);


    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    rend.render(world, b2dCam.combined);

    batch.begin();
    entity.render(batch);
    batch.end();
}

以下是bodyFactory的代码:

public static Body createBox(World world, float x, float y, float width,
        float height, BodyType type, float density, float friction, float restitution, Object data){
    bodyDef = new BodyDef();
    bodyDef.type = type;
    bodyDef.position.set(new Vector2(x, y));


    body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(width / Core.PPM, height / Core.PPM);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;
    fixture = body.createFixture(fixtureDef);
    fixture.setUserData(data);
    shape.dispose();


    return body;
}

使用此代码创建框时,会呈现它。我尝试创建这样的另一个体(在CircleEntity类中):

public class CircleEntity extends EntityParent {

/**
 * 
 * @param world
 *            of game
 * @param x
 *            position
 * @param y
 *            position
 * @param type
 *            of body (static, kinematic, dynamic)
 */
public CircleEntity(World world, float x, float y, float radius, BodyType type) {
    super(world, x, y, radius, type);
    createBody();
}

public void createBody() {
    bodyDef = new BodyDef();
    bodyDef.type = type;
    bodyDef.position.set(position);

    body = world.createBody(bodyDef);

    CircleShape entityShape = new CircleShape();
    entityShape.setRadius(radius / Core.PPM);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = entityShape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;
    fixture = body.createFixture(fixtureDef);
    entityShape.dispose();

}

@Override
public void attachSprite(String filePath) {
    super.attachSprite(filePath);
    if (entitySprite != null) {
        fixture.setUserData(entitySprite);
        body.setUserData(entitySprite);
        entitySprite.setPosition(position.x - dimensions.x, position.y - dimensions.y);
        entitySprite.setSize(dimensions.x, dimensions.y);
    }

}

@Override
public void update() {
    super.update();
    if (entitySprite != null)
        entitySprite.setPosition(position.x - dimensions.x, position.y - dimensions.y);

    if (Gdx.input.isKeyPressed(Keys.D))
        body.applyForce(10, 0, position.x, position.y, true);
    if (Gdx.input.isKeyPressed(Keys.A))
        body.applyForce(-10, 0, position.x, position.y, true);
}

@Override
public void render(SpriteBatch batch) {
    if (entitySprite != null)
        entitySprite.draw(batch);
}

}

当我调用它时(我在第一组代码中调用它)它不起作用。我无法弄清楚为什么只有这个实体不会渲染圆圈。碰撞仍然适用于身体,所以它在世界上,但我看不到圆圈本身。非常感谢任何帮助。

要清楚,Box2DBody没有渲染,Sprite是无关紧要的。

0 个答案:

没有答案