Skip to content

Instantly share code, notes, and snippets.

@john45traver
Last active January 4, 2016 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john45traver/0b45745857091ac89033 to your computer and use it in GitHub Desktop.
Save john45traver/0b45745857091ac89033 to your computer and use it in GitHub Desktop.
Simple Fluid Simulation with Famo.us
// Note: Edit to Force.js
// The Engine sends applyForce a list of bodies, even when a single body is attached
// eg..
// engine.attach(gravity,body)
// DOES NOT WORK
// applyForce to work with the current physics engine must be defined as follows:
//
// Force.prototype.applyForce = function applyForce(bodies) {
// for (var i = 0; i < bodies.length; i++) {
// b = bodies[i];
// b.applyForce(this.force);
// };
// };
define(function(require, exports, module) {
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var Modifier = require('famous/core/Modifier');
var Draggable = require('famous/modifiers/Draggable');
var PhysicsEngine = require('famous/physics/PhysicsEngine');
var Circle = require('famous/physics/bodies/Circle');
var Force = require('famous/physics/forces/Force');
var Collision = require('famous/physics/constraints/Collision');
var Wall = require('famous/physics/constraints/Wall');
var Random = require('famous/math/Random');
var pe = new PhysicsEngine();
var context = Engine.createContext();
// Play with this Number
var numBodies = 100;
var moleculeBodies = [];
for (var i = 0; i < numBodies; i++) {
// Play with this range
radius = Random.integer(5,25);
var molecule = new Surface({
size:[radius*2,radius*2],
properties: {
backgroundColor: 'black',
borderRadius: radius + 'px',
}
});
molecule.body = new Circle({
radius:radius*0.5,
mass: 1
});
pe.addBody(molecule.body);
moleculeBodies.push(molecule.body);
molecule.state = new Modifier({origin:[0.5,0.5]});
molecule.state.transformFrom( function(){ return this.getTransform()}.bind(molecule.body) );
context.add(molecule.state).add(molecule);
};
var collision = new Collision({restitution:0});
for (var i = 0; i < moleculeBodies.length; i++) {
b1 = moleculeBodies[i];
if ((i+1) < moleculeBodies.length) {
for (var j = i+1; j < moleculeBodies.length; j++) {
b2 = moleculeBodies[j];
pe.attach(collision,b1,b2);
};
}
};
var gravity = new Force([0,0.002,0]);
pe.attach(gravity,moleculeBodies);
var floor = new Wall({normal:[0,-1,0], distance:200, restitution : 0});
pe.attach(floor, moleculeBodies);
var left = new Wall({normal:[1,0,0],distance:150, restitution:0 });
pe.attach(left,moleculeBodies);
var right = new Wall({normal:[-1,0,0],distance:150, restitution:0 });
pe.attach(right,moleculeBodies);
var dragger = new Surface({
size:[300,300],
properties: {
border: "2px solid black",
borderRadius: '150px'
}
});
dragger.body = new Circle({
radius:150,
mass: 100
});
pe.addBody(dragger.body);
dragger.state = new Modifier({origin:[0.5,0.5]});
dragger.draggable = new Draggable();
dragger.pipe(dragger.draggable);
Engine.on('prerender',function(){
var pos = dragger.draggable.getPosition();
dragger.body.setPosition([pos[0],pos[1]]);
});
pe.attach(collision,moleculeBodies,dragger.body);
context.add(dragger.state).add(dragger.draggable).add(dragger);
});
@amcc
Copy link

amcc commented Jun 30, 2014

i'm getting an error at line 66:
Uncaught TypeError: undefined is not a function (its actually at famous.min.js:48, but comes originally from the above line)

Has something changed in famo.us since you made this - i'm using the famo.us boilerplate and am pasting this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment