首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Box2D体不呈现和崩溃

Box2D体不呈现和崩溃
EN

Stack Overflow用户
提问于 2016-07-03 17:18:19
回答 1查看 67关注 0票数 0

我只是想在屏幕上呈现一个Box2D体。

这里是我的核心类代码:

代码语言:javascript
复制
 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的代码

代码语言:javascript
复制
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;
}

}

那么,当我运行这个时会发生什么呢?我得到以下错误:

代码语言:javascript
复制
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)中的语句;

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

我测试了它是否是世界,世界说其中有一个身体,所以它在注册,但我只是不明白它为什么要抛出这个错误,而不是渲染。任何帮助都是非常感谢的,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-04 12:29:58

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

代码语言:javascript
复制
    @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);

它应该是:

代码语言:javascript
复制
        CircleShape shape = new CircleShape();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38172435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档