[KindaSolved]Is box2D really neccessary ?

Im having a hard time with box2d, seriously, i was able to draw balls on the screen and made them crash at the ground and rebounce.

I took out the camera thing to see if it improved, well, it did.
Now 0,0 is really 0,0

But if i draw my grass blocks on the screen, that i made with box2d Editor the balls get really small, i mean, VERY VERY small.

Before that, i had a normal class with the camera optimized like in the example, and the grass sprites simple fly over the screen or doesnt show up. It gets all bugged.
Though, the balls go down, rebounce and down again, its preety cool and fun and funny.
But i wanted to replace some ground with the grassBlocks, like this :

package grassproject;

import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import java.util.Random;

/**
 *
 * @author André Vinícius Lopes
 */
public class App extends ApplicationAdapter {

    private static final float VIEWPORT_WIDTH = 15;
    private OrthographicCamera camera;
    private float w;
    private float h;
    //
    private World world;
    //Ball
    private Sprite[] ballSprites;
    private Texture ballTexture;
    private int MAX_BALLS = 20;
    private Body[] ballModels;
    private static final float BALL_RADIUS = 0.15f;
    private Sprite grassSprite;
    private Texture grassTexture;
    //Ground :: 
    private Sprite groundSprite;
    private Texture groundTexture;
    // Render general
    private SpriteBatch batch;
    private final Random rand = new Random();
    private final TweenManager tweenManager = new TweenManager();
    private Body grassModel;
    private float GRASS_WIDTH = 512;
    private Vector2 grassModelOrigin;

    @Override
    public void create() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        //
        world = new World(new Vector2(0, -10), true);
        batch = new SpriteBatch();

        //
        camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * h / w);
        camera.position.set(0, camera.viewportHeight/2, 0);
        camera.update();

        createSprites();
        createBalls();
        createGround();

    }

    private void createSprites() {
        ballTexture = new Texture(Gdx.files.internal("imagens/ball.png"));
        ballTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

        ballSprites = new Sprite[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i] = new Sprite(ballTexture);
            ballSprites[i].setSize(BALL_RADIUS * 2, BALL_RADIUS * 2);
            ballSprites[i].setOrigin(BALL_RADIUS, BALL_RADIUS);
        }

        grassTexture = new Texture(Gdx.files.internal("imagens/grassSmall.png"));
        createGrass();
        grassSprite = new Sprite(grassTexture);
        

        groundTexture = new Texture(Gdx.files.internal("imagens/white.png"));
        groundSprite = new Sprite(groundTexture);
        groundSprite.setSize(VIEWPORT_WIDTH, 1);
        groundSprite.setPosition(-VIEWPORT_WIDTH / 2, 0);
        groundSprite.setColor(Color.GREEN);

    }

    private void createGrass() {
        BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("GrassProject"));

        // 1. Create a BodyDef, as usual.
        BodyDef bd = new BodyDef();
        bd.position.set(10,10);
        bd.type = BodyDef.BodyType.StaticBody;

        
        // 2. Create a FixtureDef, as usual.
        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.3f;

        
        // 3. Create a Body, as usual.
        grassModel = world.createBody(bd);

        
        // 4. Create the body fixture automatically by using the loader.
        loader.attachFixture(grassModel, "grass02", fd, 1);
        grassModelOrigin = loader.getOrigin("grass02", 1).cpy();

    }

    private void createBalls() {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyDef.BodyType.DynamicBody;
        //ballBodyDef.position.x = getRandom();
        //ballBodyDef.position.y = 15;

        CircleShape shape = new CircleShape();
        shape.setRadius(BALL_RADIUS);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.4f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        ballModels = new Body[MAX_BALLS];

        Vector2 vec = new Vector2();

        for (int i = 0; i < MAX_BALLS; i++) {
            ballModels[i] = world.createBody(ballBodyDef);
            ballModels[i].createFixture(fd);

            float tx = rand.nextFloat() * 1.0f - 0.5f;
            float ty = camera.position.y + camera.viewportHeight / 2 + BALL_RADIUS;
            float angle = rand.nextFloat() * MathUtils.PI * 2;
            ballModels[i].setLinearVelocity(vec.set(0, 0));
            ballModels[i].setAngularVelocity(0);
            ballModels[i].setTransform(vec.set(tx, ty), angle);
            ballModels[i].setBullet(true);
            ballModels[i].setActive(true);
        }



        shape.dispose();
    }

    private void createGround() {
        BodyDef bd = new BodyDef();
        bd.position.set(0,0);
        bd.type = BodyDef.BodyType.StaticBody;

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(VIEWPORT_WIDTH, 1);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.8f;
        fd.restitution = 1f;
        fd.shape = shape;

        world.createBody(bd).createFixture(fd);

        shape.dispose();
    }

    @Override
    public void render() {
        GL10 gl = Gdx.gl10;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        tweenManager.update(1 / 60f);
        world.step(1 / 60f, 10, 10);


        Vector2 grassPos = grassModel.getPosition().sub(grassModelOrigin);
        grassSprite.setPosition(grassPos.x, grassPos.y);
        grassSprite.setOrigin(grassModelOrigin.x, grassModelOrigin.y);



        for (int i = 0; i < MAX_BALLS; i++) {
            Vector2 ballPos = ballModels[i].getPosition();
            ballSprites[i].setPosition(ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2);
            ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees);
            if (i == 0) {
                System.out.println(ballPos.x + " ; " + ballPos.y);
            }
        }

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        groundSprite.draw(batch);
        grassSprite.draw(batch);
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i].draw(batch);
        }
        batch.flush();
        batch.end();


    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

