libgdx box2d摩擦接头不起作用

时间:2016-05-13 11:15:36

标签: libgdx box2d

我试图用box2d的摩擦接头做一个简单的例子。我创建了一个可以用鼠标或箭头键控制的圆圈。还有一个用圆圈推动的盒子。问题是,当我推开盒子时,它会一直移动。我添加了一个辅助静态盒子,所以我可以用两个带有摩擦接头的盒子来模拟自顶向下的摩擦力。但它不起作用。这是代码。

public class MyGdxGame extends ApplicationAdapter {

    Camera camera;
    int windowWidth, windowHeight;

    World physicsWorld;
    Box2DDebugRenderer debugRenderer;

    Body frictionerBox;

    Body boxBody;
    Body circleBody;

    @Override
    public void create () {
        camera = new OrthographicCamera(2, 2);

        Box2D.init();
        physicsWorld = new World(new Vector2(0, 0), true);
        debugRenderer = new Box2DDebugRenderer();

        // frictioner box
        BodyDef bDef = new BodyDef();
        bDef.type = BodyDef.BodyType.StaticBody;
        bDef.position.set(0, 0);
        frictionerBox = physicsWorld.createBody(bDef);
        PolygonShape fShape = new PolygonShape();
        fShape.setAsBox(10, 10);
        FixtureDef fixDef = new FixtureDef();
        fixDef.shape = fShape;
        fixDef.density = 1.f;
        fixDef.friction = 1.f;
        //Fixture fix =
            frictionerBox.createFixture(fixDef);

        // box
        bDef = new BodyDef();
        bDef.type = BodyDef.BodyType.DynamicBody;
        bDef.position.set(0, 50);
        boxBody = physicsWorld.createBody(bDef);
        PolygonShape bShape = new PolygonShape();
        bShape.setAsBox(25, 25);
        fixDef = new FixtureDef();
        fixDef.shape = bShape;
        fixDef.density = 1.f;
        fixDef.friction = 1.f;
        boxBody.createFixture(fixDef);

        // circle
        BodyDef cDef = new BodyDef();
        cDef.type = BodyDef.BodyType.DynamicBody;
        cDef.position.set(20, 20);
        circleBody = physicsWorld.createBody(cDef);
        CircleShape cShape = new CircleShape();
        cShape.setRadius(12);
        fixDef = new FixtureDef();
        fixDef.shape = cShape;
        fixDef.density = 1.f;
        circleBody.createFixture(fixDef);

        FrictionJointDef frictionJointDef = new FrictionJointDef();
        frictionJointDef.maxForce = 10;
        frictionJointDef.maxTorque = 10;
        frictionJointDef.initialize(frictionerBox, boxBody, new Vector2(0, 0));
        physicsWorld.createJoint(frictionJointDef);

        bShape.dispose();
        cShape.dispose();
        fShape.dispose();
    }

    @Override
    public void render () {       

        handleInput();

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


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

        debugRenderer.render(physicsWorld, camera.combined);

    }


    @Override
    public void resize(int width, int height)
    {
        camera.viewportWidth = width;
        camera.viewportHeight = height;
        camera.update();
        windowWidth = width;
        windowHeight = height;
        System.out.println("resized");
    }

    private void handleInput()
    {
        final float ARROW_SPEED = 60.0f;
        final float MAX_SPEED = 200.f;
        final float MIN_SPEED = 20.f;
        float vx, vy;
        if(Gdx.input.isTouched() || Gdx.input.isButtonPressed(Input.Buttons.LEFT))
        {
            int tx = (Gdx.input.getX() - windowWidth/2);
            int ty = -Gdx.input.getY() + windowHeight/2;
            float cx = camera.position.x;
            float cy = camera.position.y;
            float px = circleBody.getPosition().x;
            float py = circleBody.getPosition().y;
            px = px - cx;
            py = py - cy;
            vx = tx - px;
            vy = ty - py;
            float speed = (float) Math.sqrt(vx*vx + vy*vy);
            if( speed > MAX_SPEED )
            {
                vx = (vx/speed) * MAX_SPEED;
                vy = (vy/speed) * MAX_SPEED;
            }
            if( speed > 0 && speed < MIN_SPEED )
            {
                vx = (vx/speed) * MIN_SPEED;
                vy = (vy/speed) * MIN_SPEED;
            }
        }

        else
        {
            vx = vy = 0;
            if(Gdx.input.isKeyPressed(Input.Keys.W))
            {
                vy += 1;
            }
            if(Gdx.input.isKeyPressed(Input.Keys.S))
            {
                vy -= 1;
            }
            if(Gdx.input.isKeyPressed(Input.Keys.A))
            {
                vx -= 1;
            }
            if(Gdx.input.isKeyPressed(Input.Keys.D))
            {
                vx += 1;
            }
            float speed = (float) Math.sqrt(vx*vx + vy*vy);
            if(speed > 0.f)
            {
                vx = (vx/speed) * ARROW_SPEED;
                vy = (vy/speed) * ARROW_SPEED;
            }
        }

        circleBody.setLinearVelocity(vx, vy);

    }

}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我明白了。似乎为了使摩擦显着,您必须将非常高的值设置为maxForcemaxTorque

frictionJointDef.maxForce = 1000000;
frictionJointDef.maxTorque = 10000000;