简体   繁体   English

Box2D主体未渲染并崩溃

[英]Box2D Body not rendering and crashing

I am simply trying to render a Box2D body on the screen. 我只是想在屏幕上渲染Box2D实体。

Here is my Core class code: 这是我的核心课程代码:

 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);

}

} }

And this is the code for the EntityParent : 这是 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;
}

} }

So what happens when I run this I get the following error: 因此,当我运行此命令时会发生以下错误:

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

If I get rid of the statement from the core class p.initBodyVariables(1, 1, 1); 如果我摆脱了核心类的声明p.initBodyVariables(1,1,1);

I simply get a blank screen. 我只是得到一个黑屏。

I tested to see if it was the world and the world said there is one body in it so it is registering, but I just don't get why it is throwing this error and not rendering at all. 我测试了一下是否是世界,并且世界说其中有一个物体,所以它正在注册,但是我只是不明白为什么它会引发此错误而不是完全渲染。 Any help is greatly appreciated thanks! 任何帮助,万分感谢!

You are using wrong type of shape - PolygonShape instead of CircleShape when creating circles: 创建圆形时,您使用了错误的形状类型-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);

It should be: 它应该是:

        CircleShape shape = new CircleShape();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM