Box2D Body无法渲染和崩溃

时间:2016-07-03 17:18:20

标签: java libgdx

我只是想在屏幕上渲染一个Box2D正文。

这是我的核心类代码:

 public class Core extends ApplicationAdapter {

private World world;
private Box2DDebugRenderer rend;
EntityParent p;
private OrthographicCamera cam;
@Override
public void create () {

    cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.setToOrtho(false);
    cam.viewportWidth = 640;
    cam.viewportHeight = 480;
    world = new World(new Vector2(0, -9.81f), true);

    rend = new Box2DDebugRenderer();


    p = new EntityParent(world, new Vector2(100, 100), BodyType.DynamicBody);
    p.initBodyVariables(1, 1, 1);
    p.createCircle(50);
}


@Override
public void render () {
    cam.update();
    world.step(1/60f, 6, 2);
    System.out.println(p.getPosition());

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

    rend.render(world, cam.combined);

}

}

这是 EntityParent的代码:

package com.reality.entity;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class EntityParent implements Entity{

/*
 * Used for initializing the body
 */
protected Vector2 position;
protected World world;

/*
 * All definitions for creating the body
 */
protected BodyType type;
protected BodyDef bodyDef;
protected Body body;
protected FixtureDef fixtureDef;
protected Fixture fixture;

protected float density, friction, restitution;

/**
 * Puts body by default at (0,0)
 * @param world
 * @param type
 */
public EntityParent(World world, BodyType type){
    this.world = world;
    this.type = type;
    this.position = new Vector2(0, 0);

}
/**
 * 
 * @param world
 * @param position of body
 * @param type
 */
public EntityParent(World world, Vector2 position, BodyType type){
    this.world = world;
    this.position = position;
    this.type = type;
}


/**
 * 
 * @param world
 * @param x position of body
 * @param y position of body
 * @param type
 */
public EntityParent(World world, float x, float y, BodyType type){
    this.world = world;
    this.position = new Vector2(x, y);
    this.type = type;
}

public void initBodyVariables(float density, float friction, float restitution){
    this.density = density;
    this.friction = friction;
    this.restitution = restitution;
}


@Override
public void createPolygonBody(float[] vertices) {

    bodyDef = new BodyDef();
    bodyDef.type = type;
    bodyDef.position.set(position);

    body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.set(vertices);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;

    fixture = body.createFixture(fixtureDef);
    shape.dispose();

}

@Override
public void createRectangle(Vector2 dimensions) {

    bodyDef = new BodyDef();
    bodyDef.type = type;
    bodyDef.position.set(position);

    body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(dimensions.x, dimensions.y);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;

    fixture = body.createFixture(fixtureDef);
    shape.dispose();
}

@Override
public void createCircle(float radius) {
    bodyDef = new BodyDef();
    bodyDef.type = type;
    bodyDef.position.set(position);

    body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setRadius(radius);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;

    fixture = body.createFixture(fixtureDef);
    shape.dispose();
}

@Override
public void update() {

}

@Override
public void render(Box2DDebugRenderer renderer) {

}




@Override
public Vector2 getPosition() {
    return this.position;
}

}

那么当我运行时会发生以下错误:

AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jre1.8.0_60\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp, Line 384

Expression: m_count >= 3

如果我摆脱核心类的声明     p.initBodyVariables(1,1,1);

我只是得到一个空白屏幕。

我测试看它是否是世界,世界上说它有一个身体,所以它正在注册,但我只是不明白它为什么抛出这个错误而根本没有渲染。非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

创建圆圈时,您使用了错误的形状类型 - PolygonShape 而不是 CircleShape

    @Override
    public void createCircle(float radius) {
        bodyDef = new BodyDef();
        bodyDef.type = type;
        bodyDef.position.set(position);

        body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape(); //here it should be CircleShape
        shape.setRadius(radius);

应该是:

        CircleShape shape = new CircleShape();