Libgdx box2d body moves slow

后端 未结 2 850
庸人自扰
庸人自扰 2021-01-14 01:26

This is a very simple scene with box2d. I tried different viewports and different screen sizes. I could not figure out why the body drops very slow. Actually, I am not quite

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 01:43

    You should use small camera for box2d because box2d works better in 0-10 values. Here is your level screen class.Try it.

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

提交回复
热议问题