Libgdx box2d body moves slowly

This is a very simple scene with box2d. I tried different viewports and different screen sizes. I could not understand why the body falls very slowly. In fact, I'm not quite sure that it is slow, the reason is that these are viewing options, etc. This is the main class:

public class Main extends Game {
LevelScreen levelScreen;

@Override
public void create () {
    levelScreen = new LevelScreen();
    setScreen(levelScreen);

}

@Override
public void render () {
    super.render();
}
}

And level screen:

public class LevelScreen extends Stage implements Screen {

private Batch batch;
private Camera camera;
private Texture ballTexture;
private Sprite ball;
private Viewport viewport;
//com

private Vector3 point = new Vector3();

private World world;
private Box2DDebugRenderer box2DDebugRenderer;

private CircleShape circleShape;
private FixtureDef fixtureDef;
private BodyDef bodyDef;
private Body circleBody;

private static final float SCENE_WIDTH = 1080;
private static final float SCENE_HEIGHT = 1920f;


public LevelScreen() {
    super(new FitViewport(SCENE_WIDTH, SCENE_HEIGHT,  new OrthographicCamera(SCENE_WIDTH, SCENE_HEIGHT)));


    batch = getBatch();
    camera = getCamera();
    viewport = getViewport();

    world = new World(new Vector2(0,-9.8f), true);
    box2DDebugRenderer = new Box2DDebugRenderer();
    bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(600, 1000);

    ballTexture = new Texture("ball.png");
    ball = new Sprite(ballTexture);
    ball.setPosition(0,0);

    circleShape = new CircleShape();
    circleShape.setRadius(25f);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = circleShape;
    fixtureDef.density = 0.5f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0.6f;

    circleBody = world.createBody(bodyDef);
    circleBody.createFixture(fixtureDef);

    box2DDebugRenderer = new Box2DDebugRenderer(
            true, /* draw bodies */
            false, /* don't draw joints */
            true, /* draw aabbs */
            true, /* draw inactive bodies */
            false, /* don't draw velocities */
            true /* draw contacts */);

    Gdx.input.setInputProcessor(this);


}

@Override
public void show() {
    System.out.println("show");
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    ball.draw(batch);

    batch.end();

    world.step(1 / 60f, 6, 2);
    ball.setPosition(circleBody.getPosition().x - 25f, circleBody.getPosition().y - 25f);
    box2DDebugRenderer.render(world, viewport.getCamera().combined);

}

@Override
public void resize(int width, int height) {
    viewport.update(width, height);
    System.out.println("resize");
}

@Override
public void pause() {
    System.out.println("pause");
}

@Override
public void resume() {
    System.out.println("resume");
}

@Override
public void hide() {
    System.out.println("hide");
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    viewport.getCamera().unproject(point.set(screenX, screenY, 0));
    return false;
}
}

enter image description here

+4
source share
2 answers

