Famo.us对粒子添加​​排斥力?

时间:2014-06-27 23:57:50

标签: famo.us

基于how to add walls to a famo.us physics simulation?,有一个球会从墙壁反弹

你如何对粒子添加​​排斥力以使它永远不会撞击墙壁?

var Engine          = require('famous/core/Engine');
var Surface         = require('famous/core/Surface');
var EventHandler    = require('famous/core/EventHandler');
var View            = require('famous/core/View');
var Transform       = require('famous/core/Transform');

var StateModifier   = require('famous/modifiers/StateModifier');

var PhysicsEngine   = require('famous/physics/PhysicsEngine');
var Body            = require('famous/physics/bodies/Body');
var Circle          = require('famous/physics/bodies/Circle');
var Wall            = require('famous/physics/constraints/Wall');

var Repulsion       = require('famous/physics/forces/Repulsion');

module.exports = function () {

    var context = Engine.createContext();

    var contextSize = context.getSize();

    var handler = new EventHandler();

    var physicsEngine = new PhysicsEngine();

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

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

    ball.particle = new Circle({radius:100});

    var replusion = new Repulsion();
    ball.particle.applyForce(replusion);

    physicsEngine.addBody(ball.particle);

    ball.on("click",function(){
      ball.particle.setVelocity([1,1,0]);
    });

    context.add(ball.state).add(ball)

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

    physicsEngine.attach( leftWall,  [ball.particle]);
    physicsEngine.attach( rightWall, [ball.particle]);
    physicsEngine.attach( topWall,   [ball.particle]);
    physicsEngine.attach( bottomWall,[ball.particle]);

    Engine.on('prerender', function(){
      ball.state.setTransform(ball.particle.getTransform())
    });
};

我的第一个想法是对球的粒子添加排斥力,但这似乎并没有起作用。

有没有人这样做过,还是有这种行为的好文档?

1 个答案:

答案 0 :(得分:0)

我已经玩过了,我觉得我现在有一个更好的主意。

这个代码在同一个物理引擎中有两个球(粒子)会击退(一个会追逐另一个)

var context = Engine.createContext();

var contextSize = context.getSize();

var handler = new EventHandler();

var physicsEngine = new PhysicsEngine();

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

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

ball.particle = new Circle({radius:100});

var ball2 = new Surface ({
  size: [200,200],
  properties: {
    backgroundColor: 'green',
    borderRadius: '100px'
  }
})

ball2.state = new StateModifier({origin:[0.3,0.3]});

ball2.particle = new Circle({radius:100});

var leftWall    = new Wall({normal : [1,0,0],  distance : contextSize[0]/4.0, restitution : 0.3});
var rightWall   = new Wall({normal : [-1,0,0], distance : contextSize[0]/4.0, restitution : 0.3});
var topWall     = new Wall({normal : [0,1,0],  distance : contextSize[1]/4.0, restitution : 0.3});
var bottomWall  = new Wall({normal : [0,-1,0], distance : contextSize[1]/4.0, restitution : 0.3});

physicsEngine.attach( leftWall,  [ball.particle, ball2.particle]);
physicsEngine.attach( rightWall, [ball.particle, ball2.particle]);
physicsEngine.attach( topWall,   [ball.particle, ball2.particle]);
physicsEngine.attach( bottomWall,[ball.particle, ball2.particle]);

physicsEngine.addBody(ball.particle); 

physicsEngine.addBody(ball2.particle);

必须使用目标和来源将排斥力添加到PE中,这两者都必须已存在于引擎中。

var replusion = new Repulsion({strength: 90});
physicsEngine.attach(replusion, [ball2.particle], ball.particle);

ball.on("click",function(){
  ball.particle.setVelocity([1,0,0]);
});


ball2.on("click",function(){
  ball2.particle.setVelocity([1,1,0]);
});

context.add(ball.state).add(ball)
context.add(ball2.state).add(ball2)

Engine.on('prerender', function(){
  ball.state.setTransform(ball.particle.getTransform())
  ball2.state.setTransform(ball2.particle.getTransform())
});