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())
});
};
我的第一個想法是增加斥力球的粒子,但似乎並沒有工作。
有沒有人做過這樣的事情,還是有這種行爲的良好文檔?
您還可以使用Circle作爲修改器,而不用使用'prerender''事件更新位置。 'context.add(ball.particle)。新增(球)' – BlinkyTop