You should use a small camera for box2d, because box2d works better with 0-10 values. Here is your screen level class. Give it a try.

    public class LevelScreen extends Stage implements Screen {

         private Batch batch;
         private Camera camera;
         private Texture ballTexture;
         private Sprite ball;
         private Viewport viewport;


         private Vector3 point = new Vector3();

private World world;
private Box2DDebugRenderer box2DDebugRenderer;

private CircleShape circleShape;
private FixtureDef fixtureDef;
private BodyDef bodyDef;
private Body circleBody;

private static final float SCENE_WIDTH = 28;
private static final float SCENE_HEIGHT = 48f;


public LevelScreen() {
    super(new FitViewport(SCENE_WIDTH, SCENE_HEIGHT,  new OrthographicCamera(SCENE_WIDTH, SCENE_HEIGHT)));


    batch = getBatch();
    camera = getCamera();
    viewport = getViewport();

    world = new World(new Vector2(0,-9.8f), true);
    box2DDebugRenderer = new Box2DDebugRenderer();
    bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(10, 28);

    ballTexture = new Texture("ball.png");
    ball = new Sprite(ballTexture);
    ball.setPosition(0,0);

    circleShape = new CircleShape();
    circleShape.setRadius(1f);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = circleShape;
    fixtureDef.density = 0.5f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0.6f;

    circleBody = world.createBody(bodyDef);
    circleBody.createFixture(fixtureDef);

    box2DDebugRenderer = new Box2DDebugRenderer(
            true, /* draw bodies */
            false, /* don't draw joints */
            true, /* draw aabbs */
            true, /* draw inactive bodies */
            false, /* don't draw velocities */
            true /* draw contacts */);

    Gdx.input.setInputProcessor(this);


}

@Override
public void show() {
    System.out.println("show");
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    ball.draw(batch);

    batch.end();

    world.step(1 / 60f, 6, 2);
    ball.setPosition(circleBody.getPosition().x - 25f, circleBody.getPosition().y - 25f);
    box2DDebugRenderer.render(world, viewport.getCamera().combined);

}

@Override
public void resize(int width, int height) {
    viewport.update(width, height);
    System.out.println("resize");
}

@Override
public void pause() {
    System.out.println("pause");
}

@Override
public void resume() {
    System.out.println("resume");
}

@Override
public void hide() {
    System.out.println("hide");
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    viewport.getCamera().unproject(point.set(screenX, screenY, 0));
    return false;
}
}
+2
source

Well, I can't make you understand a simple comment. Try by code:

Screen

public class TestScreen implements Screen {
    SpriteBatch batch;
    OrthographicCamera camera;

    World world;
    Box2DDebugRenderer dr;

    Ball ball;


    public TestScreen() {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(1.6f, 1f); // <---- Very small camera so it passes by fast since less surface is being shown

        world = new World(new Vector2(0, -9.8f), true);

        ball = new Ball(.11f, world); // <---- Create ball and pass in the diameter

        //Try playing with the value of camera let say we have a ball the size of planet earth:
        //ball = new Ball(6371, world);

        //Now zoom out the screen so we can see our planet sized ball
        //camera = new OrthographicCamera(16000, 10000);

        //Believe me, our planet ball falls as fast as the little soccer ball.       //But since you zoomed out each pixel represents so much more distance.


        dr = new Box2DDebugRenderer(true, false, false, false, false, false);
    }

    @Override
    public void show() {

    }

    @Override
    public void render (float delta) {
        Gdx.gl.glClearColor(.1f, .1f, .14f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(camera.combined);

        world.step(Gdx.graphics.getDeltaTime(), 6, 2);

        batch.begin();
        ball.draw(batch);
        batch.end();

        dr.render(world, camera.combined);

    }
 //... Other mandatory screen methods
}

Ball.java

public class Ball {

    private float radius;

    private CircleShape shape;
    private FixtureDef fixtureDef;
    private BodyDef bodyDef;
    private Body circleBody;

    private Texture ballTexture;

    public Ball(float radius, World world) {
        this.radius = radius;

        ballTexture = new Texture("sprites/soccerball.png");

        bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(0, 0);

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

        fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 0.5f;
        fixtureDef.friction = 0.4f;
        fixtureDef.restitution = 0.6f;

        circleBody = world.createBody(bodyDef);
        circleBody.createFixture(fixtureDef);
    }

    public void draw(SpriteBatch batch)
    {
        batch.draw(ballTexture, circleBody.getPosition().x - radius, circleBody.getPosition().y - radius,
                radius * 2, radius * 2); // <---- draw the size you give it in the physics engine
    }
}

Run what you did wrong:

9,8 . , , - . , ( ), , .

25 , 1920 . , 2 , . , , 9,8 .

, , . 11 , 0,11 . , , . ( , ).

, , , . , vp , . , - . , .

, , , , .

+2

Source: https://habr.com/ru/post/1629668/


All Articles