【问题标题】:Friction in famo.us?著名的摩擦?
【发布时间】:2014-07-02 07:09:34
【问题描述】:

如果我有一些被排斥的物体并且也有距离。现在他们无休止地来回弹跳。有没有办法给物理引擎增加阻力,让它们可以休息?

var context = Engine.createContext();

var contextSize = context.getSize();

var handler = new EventHandler();

var physicsEngine = new PhysicsEngine();

function addBall(color, i) {

    var ball = new Surface ({
      size: [50,50],
      properties: {
        backgroundColor: color,
        borderRadius: '100px'
      }
    });

    ball.state = new StateModifier({origin:[0.5,0.5]});

    ball.particle = new Circle({radius:50, mass: 20 + i * 1});

    return ball;
}
var leftWall    = new Wall({normal : [1,0,0],  distance : contextSize[0]/2.0, restitution : 0.7});
var rightWall   = new Wall({normal : [-1,0,0], distance : contextSize[0]/2.0, restitution : 0.7});
var topWall     = new Wall({normal : [0,1,0],  distance : contextSize[1]/2.0, restitution : 0.7});
var bottomWall  = new Wall({normal : [0,-1,0], distance : contextSize[1]/2.0, restitution : 0.7});
var walls = [leftWall,rightWall,bottomWall,topWall];

var bodies = [];

_.each(['yellow', 'blue','green', 'red', 'orange', 'purple','gray', 'black'],function(color, index) {
    var body = addBall(color, index);
    bodies.push(body);
});
_.each(bodies, function (body) {
    physicsEngine.addBody(body.particle);
    context.add(body.state).add(body);
});

var particles = _.map(bodies, function(body){
    return body.particle;
});

_.each(particles, function(particle, index){

    var r = new Repulsion({strength: 61});

    var d = new Distance({length: 80, minLength: 0});

    physicsEngine.attach(r, [particles[(index + 4) % 8]], particle);

    physicsEngine.attach(r, [particle], particles[(index + 4) % 8]);

    physicsEngine.attach(d, [particles[(index + 1) % 8]], particle);
    if (index == 0) {
        physicsEngine.attach(d, [particle], particles[7]);
    } else {
        physicsEngine.attach(d, [particle], particles[(index - 1) % 8]);
    }
    particle.setVelocity([0.004101*index,0.004101*index,0 ]);

});

_.each(walls, function(wall) {
    physicsEngine.attach(wall);

});


Engine.on('prerender', function(){
  _.each(bodies, function(body) {
        body.state.setTransform(body.particle.getTransform());
    });
});

【问题讨论】:

    标签: famo.us


    【解决方案1】:

    我在找 Drag!

    var drag = new Drag({strength: 0.1})
    
    physicsEngine.attach(drag, [particle]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多