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

相互碰撞时如何使球弹跳?我设法添加了球并创建了墙。球撞到墙壁时会弹跳,但撞到彼此时不会反弹,它们只是通过。我一直在尝试我能找到的任何东西,但是不幸的是,famo.us文档上信息的缺乏对我来说是一个障碍(因为我仍在学习概念)。

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

这是我要实现的目标: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())
        }
    });
});

最佳答案

问题是这一行:

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


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

要定义所有对粒子之间的碰撞,您可以执行以下操作:

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


等效地(认为不是最佳选择),可以使用double for循环来显式

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

关于famo.us - Famo.us-球互相弹跳,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24513003/

10-11 12:55