3

在我的问题面前,我的英语很冒险......

在我的 libgdx 应用程序中,我实现了 Box2D。所以我创建了一个 World 并添加了 2 个 Body。第一个 Body 是一个 PolygonShape 设置为盒子,它是静态的并且在我的世界中间旋转。第二个身体只是一个圆圈,它是动态的,落在第一个身体的顶部。当下降的圆圈与盒子碰撞时,我创建了一个 WeldJoint 并期望,圆圈现在也随着盒子旋转......但大约 20 度后,圆圈停止在盒子内旋转并且不要移动再...

这是一张图片: 像胶水一样正常工作

另一个: 停止他在盒子周围的旋转

我的代码:

BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.StaticBody;
    bodyDef.position.set(10, 8);

    mainBox = world.createBody(bodyDef);

    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(1, .5F);
    mainBox.createFixture(polygonShape, 1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.density = .1F;
    fixtureDef.friction = .5F;
    fixtureDef.restitution = .2F;

    Fixture f = mainBox.createFixture(fixtureDef);
    f.setUserData("mainbox");
    mainBox.setUserData("mainbox");

    /* ------------------------------------------------------------------------------------------ */

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

    shootBall = world.createBody(bodyDef);
    shootBall.setUserData("shootball");

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(.2F);


    fixtureDef = new FixtureDef();
    fixtureDef.shape = circleShape;
    fixtureDef.density = .5F;
    fixtureDef.friction = .5F;
    fixtureDef.restitution = .3F;

    f = shootBall.createFixture(fixtureDef);
    f.setUserData("shootball");

    circleShape.dispose();

    world.setContactListener(new ContactListener(){

        @Override
        public void beginContact(Contact contact) {
            if(contact.getFixtureA().getBody().getUserData().equals("mainbox") && contact.getFixtureB().getBody().getUserData().equals("shootball")){
                contact.getFixtureA().getBody().setUserData("createjoint");
                System.out.println("contact!");
            }
        }

在我的渲染循环中:

if(mainBox.getUserData().equals("createjoint")){
        WeldJointDef jointDef = new WeldJointDef();
        jointDef.bodyA = mainBox;
        jointDef.bodyB = shootBall;
        jointDef.initialize(mainBox, shootBall, mainBox.getPosition());
        world.createJoint(jointDef);
        mainBox.setUserData("mainbox");
    }

忽略不好的代码,它只是为了测试......

谢谢 :)

4

0 回答 0