0
\$\begingroup\$

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;
    @Override
    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);
        camera.update();

        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);
        body.setUserData(fruitSprite);

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

        body.createFixture(fixtureDef);

        debugRenderer = new Box2DDebugRenderer();
    }

    @Override
    public void render() {

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

        camera.update();
        debugRenderer.render(world, camera.combined);
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        batch.disableBlending();

        backgroundSprite.draw(batch);

        batch.enableBlending();
        grassSprite.draw(batch);
        bucketSprite.draw(batch);
        Sprite sprite = (Sprite) body.getUserData();
        sprite.setBounds(100, 400, 40, 45);
        sprite.draw(batch);
        batch.end();

        world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
    }
}

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.

error

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.

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

1 Answer 1

2
\$\begingroup\$

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!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .