I found johntraver's answer above and it answered all of my questions re: implementing the physics engine. The one minor hiccup was that it didn't work for me (0.2.0). This was due to the fact that the contextSize was getting set to [0,0] so all of the walls were located at the center of the screen. This led me to turn the wall setup into a function and call it with Engine.nextTick(). This way the context has size when the walls are put into place. Then just for kicks I added another call to the same function on Engine resize. This also required that the walls be detached before being recreated to avoid creating an endless number of walls with a field that only got smaller.
Here is my tweak of the above answer:
/* globals define */
define(function(require, exports, module) {
'use strict';
// import dependencies
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');
function wallBounce() {
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});
physicsEngine.addBody(ball.particle);
ball.on("click",function(){
ball.particle.setVelocity([.1,.13,0]);
});
context.add(ball.state).add(ball);
function setWalls() {
//console.log(contextSize[0]);
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});
physicsEngine.detachAll();
physicsEngine.attach( leftWall, [ball.particle]);
physicsEngine.attach( rightWall, [ball.particle]);
physicsEngine.attach( topWall, [ball.particle]);
physicsEngine.attach( bottomWall,[ball.particle]);
};
Engine.nextTick(setWalls);
Engine.on('resize',setWalls);
Engine.on('prerender', function(){
ball.state.setTransform(ball.particle.getTransform())
});
};
new wallBounce();
});