0

我尝试构建一个小“汽车”来熟悉 Matter.js API。我几乎做到了,当奇怪的事情发生时。我有一个主体,每个都与两个约束相连,连接到两个轮子。但是一旦汽车到达地面,后轴就会慢慢地离开地面。这是一个错误,我该如何解决这个问题?代码:

start()
function start() {
    var Engine = Matter.Engine,
        World = Matter.World,
        Bodies = Matter.Bodies;
        Constraint = Matter.Constraint;
        Render = Matter.Render;

    var engine = Engine.create(document.body);
    var body = Bodies.rectangle(200, 20, 200, 40);
    body.density = 15;
    var wheelA = Bodies.circle(50,100,40);
    var wheelB = Bodies.circle(350,100,40);
    wheelA.density = 10;
    wheelB.density = 10;
    var axisAA = Constraint.create({bodyB:wheelA,bodyA:body,pointA:{x:-90,y:-15},pointB:{x:0,y:0},stiffness:0.8}, { isStatic: true });
    var axisAB = Constraint.create({bodyB:wheelA,bodyA:body,pointA:{x:-10,y:15},pointB:{x:0,y:0},stiffness:0.2});
    var axisBA = Constraint.create({bodyB:wheelB,bodyA:body,pointA:{x:90,y:-15},pointB:{x:0,y:0},stiffness:0.8});
    var axisBB = Constraint.create({bodyB:wheelB,bodyA:body,pointA:{x:10,y:15},pointB:{x:0,y:0},stiffness:0.2});
    var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });

    World.add(engine.world, [body, wheelA, wheelB, axisAA, axisAB, axisBA, axisBB, ground]);

    document.addEventListener('keydown', function(event) {
    if(event.keyCode == 39) {
        Matter.Body.setAngularVelocity(wheelA,0.1);
        Matter.Body.setAngularVelocity(wheelB,0.1);
    }
    else if(event.keyCode == 37) {
        Matter.Body.setAngularVelocity(wheelA,-0.1);
        Matter.Body.setAngularVelocity(wheelB,-0.1);
    }
    });
    Engine.run(engine);
}

感谢您希望得到答案。

4

1 回答 1

0

有一个已知问题,某些约束设置无法很好地解决,看起来就是这种情况,请参见此处:https ://github.com/liabru/matter-js/issues/93

这是一个可以工作的简单汽车的示例:http: //brm.io/matter-js/demo/#car

但请注意,由于尚未实施旋转约束,除非您使轴约束非常松散,否则它们仍然会有点不稳定。

于 2016-02-15T21:04:46.477 回答