我尝试构建一个小“汽车”来熟悉 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);
}
感谢您希望得到答案。