2017-07-18 49 views
3

我在我的世界有一個球和兩個直的垂直表面。爲什麼我的圓形以一定角度從直線表面反彈?

當我對球施加一個力時,我希望它保持在一條直線上,但是它會以一個角度反彈。

小提琴:https://jsfiddle.net/zvjvvzeL/11/

var Engine = Matter.Engine, 
     Render = Matter.Render, 
     World = Matter.World, 
     Bodies = Matter.Bodies, 
     Body = Matter.Body, 
     Vector = Matter.Vector, 
     Events = Matter.Events; 

    // create an engine 
    var engine = Engine.create(); 

    var canvas = document.getElementById('canvas'); 

    engine.world.gravity.y = 0; // gravity not needed in this app 

    // create a renderer 
    var render = Render.create({ 
     element: document.body, 
     canvas: canvas, 
     engine: engine, 
     options: {wireframes: true} 
    }); 

    var ball_0 = Bodies.circle(100, 150, 11, { 
     density: 0.04, 
     frictionAir: 0.06, 
     restitution: 0.8, 
     friction: 0.3 
    }); 

    var cushion_left = Bodies.rectangle(34, 160, 100, 208, { isStatic: true }); 
    var cushion_right = Bodies.rectangle(492, 160, 100, 208, { isStatic: true }); 

    // add all of the bodies to the world 
    World.add(engine.world, [cushion_left, cushion_right, ball_0]); 

    render.options.height = 300; 
    canvas.height = 300; 
    Engine.run(engine); 
    Render.run(render); 

    Body.applyForce(ball_0, { x: 0, y: 0 }, { x: 0.5, y: 0 }); 
+1

關掉球上的摩擦力就可以固定它。 –

+0

球有旋轉嗎? –

+0

不旋轉我想模擬一個水池球,我想要布和桌子之間的摩擦。 –

回答

4

不太熟悉MatterJS,但它似乎是球已經默認應用角度旋轉。我認爲這隻會影響你所建立的封閉系統。

也許你會想它從長遠來看,但現在你可以設置intertia : Infinity

var ball_0 = Bodies.circle(100, 150, 11, { 
    density: 0.04, 
    frictionAir: 0.06, 
    restitution: 0.8, 
    friction: 0.3, 
    inertia : Infinity 
}); 

但現在你也必須申請略多力量讓球觸壁。我只是把它變成了.6

Body.applyForce(ball_0, { x: 0, y: 0 }, { x: .6, y: 0 }); 
+0

詛咒你離開.6的前導零! :P –

+2

intertia vs inertia –

+0

它仍然在奇怪的角度反彈,設置爲無窮大的慣性 - https://jsfiddle.net/zvjvvzeL/22/ –