我正在尝试使用box2d和libgdx在Android游戏中构建杠杆。游戏开始时,杠杆的两个平台处于完美的平衡状态,当我在杠杆的一个平台上移动一个盒子时,它按预期下降,但是当我将主体移出时,这两个平台不再像以前那样保持平衡。

我正在使用此代码创建2个平台:

private Body setupShape(World world, float cx, float cy, float cw, float ch){
    Body ptf;
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(cw / 2, ch / 2);
    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;
    fdef.density = 1.0f;
    fdef.friction = 1.0f;
    fdef.restitution = 0.0f;
    BodyDef bd = new BodyDef();
    //bd.allowSleep = false;
    bd.position.set(cx, cy);
    bd.fixedRotation = true;
    ptf = world.createBody(bd);
    //lever.setGravityScale(10);
    ptf.createFixture(fdef);
    ptf.setType(BodyDef.BodyType.DynamicBody);
    ptf.setUserData("Lever");
    shape.dispose();
    return ptf;
}


以及使用“ PulleyJoint”创建杠杆的代码:

PulleyJointDef pulleyDef = new PulleyJointDef();
    Vector2 bodyoneanchor = new Vector2(Constants.Pixel2SIf(360), Constants.Pixel2SIf(400));
    Vector2 bodytwoanchor = new Vector2(Constants.Pixel2SIf(660), Constants.Pixel2SIf(400));
    pulleyDef.initialize(bodyA, bodyB, bodyoneanchor, bodytwoanchor, bodyA.getWorldCenter(), bodyB.getWorldCenter(), 1);
    world.createJoint(pulleyDef);


我想知道我的代码有什么问题,以及在从中移出对象后如何使杠杆再次重新平衡。

这是一张图,可以更清楚地说明我的问题:
http://costheta.net/quest.png

如果您能帮助我解决该问题,将不胜感激。

最好的祝福!

更新:
这是添加棱镜关节后的新代码:

PulleyJointDef pulleyDef = new PulleyJointDef();
    Vector2 bodyoneanchor = new Vector2(Constants.Pixel2SIf(360), Constants.Pixel2SIf(400));
    Vector2 bodytwoanchor = new Vector2(Constants.Pixel2SIf(660), Constants.Pixel2SIf(400));
    pulleyDef.initialize(bodyA, bodyB, bodyoneanchor, bodytwoanchor, bodyA.getWorldCenter(), bodyB.getWorldCenter(), 1);
    world.createJoint(pulleyDef);

    BodyDef bd0 = new BodyDef();
    bd0.position.set(bodyoneanchor.x, bodyoneanchor.y);
    Body ptf0 = world.createBody(bd0);
    PrismaticJointDef pjd0 = new PrismaticJointDef();
    pjd0.initialize(ptf0, bodyA, ptf0.getWorldCenter(), new Vector2(0, 1));
    pjd0.motorSpeed = 200f;
    pjd0.maxMotorForce = 30.0f;
    pjd0.enableMotor = true;
    pjd0.lowerTranslation = -Math.abs(bodyA.getWorldCenter().y - bodyoneanchor.y);
    pjd0.upperTranslation = 0;//Math.abs(bodyA.getWorldCenter().y - bodyoneanchor.y);
    pjd0.enableLimit = true;
    world.createJoint(pjd0);

    BodyDef bd1 = new BodyDef();
    bd1.position.set(bodytwoanchor.x, bodytwoanchor.y);
    Body ptf1 = world.createBody(bd1);
    PrismaticJointDef pjd1 = new PrismaticJointDef();
    pjd1.initialize(ptf1, bodyB, ptf1.getWorldCenter(), new Vector2(0, 1));
    pjd1.motorSpeed = 200f;
    pjd1.maxMotorForce = 30.0f;
    pjd1.enableMotor = true;
    pjd1.lowerTranslation = -Math.abs(bodyB.getWorldCenter().y - bodytwoanchor.y);
    pjd1.upperTranslation = 0;//Math.abs(bodyB.getWorldCenter().y - bodytwoanchor.y);
    pjd1.enableLimit = true;
    world.createJoint(pjd1);

最佳答案

它们不应该重新平衡,因为每一侧的重量都相同,并且张力彼此抵消,因此不会产生任何运动(获取一些琴弦并在现实生活中搭建装置)。

如果要使其重新平衡,请在每个平台上添加弹簧,并在框的初始位置和中心放置长度为零的弹簧。

08-26 02:50