need some support. I am novice in libgdx game engine. Below is my code

package com.game.box2d.fruitcart;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Rectangle;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.physics.box2d.World;

public class FruitCartBox2D extends Game{

    World world;
    Box2DDebugRenderer debugRenderer;
    OrthographicCamera camera;

    static final float BOX_STEP = 1 / 60f;
    static final int BOX_VELOCITY_ITERATIONS = 6;
    static final int BOX_POSITION_ITERATIONS = 2;
    static final float WORLD_TO_BOX = 0.01f;
    static final float BOX_WORLD_TO = 100f;

    Sprite backgroundSprite;
    Sprite grassSprite;
    Sprite bucketSprite;
    Sprite fruitSprite;
    Music backgroundMusic;
    Sound dropSound;
    TextureAtlas atlas;
    SpriteBatch batch;
    Body body;
    public void create() {

        world = new World(new Vector2(0, -10), true);
        camera = new OrthographicCamera();
        camera.viewportHeight = 480;
        camera.viewportWidth = 800;
        camera.position.set(camera.viewportWidth * .5f,
                camera.viewportHeight * .5f, 0f);

        atlas = new TextureAtlas(Gdx.files.internal("data/packed.txt"));

        backgroundSprite = atlas.createSprite("background_original");
        grassSprite = atlas.createSprite("grass");
        bucketSprite = atlas.createSprite("bucket");
        fruitSprite = atlas.createSprite("apple");
        batch = new SpriteBatch();
        bucketSprite.setBounds(800 / 2 - 80 / 2, 35,80, 80);

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.set(camera.viewportWidth / 2,
                camera.viewportHeight / 2);

        body = world.createBody(bodyDef);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = new PolygonShape();
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 1.0f;
        fixtureDef.restitution = 1;


        debugRenderer = new Box2DDebugRenderer();

    public void render() {

        Gdx.gl.glClearColor(0, 0, 0, 1);

        debugRenderer.render(world, camera.combined);



        Sprite sprite = (Sprite) body.getUserData();
        sprite.setBounds(100, 400, 40, 45);


In above code, I have added a background in the form of sprites.It's working fine. After that, there is a fruitSprite, now I want to handle it with box2d so that I can apply gravity on fruitSprite. To achieve this, I've created a dynamic body, added fixture. Now, the problem is here : when I add a polygon shape to fixture and than attached to a body, it gives an unexpected error. It doesn't show any stacktrace. Following is the snapshot of error encountered.


If, I remove polygon shape from fixture, than it gives NullPointerException which shows that shape is mandatory for fixturedef.

I want to add fruitSprite to the box2d body so that I don't have to handle gravity of fruit.

Any help is appreciable, struggling from last 3-4 days.

  • \$\begingroup\$ This is very much a "debug my code" question. I suggest you ask in chat. \$\endgroup\$ – MichaelHouse Sep 12 '13 at 1:02

You haven't set any vertices on the shape. Create a new polygonshape and set the vertices of the shape using setAsBox or set(float[] vertices). Have a look at the documentation!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.