DynamicBody останавливается на StaticBody при езде со скоростью - PullRequest
1 голос
/ 28 марта 2019

Я создаю приложение на LibGDX с Box2D , и когда я даю скорость body(dynamic) и устанавливаю трение на 0, но тело останавливается во время езды на платформе,Как мне решить эту проблему?

Я пытался изменить трение или скорость тела, а также изменить положение блоков на платформе (сделать их ближе друг к другу).

Класс экрана

private World world;
    private Box2DDebugRenderer b2dr;
    private OrthographicCamera camera;
    private Player player;
    private SpriteBatch spriteBatch;
    private Block[] blocks;

    @Override
    public void show() {
        spriteBatch = new SpriteBatch();
        world = new World(new Vector2(0,-9.81f),true);
        b2dr = new Box2DDebugRenderer();
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 640/PPM,480/PPM);
        world.setContactListener(new MyContactListener());
        blocks = new Block[100];

        Body body;
        PolygonShape shape = new PolygonShape();
        FixtureDef fixtureDef = new FixtureDef();
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;

        for(int i = 0;i<blocks.length;i++){
            bodyDef.position.set((320+64*i)/PPM,240/PPM);

            body = world.createBody(bodyDef);
            shape.setAsBox(32/PPM,32/PPM,new Vector2(32/PPM,32/PPM), 0);
            fixtureDef.shape = shape;

            blocks[i] = new Block(Block.BlockType.GRASS, body.createFixture(fixtureDef));
        }

        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(320/PPM,400/PPM);
        body = world.createBody(bodyDef);
        shape.setAsBox(27.5f/PPM,32/PPM,new Vector2((27.5f)/PPM,(32f)/PPM), 0);
        fixtureDef.shape = shape;
        fixtureDef.friction = 0;
        body.setSleepingAllowed(false);

        player = new Player(body.createFixture(fixtureDef));
        body.setUserData(player);
    }

    @Override
    public void render(float delta) {
        update(delta);

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

        b2dr.setDrawContacts(true);
        b2dr.render(world, camera.combined);
        camera.update();

        spriteBatch.setProjectionMatrix(camera.combined);
        spriteBatch.begin();
        player.draw(spriteBatch,1);
        for (Block block : blocks) block.draw(spriteBatch, 1);
        spriteBatch.end();
    }

    private void update(float dt){
        //if(Gdx.input.isKeyPressed(Input.Keys.SPACE))
        world.step(dt,6,2);
        player.update(dt);

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
            player.fixture.getBody().setLinearVelocity(5,player.fixture.getBody().getLinearVelocity().y);
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
            player.fixture.getBody().setLinearVelocity(-5,player.fixture.getBody().getLinearVelocity().y);
        if(Gdx.input.isKeyJustPressed(Input.Keys.UP) && player.getState() != State.JUMPING && player.getState() != State.FALLING)
            player.fixture.getBody().setLinearVelocity(player.fixture.getBody().getLinearVelocity().x,7.5f);

        if(Gdx.input.isKeyPressed(Input.Keys.W))
            camera.zoom+=0.1;
        if(Gdx.input.isKeyPressed(Input.Keys.S))
            camera.zoom-=0.1;
        if(Gdx.input.isKeyPressed(Input.Keys.A))
            camera.translate(-30/PPM,0);
        if(Gdx.input.isKeyPressed(Input.Keys.D))
            camera.translate(30/PPM,0);
    }
}

Класс блока

public class Block extends Sprite {
    public Fixture fixture;

    public Block(Texture texture, Fixture fixture){
        this.fixture = fixture;

        setTexture(texture);
        setRegion(texture);
        setBounds(fixture.getBody().getPosition().x,fixture.getBody().getPosition().y,texture.getWidth()/ Var.PPM, texture.getHeight()/ Var.PPM);
    }

    static public class BlockType{
        public static final Texture GRASS = new Texture("grass.png");
        public static final Texture GROUND = new Texture("ground.png");
    }
}

Итак, я увеличил камеру и вижу один пиксель, который останавливает мое тело.

Image 1

Image 2

...