如何限制Box2D中的旋转关节

时间:2018-12-06 23:35:49

标签: javascript box2d

我想限制旋转关节。下角度:-10度,最大:10度。但是尽管我使用了以下代码:

joint.lowerAngle = -10 * (Math.PI/180);
joint.upperAngle = 10 * (Math.PI/180);
joint.enableLimit = true;

它不起作用。 我想限制小车的旋转: enter image description here 我如何对这种关节进行硬性限制,以至于尽管有任何情况,马蹄形都不会彼此相对转动? 这是我的代码:

const createVagon = function(num) {

    let fixDef = new box2d.b2FixtureDef();
    fixDef.density = 1;
    fixDef.friction = 1;
    fixDef.restitution = 0;

    let bodyDef = new box2d.b2BodyDef();
    bodyDef.type = box2d.b2Body.b2_dynamicBody;
    bodyDef.position.Set(toMeters(VAGON_FIRST_POSX - (VAGONS_DISTANCE+VAGON_WIDTH)*num), toMeters(0));
    // bodyDef.angle = 0;

    vagons[num].body = world.CreateBody(bodyDef);

    fixDef.shape = new box2d.b2PolygonShape();
    fixDef.shape.SetAsBox(toMeters(VAGON_WIDTH), toMeters(VAGON_HEIGHT));

    vagons[num].body.CreateFixture(fixDef);

    let wheel = world.CreateBody(bodyDef);

    fixDef.shape = new box2d.b2CircleShape(toMeters(VAGON_WHEEL));

    wheel.CreateFixture(fixDef);

    let joint = new box2d.b2Joints.b2RevoluteJointDef();
    joint.bodyA = vagons[num].body;
    joint.bodyB = wheel;
    joint.localAnchorA = new box2d.b2Vec2(toMeters(VAGON_WIDTH-VAGON_WHEEL/2), toMeters(VAGON_HEIGHT+VAGON_WHEEL/2));
    joint.maxMotorTorque = 1.0;
    // joint.motorSpeed = 30.0;
    joint.enableMotor = true;
    joint = world.CreateJoint(joint);

    vagons[num].backWheel = joint;

    wheel = world.CreateBody(bodyDef);

    fixDef.shape = new box2d.b2CircleShape(toMeters(VAGON_WHEEL));

    wheel.CreateFixture(fixDef);

    joint = new box2d.b2Joints.b2RevoluteJointDef();
    joint.bodyA = vagons[num].body;
    joint.bodyB = wheel;
    joint.localAnchorA = new box2d.b2Vec2(-toMeters(VAGON_WIDTH-VAGON_WHEEL/2), toMeters(VAGON_HEIGHT+VAGON_WHEEL/2));
    joint.maxMotorTorque = 1;
    // joint.motorSpeed = 0.0;
    joint.enableMotor = true;
    joint = world.CreateJoint(joint);

    vagons[num].frontWheel = joint;

    bodyDef = new box2d.b2BodyDef();
    bodyDef.type = box2d.b2Body.b2_dynamicBody;
    let frontBind = world.CreateBody(bodyDef);

    vagons[num].frontBind = frontBind;

    fixDef.shape = new box2d.b2PolygonShape();
    fixDef.shape.SetAsBox(toMeters(VAGON_WHEEL/2), toMeters(VAGON_WHEEL/2));
    fixDef.filter.maskBits = 0x0000;

    frontBind.CreateFixture(fixDef);

    joint = new box2d.b2Joints.b2RevoluteJointDef();
    joint.bodyA = vagons[num].body;
    joint.bodyB = frontBind;
    joint.lowerAngle = -10 * (Math.PI/180);
    joint.upperAngle = 10 * (Math.PI/180);
    joint.enableLimit = true;
    joint.localAnchorA = new box2d.b2Vec2(toMeters(VAGON_WIDTH+VAGON_WHEEL/2), toMeters(0));
    joint = world.CreateJoint(joint);

    let backBind = world.CreateBody(bodyDef);

    vagons[num].backBind = backBind;

    backBind.CreateFixture(fixDef);

    joint = new box2d.b2Joints.b2RevoluteJointDef();
    joint.bodyA = vagons[num].body;
    joint.bodyB = backBind;
    joint.lowerAngle = -10 * (Math.PI/180);
    joint.upperAngle = 10 * (Math.PI/180);
    joint.enableLimit = true;
    joint.localAnchorA = new box2d.b2Vec2(-toMeters(VAGON_WIDTH+VAGON_WHEEL/2), toMeters(0));
    joint = world.CreateJoint(joint);

}

0 个答案:

没有答案