I really fail

package grassproject;

import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import java.util.Random;

/**
 *
 * @author André Vinícius Lopes
 */
public class DrawGrass implements ApplicationListener {

    private World world;
    private static final float VIEWPORT_WIDTH = 10;
    private OrthographicCamera camera;
    private float w, h;
    private Sprite grassSprite;
    private Texture grassTexture;
    private TweenManager tweenManager;
    private Body grassModel;
    private float GRASS_WIDTH = 256;
    private Vector2 grassModelOrigin;
    private SpriteBatch batch;
    //Ball
    private Sprite[] ballSprites;
    private Texture ballTexture;
    private int MAX_BALLS = 20;
    private Body[] ballModels;
    private static final float BALL_RADIUS = 0.15f;
    private final Random rand = new Random();

    @Override
    public void create() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        //
        world = new World(new Vector2(0, -10), true);
        batch = new SpriteBatch();

        camera = new OrthographicCamera();
     
        tweenManager = new TweenManager();
        grassTexture = new Texture(Gdx.files.internal("imagens/grassSmall.png"));
        grassSprite = new Sprite(grassTexture);

        BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("GrassProject"));

        
        // 1. Create a BodyDef, as usual.
        BodyDef bd = new BodyDef();
        bd.position.set(0, 0);
        bd.type = BodyDef.BodyType.StaticBody;


        // 2. Create a FixtureDef, as usual.
        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.3f;


        // 3. Create a Body, as usual.
        grassModel = world.createBody(bd);


        // 4. Create the body fixture automatically by using the loader.
        loader.attachFixture(grassModel,"grass02", fd, GRASS_WIDTH);
        grassModelOrigin = loader.getOrigin("grass02",GRASS_WIDTH).cpy();
        createGround();

        ///BALLS
        
        createBalls();
        ballTexture = new Texture(Gdx.files.internal("imagens/ball.png"));
        ballTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

        ballSprites = new Sprite[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i] = new Sprite(ballTexture);
            ballSprites[i].setSize(BALL_RADIUS * 2, BALL_RADIUS * 2);
            ballSprites[i].setOrigin(BALL_RADIUS, BALL_RADIUS);
        }

        


    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void render() {
        GL10 gl = Gdx.gl10;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        tweenManager.update(1 / 60f);
        world.step(1 / 60f, 10, 10);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
       
        grassSprite.draw(batch);
        
        for (int i = 0; i < MAX_BALLS; i++) {
            Vector2 ballPos = ballModels[i].getPosition();
            ballSprites[i].setPosition(ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2);
            ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees);
           ballSprites[i].draw(batch);
        }

        batch.flush();
        batch.end();


    }

    private void createBalls() {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyDef.BodyType.DynamicBody;
        ballBodyDef.position.x = 200;
        ballBodyDef.position.y = 100;

        CircleShape shape = new CircleShape();
        shape.setRadius(BALL_RADIUS);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.4f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        ballModels = new Body[MAX_BALLS];

        Vector2 vec = new Vector2();

        for (int i = 0; i < MAX_BALLS; i++) {
            ballModels[i] = world.createBody(ballBodyDef);
            ballModels[i].createFixture(fd);

            float tx = rand.nextFloat() * 1.0f - 0.5f;
            float ty = camera.position.y + camera.viewportHeight / 2 + BALL_RADIUS;
            
            float angle = rand.nextFloat() * MathUtils.PI * 2;
            ballModels[i].setLinearVelocity(vec.set(0, 0));
            ballModels[i].setAngularVelocity(0);
            ballModels[i].setTransform(vec.set(tx, ty), angle);
            ballModels[i].setBullet(true);
            ballModels[i].setActive(true);
        }
    }

    private void createGround() {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 0);
        bd.type = BodyDef.BodyType.StaticBody;

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(VIEWPORT_WIDTH,1);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.8f;
        fd.restitution = 1f;
        fd.shape = shape;

        world.createBody(bd).createFixture(fd);

        shape.dispose();
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

