2

我正在尝试重新创建一个流体阻力模型,如 本文第 2.2.1 段所示在这部 youtube 电影中可以看到一个工作版本(我在哪里找到了这篇论文)。

在论文中,他们指出他们计算了软体边缘速度的法向力和切向力。我试图了解如何从边缘速度到这两个力分量。但是,我只能找到有关基于函数(例如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);

其中大部分是 matte.js 样板文件,有趣的部分是函数:pulse()applyForcesOnEdge()

我正在使用matter.js,但我可以想象同样的问题可能适用于 box2d 环境。

4

1 回答 1

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);
});
于 2017-07-08T09:12:52.947 回答