Pookie
Pookie

Reputation: 1279

Box2D Body not rendering and crashing

I am simply trying to render a Box2D body on the screen.

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:

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

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!

Upvotes: 0

Views: 83

Answers (1)

m.antkowicz
m.antkowicz

Reputation: 13571

You are using wrong type of shape - PolygonShape instead of CircleShape when creating circles:

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

Upvotes: 1

Related Questions