Famo.us - 球互相反弹

时间:2014-07-01 14:38:15

标签: famo.us

我是物理引擎世界的新手。我决定使用Famo.us进行实验并尝试学习如何使用物理引擎。

如何在击球时让球反弹?我设法添加球并创建墙壁。撞击墙壁时球会反弹,但是当击打对方时球不会弹出。我一直在尝试任何我能找到的东西,但不幸的是,缺乏关于famo.us doc的信息对我来说是一个小障碍(因为我还在学习这些概念)。

在实践中我不太了解的另一个概念是如何增加球的重力。

这就是我想要实现的目标:http://mrdoob.com/projects/chromeexperiments/ball-pool/

这是我到目前为止的代码:

define(function(require, exports, module) {
    var Engine          = require('famous/core/Engine');
    var Surface         = require('famous/core/Surface');
    var ContainerSurface         = require('famous/surfaces/ContainerSurface');
    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 Vector          = require('famous/math/Vector');

    var Collision       = require('famous/physics/constraints/Collision');


    var context = Engine.createContext();

    var contextSize;

    var mainSurface = new ContainerSurface ({
        size: [undefined,undefined],
        properties: {
            backgroundColor: '#dddddd'
        }
    });

    mainSurface.on("click",function(){
        addNewBall(1);
        setWalls();
    });


    var handler = new EventHandler();

    var physicsEngine = new PhysicsEngine();

    var balls = [];
    var collision = new Collision();
    function addNewBall(number){
        if(number == undefined) var number = 1;
        for(var i=0; i<number; i++){
            var index = balls.length;
            balls[index] = new Surface ({
              size: [50,50],
              properties: {
                backgroundColor: 'red',
                borderRadius: '50px'
              }
            })

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

            balls[index].particle = new Circle({radius:25});

            //Attempting to add collision
            physicsEngine.attach( collision,  [balls[index]]);

            balls[index].particle.applyForce(new Vector(Math.random(), 1, 0));

            physicsEngine.addBody(balls[index].particle);


            mainSurface.add(balls[index].state).add(balls[index]);
        }
    }

    context.add(mainSurface);
    addNewBall(1);

    //Function to loop through all balls and set the walls
    function setWalls() {
        contextSize = context.getSize();
        var leftWall    = new Wall({normal : [1,0,0],  distance : contextSize[0]/2.0, restitution : .5});
        var rightWall   = new Wall({normal : [-1,0,0], distance : contextSize[0]/2.0, restitution : .5});
        var topWall     = new Wall({normal : [0,1,0],  distance : contextSize[1]/2.0, restitution : .5});
        var bottomWall  = new Wall({normal : [0,-1,0], distance : contextSize[1]/2.0, restitution : .5});

        for(var i=0; i < balls.length; i++){
            physicsEngine.attach( leftWall,  [balls[i].particle]);
            physicsEngine.attach( rightWall, [balls[i].particle]);
            physicsEngine.attach( topWall,   [balls[i].particle]);
            physicsEngine.attach( bottomWall,[balls[i].particle]);
        }
    }


    Engine.nextTick(setWalls);
    Engine.on('resize',setWalls);

    Engine.on('prerender', function(){
        for(var i=0; i < balls.length; i++){
            balls[i].state.setTransform(balls[i].particle.getTransform())
        }
    });
});

2 个答案:

答案 0 :(得分:2)

问题在于这一行:

physicsEngine.attach( collision,  [balls[index]]);

.attach方法采用物理代理(Collision),目标数组(balls)和[可选]源。由于两个球之间的碰撞是,因此您需要定义碰撞源。同样,您可以在两个粒子之间连接SpringRepulsionDragGravity等不需要此。

要定义所有粒子对之间的碰撞,您可以这样做:

for (var i = 0; i < balls.length; i++) {
    physicsEngine.attach(collision, balls, balls[i]);
}

等效(略微不太理想),你可以使用双循环显式

for (var i = 0; i < balls.length; i++) {
    for (var j = 0; j < balls.length; j++) {
        if (i !== j) physicsEngine.attach(collision, balls[j], balls[i]);
    }
}

答案 1 :(得分:0)

团队doob?做着名:)

检查你的演示它似乎你现在正在运行,而且DMV也指出了具体细节。但就像另一种方法一样......我把一个演示放在一起(与着名人物帮助一起黑客马拉松)的球互相弹跳,墙壁等等。

http://famfiziks.meteor.com/battle

来源在这里: https://github.com/dcsan/fambot