Heres a picture of whats happening :

http://img19.imageshack.us/img19/4169/box2derror.jpg

Ok, it’s getting to the point of where you’re asking WAY too many questions. Slow down, take your time, read the tutorials and reread them, and fix the problem by yourself. Your 21 topics started are almost all about something that we told you about in a previous question - I recommend atlases, you start posting 3 topics on atlases, someone starts you on box2d, you post 2+ topics on box2d. Not all your problems are going to be solved in a day or less, and to be successful in the industry, you have to be more independent. I’m getting the feeling that 99% of your code is either copied from tutorial/example code, or stuff that we’re giving you, which isn’t right.

Im sorry, i will try to research more.

Jimmt, is this tutorial any good?
http://sierakowski.eu/list-of-tips/114-box2d-basics-part-1.html

Is C++ i think, but i know a little of C++, Probably will give me an idea of whats actually happening.

I mean, box2D of libgdx = any box2D, right ?

Any c++ box2d tutorials are pretty much good for libgdx’s version. JBox2D, maybe not. But yeah, iforce2d, etc are pretty much all the same.

You won’t really get that far if you just keep following tutorials for specific purposes and copying their code. Most of these assume prior programming knowledge.

Start small. Don’t use box2d physics until you have a firm grip of Java and LibGDX concepts, and can debug on your own.

Programming requires a lot of debugging and solving problems on your own. If you can’t be arsed, maybe another tool like Game Maker would be better suited for you. :smiley:

hey, i will make it.
Im just kinda lost, its all new to me, except java.
I have a need for tutorials to explain to me whats happening in the code, not just copying. Im trying to learn what each object does, and whats the logic the guy is using.

And no, game maker , no. seriously, no,thats like the biggest insult ever :smiley: , dont be rude!!! xD

Not an insult, but good advice - if you don’t want to/can’t debug - like your recent posts seem to show, then Game Maker would be better.

Also: Only use a physics engine if you really it much of it.
In most cases hacking some physics stuff in is enough.
If your game primarily relies on physics go for it. If not, then not.

There many primarily casual games which focus on “lets look at physics - and maybe play with it”, like angry birds. Then you will want it.
If not, then there is no reason.

Do no treat box2D as a default goto solution to simple physics problems.

Oh ok, then i can make a 2D game , like sonic without box2D ?
Because i dont feel ready for it.

I like to debug, its just somewhat confusing.I am learning.

Absolutely.
Box2D would be way way more than you need in Sonic.
We are creating a Metroid/Castlevania type game - its all improvised physics.

Hm, i am thinking how will i be able to draw the map and check the collisions, because, when its about checking collisions with rectangles is ok.But Its a whole Sprite of the level is way different, because i will have a whole giant rectangle.
So i had the idea of making a txt with positions of each tile, and draw them and treat each one separately.

Like, i could have a txt like this :

[TXT}

grass.png

x = 0 ; y = 0
x = 5 ; y = 0
x = 10 ; y = 0

}TXT}

Supposing the grass.png is 5 in width;

Is that a good approach ?

cannot follow you completely, but in a tilemap every tile is a rectangle, there are exceptions like slopes, but either a tile is filled or not.
and since everything is a rectangle you can collision check them.

how you check with objects/players/sprites/entities depends on what kinda of collision boundaries they have.
the main approaches are: one big rectangle, one rectangle which is a little smaller than the whole image, multiple rectangles as many as you need and you can also use polygons - its of course possible to use polygon collision without box2D

performance wise: only check rectangle/tiles in the vicinity and it is possible with a static map to combine rectangles I guess

Oh, if i make like, a tmx file, like the wiki tutorial describes, will i be able to do that?
I will, wont i? So i actually never really needed box2D.

Because as far as im researching, tmx just tells where to draw the tile, and thats what i need.
Then i can load the sprite with the atlas, like in the tutorial, and make the collision test :smiley:

Something like that, right ?

Patience is a virtue. Learn one thing at a time. You’re burning yourself out.

I have never used tmx/Tiled before, so I can’t say.
But I’m 99.9% sure that it doesn’t need box2D is any way.

It doesn’t “need” box2d in be sense that you can definitely load and render a map without it. However, box2d makes it significantly easier to do collision if you assign collision boundaries to each tile - article and code here: http://dpk.net/2011/05/01/libgdx-box2d-tiled-maps-full-working-example-part-1/

Uses the deprecated tiled map package but it shouldn’t be a problem.

It kinda is.

Well, anyway, im now stalled at this :
http://www.java-gaming.org/topics/tmx-x-y-doesnt-match-libgdx-x-y-how-to-already-tried-several/29794/view.html

Would you give me any tip :smiley: ???
preety plx