2017-06-17 61 views
2

我正在嘗試重新創建流體拖動模型,如 paragraph 2.2.1 of this paper中所見。在這個this youtube movie(我發現這篇論文的地方)中可以看到一個工作版本。在box2d/matter.js中查找運動的正常和切向分量

在論文中,他們聲明他們計算軟體邊緣速度的法向力和切向力。我試圖理解如何從邊緣速度到這兩個力分量。不過,我只能找到關於基於函數計算組件的資源(例如this),並且我正在努力將其轉換爲我的物理環境。什麼是實現這種流體阻力模型的方法?

這裏是一個小提琴顯示我的環境: https://jsfiddle.net/Swendude/1nnckp5p/

// module aliases 
var Engine = Matter.Engine, 
    Render = Matter.Render, 
    World = Matter.World, 
    Bodies = Matter.Bodies, 
    Body = Matter.Body, 
    Vector = Matter.Vector, 
    Composite = Matter.Composite, 
    Composites = Matter.Composites, 
    Constraint = Matter.Constraint, 
    Events = Matter.Events; 

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

// create a canvas 
var canvas = document.getElementById("maincanvas"); 

var render = Render.create({ 
    element: document.body, 
    canvas: canvas, 
    engine: engine, 
    options: { 
     background: "#fff", 
     height: 400, 
     width: 400, 
     wireframes: false, 
    } 
}); 

engine.world.gravity = {x:0, y:0}; 

// Create a soft body composite, see 
// http://brm.io/matter-js/docs/classes/Composites.html#method_softBody 
var softbox = Composites.softBody(100,100,2,2,40,40,true,1); 

World.add(engine.world, softbox); 


// This functions makes some constraints move. 

function pulse(composite) { 
    var allcons = Composite.allConstraints(composite); 
    allcons[0].length += 5; 
    allcons[4].length += 5; 

    // Set a timeout to make the constraints short again 
    setTimeout(function (cons) { cons.length -= 5;}, 1000, allcons[0]); 
    setTimeout(function (cons) { cons.length -= 5;}, 1000, allcons[4]); 
} 

setInterval(pulse, 2000, softbox); 

function applyForcesOnEdge() { 
    var allcons = Composite.allConstraints(engine.world); 

    allcons.forEach(function(cons, index) { 
     // Edge speed defined as the average of both connected body's speed. 
     var constraintspeed = Vector.div(Vector.add(cons.bodyA.velocity, cons.bodyA.velocity),2); 
     if (constraintspeed.x != 0 && constraintspeed.y != 0) { 
      console.log(constraintspeed); // How to get the tangential and normal components from this? 
     } 
    }); 
} 

Events.on(engine, 'beforeUpdate', function() { 
    applyForcesOnEdge(); 
}) 


Engine.run(engine); 

Render.run(render); 

大部分是matter.js樣板,有趣的部分是功能:pulse()applyForcesOnEdge()

我使用matter.js ,但我可以想象,同樣的問題可能適用於box2d環境。

回答

1

只需在約束兩側的對象位置之間進行區分即可。最好在代碼中解釋:

allcons.forEach(function(cons, index) { 
    // Edge speed defined as the average of both connected body's speed. 
    var constraintspeed = Vector.div(
     Vector.add(
      cons.bodyA.velocity, 
      cons.bodyB.velocity), 
     2); 
    var constraintpos = Vector.div(
     Vector.add(
      cons.bodyA.position, 
      cons.bodyB.position), 
     2); 
    var tangent = Vector.normalise(
     cons.bodyB.position - 
     cons.bodyA.position); 
    var normal = Vector.perp(tangent); 
    var v_T = Vector.dot(constraintspeed, tangent); 
    var v_N = Vector.dot(constraintspeed, normal); 
    var F_T = - lambda_T * Math.sign(v_T) * v_T * v_T; 
    var F_N = - lambda_N * Math.sign(v_N) * v_N * v_N; 
    var F = Vector.add(
     Vector.mult(tangent, F_T), 
     Vector.mult(normal, F_N)); 
    Body.applyForce(cons.bodyA, constraintpos, F); 
    Body.applyForce(cons.bodyB, constraintpos, F); 
}); 
+0

這似乎是正確的答案。我現在唯一的問題是調整價值觀以獲得切合實際的行爲,而不是弄錯整個頁面。查看 這個小提琴是如何使用它的 - > https://jsfiddle.net/Swendude/1nnckp5p/5/。 (小心,它往往會崩潰)。謝謝! – SwenMulderij

+0

奇怪的是,它似乎在我的手機(iOS 10.3.2,Safari)上正常工作。 – cdo256

+0

嘗試更改值。 (在本文中,法向分量大約是切向大小的50倍) – SwenMulderij