2014-06-27 84 views
0

根據how to add walls to a famo.us physics simulation?有一個球會從牆上反彈Famo.us添加排斥粒子?

你如何去增加對粒子的斥力,使它不會撞到牆上?

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()) 
    }); 
}; 

我的第一個想法是增加斥力球的粒子,但似乎並沒有工作。

有沒有人做過這樣的事情,還是有這種行爲的良好文檔?

回答

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()) 
}); 
+2

您還可以使用Circle作爲修改器,而不用使用'prerender''事件更新位置。 'context.add(ball.particle)。新增(球)' – BlinkyTop