2014-07-01 70 views
3

我是新來的'物理引擎世界'。我決定使用Famo.us進行試驗並試圖學習如何使用物理引擎。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()) 
     } 
    }); 
}); 
+0

你弄明白了嗎?我無法使用這裏的答案將衝突添加到您的示例中...很想看看您是如何做到的。你可以分享源代碼嗎? – Pueggel

回答

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循環要明確

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

感謝您的幫助@dmvaldman。現在它工作正常。這是我沒有得到的部分。我仍然需要制定出Gravity的東西......我在互聯網上找到了一個可以工作的例子(http://hbsand.com/HappyBoxes/),但我無法在自己的代碼中實現它。我必須錯過一些東西......但我還沒有放棄。我會繼續調查。 :) – user3794206