When I set the balls origin to [0.5,0.5] the walls are placed correctly, when i set its transform or origin to any other location the walls move as well?
I have tried to explicitly set the engines origin but that doesn't work either
this code is copy paste able into main.js in the famous starter kit
define(function(require, exports, module) {
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 $ = require('jquery');
var StateModifier = require('famous/modifiers/StateModifier');
var Modifier = require('famous/core/Modifier');
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 context = Engine.createContext();
var handler = new EventHandler();
//{origin:[0.5,0.5]}
var physicsEngine = new PhysicsEngine();
$('#game').on('click', function(event) {
console.log('x '+event.clientX);
console.log('y '+event.clientY)
createBall(event.clientX, event.clientY);
})
var leftWall = new Wall({
normal: [1, 0, 0],
distance: window.innerWidth / 2.0,
restitution: 0.5
});
var rightWall = new Wall({
normal: [-1, 0, 0],
distance: window.innerWidth / 2.0,
restitution: 0.5
});
var topWall = new Wall({
normal: [0, 1, 0],
distance: window.innerHeight / 2.0,
restitution: 0.5
});
console.log(window.innerHeight )
console.log(window.innerWidth )
var bottomWall = new Wall({
normal: [0, -1, 0],
distance: window.innerHeight / 2.0,
restitution: 0.5
});
leftWall = physicsEngine.attach(leftWall,[]);
rightWall = physicsEngine.attach(rightWall,[]);
topWall = physicsEngine.attach(topWall,[]);
bottomWall = physicsEngine.attach(bottomWall,[]);
var balls = []
function createBall(x, y) {
var ball = new Surface({
size: [100, 100],
properties: {
backgroundColor: 'red',
borderRadius: '100px'
}
})
ball.state = new StateModifier({
// transform: Transform.translate(x, y, 0)
});
ball.particle = new Circle({
radius: 50,
position : new Vector(x, y, 0)
});
physicsEngine.addBody(ball.particle);
ball.on("click", function() {
console.log('clicked ball')
ball.particle.setVelocity([1, 1, 0]);
});
context.add(ball.state).add(ball)
Engine.on('prerender', function() {
ball.state.setTransform(ball.particle.getTransform())
});
// balls.push(ball.particle);
//bottomWall = physicsEngine.attach(ball.particle,balls);
physicsEngine.attachTo(leftWall,ball.particle);
physicsEngine.attachTo(rightWall,ball.particle);
physicsEngine.attachTo(topWall,ball.particle);
physicsEngine.attachTo(bottomWall,ball.particle);
}
});
The physics collision and adding of particles is driven by the origin of the physics engine in famo.us, so you would want to set the origin of the physics engine to your render node.
As you can see from the code below or the following link:
jsFiddle Link
The placement of the walls are driven by the origin of the physics engine when they are attached. The particles you attach to the walls are bound by the origin of the physics engine also, so changing their origin is going to have an effect on their collision to the walls.
The example code does not attach a modifier to the circles, but adds them to the particles. I am not sure if you were trying to do something different, but hopefully this answers your question.
define('main', function (require, exports, module) {
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var RenderNode = require('famous/core/RenderNode');
var EventHandler = require('famous/core/EventHandler');
var View = require('famous/core/View');
var Transform = require('famous/core/Transform');
//var $ = require('jquery');
var StateModifier = require('famous/modifiers/StateModifier');
var Modifier = require('famous/core/Modifier');
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 context = Engine.createContext();
var node = new RenderNode();
var physicsOrigin = [0.5, 0.5];
var radius = 50;
context.add(new Modifier({
origin: physicsOrigin
})).add(node);
var handler = new EventHandler();
var physicsEngine = new PhysicsEngine();
node.add(physicsEngine);
console.log(window.innerHeight);
console.log(window.innerWidth);
var dimX = window.innerWidth;
var dimY = window.innerHeight;
Engine.on('click', function (event) {
console.log('x ' + event.clientX);
console.log('y ' + event.clientY)
var x = event.clientX - (dimX * physicsOrigin[0]);
var y = event.clientY - (dimY * physicsOrigin[1]);
createBall(x, y);
});
var leftWall = new Wall({
normal: [1, 0, 0],
distance: Math.round(dimX / 2.0),
restitution: 0.5
});
var rightWall = new Wall({
normal: [-1, 0, 0],
distance: Math.round(dimX / 2.0),
restitution: 0.5
});
var topWall = new Wall({
normal: [0, 1, 0],
distance: Math.round(dimY / 2.0),
restitution: 0.5
});
var bottomWall = new Wall({
normal: [0, -1, 0],
distance: Math.round(dimY / 2.0),
restitution: 0.5
});
var balls = [];
physicsEngine.attach([leftWall, rightWall, topWall, bottomWall], balls);
function createBall(x, y) {
var ball = new Surface({
size: [radius * 2, radius * 2],
properties: {
backgroundColor: 'blue',
borderRadius: (radius * 2) + 'px'
}
})
ball.particle = new Circle({
radius: radius,
position: [x, y, 0]
});
physicsEngine.addBody(ball.particle);
node.add(ball.particle).add(ball)
ball.on("click", function () {
console.log('clicked ball')
ball.setOptions({properties: {backgroundColor: 'red'}});
ball.particle.setVelocity([1, 1, 0]);
});
balls.push(ball.particle);
}
});
Related
My goal is to mimic Z-translation in perspective mode by using multiple modifiers. I can not use just z-translation of a surface because a text of translated surface became blurred (at least at Chrome but also on another browsers). The idea of using concurrent modifiers is explained in my blog: https://ozinchenko.wordpress.com/2015/02/04/how-to-avoid-blurring-of-the-text-in-famo-us-during-transition-in-z-direction/
As a result I want to have smooth translation in Z direction surface with a smooth text scaling.
the codepen code is here:
http://codepen.io/Qvatra/pen/yyPMyK?editors=001
var Engine = famous.core.Engine;
var Surface = famous.core.Surface;
var ImageSurface = famous.surfaces.ImageSurface;
var ContainerSurface = famous.surfaces.ContainerSurface;
var View = famous.core.View;
var Entity = famous.core.Entity;
var Modifier = famous.core.Modifier;
var StateModifier = famous.modifiers.StateModifier;
var Transform = famous.core.Transform;
var Transitionable = famous.transitions.Transitionable;
var TransitionableTransform = famous.transitions.TransitionableTransform;
var Easing = famous.transitions.Easing;
var Scrollview = famous.views.Scrollview;
var perspective = 1000;
var fontValue = 100; //initially font-size is 100%
var surfSize = [100,100];
var mainContext = Engine.createContext();
mainContext.setPerspective(perspective);
var transitionable = new Transitionable(0);
var mySurface = new Surface({
size: surfSize,
properties: {
backgroundColor: 'red',
textAlign: 'center',
color: 'white',
fontSize: fontValue + '%',
lineHeight: surfSize[1] + 'px'
},
content: 'Click Me'
});
var transitionModifier = new StateModifier({
origin: [.5, .5],
align: [.5, .5],
transform: Transform.translate(0,0,0.01)
});
mainContext.add(transitionModifier).add(mySurface);
function translateZ(dist, transition) {
transitionable.reset(0);
transitionable.set(dist, transition);
function prerender() {
var currentDist = transitionable.get();
//perspective formula: dist = perspective(1 - 1/scaleFactor)
var currentScale = 1 / (1 - currentDist / perspective);
var currentSize = [surfSize[0] * currentScale, surfSize[1] * currentScale];
var currentFontValue = fontValue * currentScale;
//f.e: bring closer => make projection scaleFactor times bigger
var transitionTransform = Transform.translate(0,0, currentDist);
//scaling back to avoid text blurring
var scaleTransform = Transform.scale(1/currentScale, 1/currentScale, 1);
transitionModifier.setTransform(Transform.multiply(transitionTransform, scaleTransform));
mySurface.setSize(currentSize); //resize to get correct projection size
mySurface.setOptions({
properties:{
fontSize: currentFontValue + '%', //resizing font;
lineHeight: currentSize[1] + 'px' //align text;
}
})
if (currentDist === dist) {
Engine.removeListener('prerender', prerender);
}
}
Engine.on('prerender', prerender);
}
Engine.on('click', function() {
translateZ(750, {curve: 'easeOutBounce', duration: 2000});
});
Why do I have the shaking of the image? How to avoid that?
The StateModifier is changing the size of your surface while you are setting the size of the surface. Because you are handling the size of the surface, there is no need to change (set) the StateModifier to scale. I am not sure your method will hold up in all cases, but this answers your question.
Here is a new translateZ function:
function translateZ(dist, transition) {
transitionable.reset(0);
transitionable.set(dist, transition);
function prerender() {
var currentDist = transitionable.get();
//perspective formula: dist = perspective(1 - 1/scaleFactor)
var currentScale = 1 / (1 - currentDist / perspective);
var currentSize = [surfSize[0] * currentScale, surfSize[1] * currentScale];
var currentFontValue = fontValue * currentScale;
mySurface.setSize(currentSize); //resize to get correct projection size
mySurface.setOptions({
properties:{
fontSize: currentFontValue + '%', //resizing font;
lineHeight: currentSize[1] + 'px' //align text;
}
})
if (currentDist === dist) {
Engine.removeListener('prerender', prerender);
}
console.log('trans')
}
Engine.on('prerender', prerender);
}
I'm new to Famo.us and I am trying to expand on the Timbre sample app by adding a scrollview to the PageView where the image would be (in the _createBody function). In other words, I'm trying to add a feed similar to Facebook or Tango, etc. I found two pieces of code online that's been working with (links below). I get no errors on the console log, yet the scrollview won't display, so I'm not sure what I am missing. Your guidance is much appreciated (would also love to know if there is a better way). Finally, this is my first post ever on StackOverflow, so please let me know if I can expose my issue in a better fashion.
Links I have been using for guidance:
StackOverflowFamo.us swipe on scrollview
JSFiddle
/*** AppView.js ***/
define(function(require, exports, module) {
var View = require('famous/core/View');
var Surface = require('famous/core/Surface');
var Modifier = require('famous/core/Modifier');
var Transform = require('famous/core/Transform');
var StateModifier = require('famous/modifiers/StateModifier');
var Easing = require('famous/transitions/Easing');
var Transitionable = require('famous/transitions/Transitionable');
var GenericSync = require('famous/inputs/GenericSync');
var MouseSync = require('famous/inputs/MouseSync');
var TouchSync = require('famous/inputs/TouchSync');
GenericSync.register({'mouse': MouseSync, 'touch': TouchSync});
var PageView = require('views/PageView');
var MenuView = require('views/MenuView');
var StripData = require('data/StripData');
function AppView() {
View.apply(this, arguments);
this.menuToggle = false;
this.pageViewPos = new Transitionable(0);
_createPageView.call(this);
_createMenuView.call(this);
_setListeners.call(this);
_handleSwipe.call(this);
}
AppView.prototype = Object.create(View.prototype);
AppView.prototype.constructor = AppView;
AppView.DEFAULT_OPTIONS = {
openPosition: 276,
transition: {
duration: 300,
curve: 'easeOut'
},
posThreshold: 138,
velThreshold: 0.75
};
function _createPageView() {
this.pageView = new PageView();
this.pageModifier = new Modifier({
transform: function() {
return Transform.translate(this.pageViewPos.get(), 0, 0);
}.bind(this)
});
this._add(this.pageModifier).add(this.pageView);
}
function _createMenuView() {
this.menuView = new MenuView({ stripData: StripData });
var menuModifier = new StateModifier({
transform: Transform.behind
});
this.add(menuModifier).add(this.menuView);
}
function _setListeners() {
this.pageView.on('menuToggle', this.toggleMenu.bind(this));
}
function _handleSwipe() {
var sync = new GenericSync(
['mouse', 'touch'],
{direction : GenericSync.DIRECTION_X}
);
this.pageView.pipe(sync);
sync.on('update', function(data) {
var currentPosition = this.pageViewPos.get();
if(currentPosition === 0 && data.velocity > 0) {
this.menuView.animateStrips();
}
this.pageViewPos.set(Math.max(0, currentPosition + data.delta));
}.bind(this));
sync.on('end', (function(data) {
var velocity = data.velocity;
var position = this.pageViewPos.get();
if(this.pageViewPos.get() > this.options.posThreshold) {
if(velocity < -this.options.velThreshold) {
this.slideLeft();
} else {
this.slideRight();
}
} else {
if(velocity > this.options.velThreshold) {
this.slideRight();
} else {
this.slideLeft();
}
}
}).bind(this));
}
AppView.prototype.toggleMenu = function() {
if(this.menuToggle) {
this.slideLeft();
} else {
this.slideRight();
this.menuView.animateStrips();
}
};
AppView.prototype.slideLeft = function() {
this.pageViewPos.set(0, this.options.transition, function() {
this.menuToggle = false;
}.bind(this));
};
AppView.prototype.slideRight = function() {
this.pageViewPos.set(this.options.openPosition, this.options.transition, function() {
this.menuToggle = true;
}.bind(this));
};
module.exports = AppView;
});
/*** PageView.js ***/
define(function(require, exports, module) {
var View = require('famous/core/View');
var Surface = require('famous/core/Surface');
var Transform = require('famous/core/Transform');
var StateModifier = require('famous/modifiers/StateModifier');
var HeaderFooter = require('famous/views/HeaderFooterLayout');
var ImageSurface = require('famous/surfaces/ImageSurface');
var Scrollview = require('famous/views/Scrollview');
function PageView() {
View.apply(this, arguments);
_createBacking.call(this);
_createLayout.call(this);
_createHeader.call(this);
_createBody.call(this);
_setListeners.call(this);
}
PageView.prototype = Object.create(View.prototype);
PageView.prototype.constructor = PageView;
PageView.DEFAULT_OPTIONS = {
headerSize: 44
};
function _createBacking() {
var backing = new Surface({
properties: {
backgroundColor: 'black',
boxShadow: '0 0 20px rgba(0,0,0,0.5)'
}
});
this.add(backing);
}
function _createLayout() {
this.layout = new HeaderFooter({
headerSize: this.options.headerSize
});
var layoutModifier = new StateModifier({
transform: Transform.translate(0, 0, 0.1)
});
this.add(layoutModifier).add(this.layout);
}
function _createHeader() {
var backgroundSurface = new Surface({
properties: {
backgroundColor: 'black'
}
});
this.hamburgerSurface = new ImageSurface({
size: [44, 44],
content : 'img/hamburger.png'
});
var searchSurface = new ImageSurface({
size: [232, 44],
content : 'img/search.png'
});
var iconSurface = new ImageSurface({
size: [44, 44],
content : 'img/icon.png'
});
var backgroundModifier = new StateModifier({
transform : Transform.behind
});
var hamburgerModifier = new StateModifier({
origin: [0, 0.5],
align : [0, 0.5]
});
var searchModifier = new StateModifier({
origin: [0.5, 0.5],
align : [0.5, 0.5]
});
var iconModifier = new StateModifier({
origin: [1, 0.5],
align : [1, 0.5]
});
this.layout.header.add(backgroundModifier).add(backgroundSurface);
this.layout.header.add(hamburgerModifier).add(this.hamburgerSurface);
this.layout.header.add(searchModifier).add(searchSurface);
this.layout.header.add(iconModifier).add(iconSurface);
}
function _createBody() {
var surfaces = [];
this.scrollview = new Scrollview();
var temp;
for (var i = 0; i < 30; i++) {
temp = new Surface({
size: [undefined, 80],
content: 'Surface: ' + (i + 1),
properties: {
textAlign: 'left',
lineHeight: '80px',
borderTop: '1px solid #000',
borderBottom: '1px solid #fff',
backgroundColor: '#ffff00',
fontFamily: 'Arial',
backfaceVisibility: 'visible',
paddingLeft: '10px'
}
});
temp.pipe(this.scrollview);
surfaces.push(temp);
}
this.scrollview.sequenceFrom(surfaces);
this.bodyContent = new Surface({
size: [undefined, undefined],
properties: {
backgroundColor: '#f4f4f4'
}
});
this.layout.content.add(this.bodyContent);
}
function _setListeners() {
this.hamburgerSurface.on('click', function() {
this._eventOutput.emit('menuToggle');
}.bind(this));
//this.bodyContent.pipe(this._eventOutput);
this.scrollview.pipe(this._eventOutput);
}
module.exports = PageView;
});
You need to add this.scrollview to your layout.content element on the page. Put this in place of this.bodyContent. layout.content is the node for the content of the page.
//this.layout.content.add(this.bodyContent);
this.layout.content.add(this.scrollview);
I have a simple problem I am stuck on. I want to implement a ScaleSync on an ImageSurface. Everytime I end the scale action the data value is reset to 1 and if I perform another scale the ImageSurfaces starts transforming from 1.
define(function(require, exports, module) {
var Engine = require('famous/core/Engine');
var Modifier = require('famous/core/Modifier');
var Transform = require('famous/core/Transform');
var ImageSurface = require('famous/surfaces/ImageSurface');
var Surface = require('famous/core/Surface');
var ScaleSync = require("famous/inputs/ScaleSync");
var mainContext = Engine.createContext();
var start = 1;
var update = 0;
var end = 0;
var growShrink = "";
var objectScale = 1;
var scaleSync = new ScaleSync();
var logo = new ImageSurface({
size: [200, 200],
content: '/content/images/famous_logo.png',
classes: ['backfaceVisibility']
});
var centerModifier = new Modifier({
align: [0.5, 0.5],
origin: [0.5, 0.5]
});
logo.pipe(scaleSync);
scaleSync.on("update", function(data) {
objectScale = data.scale
centerModifier.setTransform(
Transform.scale( objectScale, objectScale, 1)
);
});
scaleSync.on("start", function(){
start ++;
});
scaleSync.on("end", function() {
end++;
});
mainContext.add(centerModifier).add(logo);
});
I tried achieving this using an Accumulator, but I think that's for different purposes. I think I have to somehow alter the data in the on.update callback, but have no clue how to do that.
Your help is much appreciated!
I found the solution. Hope this helps anybody with similar issues. The modifier should have a scale variable that is referenced as follows:
var scale = 1;
var centerModifier = new Modifier({
align: [0.5, 0.5],
origin: [0.5, 0.5],
transform: function(){
return Transform.scale(scale, scale, 1);
}
});
And the state of the variable is changed on event update (I divide data.delta by 10 to normalize the value):
scaleSync.on("update", function(data) {
scale += data.delta / 10;
});
based on how to add walls to a famo.us physics simulation? there is a ball that will bounce off of walls
How do you go about adding a repulsion to the particle so that it will never hit the walls?
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');
var Repulsion = require('famous/physics/forces/Repulsion');
module.exports = function () {
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});
var replusion = new Repulsion();
ball.particle.applyForce(replusion);
physicsEngine.addBody(ball.particle);
ball.on("click",function(){
ball.particle.setVelocity([1,1,0]);
});
context.add(ball.state).add(ball)
var leftWall = new Wall({normal : [1,0,0], distance : contextSize[0]/2.0, restitution : 0.5});
var rightWall = new Wall({normal : [-1,0,0], distance : contextSize[0]/2.0, restitution : 0.5});
var topWall = new Wall({normal : [0,1,0], distance : contextSize[1]/2.0, restitution : 0.5});
var bottomWall = new Wall({normal : [0,-1,0], distance : contextSize[1]/2.0, restitution : 0.5});
physicsEngine.attach( leftWall, [ball.particle]);
physicsEngine.attach( rightWall, [ball.particle]);
physicsEngine.attach( topWall, [ball.particle]);
physicsEngine.attach( bottomWall,[ball.particle]);
Engine.on('prerender', function(){
ball.state.setTransform(ball.particle.getTransform())
});
};
My first thought is to add repulsion to the ball's particle, but that doesn't seem to be working.
Has anyone done this or is there good documentation for this type of behavior?
I've played around and I think I have a somewhat better idea now.
This code with two balls (particles) in the same physics engine will repel ( one will chase the other around )
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.4,0.3]});
ball.particle = new Circle({radius:100});
var ball2 = new Surface ({
size: [200,200],
properties: {
backgroundColor: 'green',
borderRadius: '100px'
}
})
ball2.state = new StateModifier({origin:[0.3,0.3]});
ball2.particle = new Circle({radius:100});
var leftWall = new Wall({normal : [1,0,0], distance : contextSize[0]/4.0, restitution : 0.3});
var rightWall = new Wall({normal : [-1,0,0], distance : contextSize[0]/4.0, restitution : 0.3});
var topWall = new Wall({normal : [0,1,0], distance : contextSize[1]/4.0, restitution : 0.3});
var bottomWall = new Wall({normal : [0,-1,0], distance : contextSize[1]/4.0, restitution : 0.3});
physicsEngine.attach( leftWall, [ball.particle, ball2.particle]);
physicsEngine.attach( rightWall, [ball.particle, ball2.particle]);
physicsEngine.attach( topWall, [ball.particle, ball2.particle]);
physicsEngine.attach( bottomWall,[ball.particle, ball2.particle]);
physicsEngine.addBody(ball.particle);
physicsEngine.addBody(ball2.particle);
The repulsion has to be added to the PE with a target and source, both of which must already exist in the engine.
var replusion = new Repulsion({strength: 90});
physicsEngine.attach(replusion, [ball2.particle], ball.particle);
ball.on("click",function(){
ball.particle.setVelocity([1,0,0]);
});
ball2.on("click",function(){
ball2.particle.setVelocity([1,1,0]);
});
context.add(ball.state).add(ball)
context.add(ball2.state).add(ball2)
Engine.on('prerender', function(){
ball.state.setTransform(ball.particle.getTransform())
ball2.state.setTransform(ball2.particle.getTransform())
});
I'm trying to make some examples using Famo.us Physics engine.
I had tried an example of spring. I thought rotational spring could be similar with it,so I'm trying to do but it's not that easy for me.
Here is an example using Rotational Spring, but it doesn't work
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var ImageSurface = require("famous/surfaces/ImageSurface");
var Modifier = require("famous/core/Modifier");
var Transform = require("famous/core/Transform");
var Transitionable = require("famous/transitions/Transitionable");
var PhysicsEngine = require("famous/physics/PhysicsEngine");
var Spring = require("famous/physics/forces/Spring");
var RotationalSpring = require("famous/physics/forces/RotationalSpring");
var Particle = require("famous/physics/bodies/Particle");
var Body = require("famous/physics/bodies/Body");
var Constraint = require("famous/physics/constraints/Constraint");
var MouseSync = require("famous/inputs/MouseSync");
var mainContext = Engine.createContext();
var PE = new PhysicsEngine();
content.particle = new Particle({
mass: 1,
position: [0, 0, 0],
velocity: [0, 0, 0],
axis : 0x01 //0x01 : Particle.AXES.y
});
// Create a spring that will act on the particle
var rotationalSpring = new RotationalSpring({
anchor: [0, 0, 0],
period: 400, // <= Play with these values :-)
dampingRatio: 0.07, // <= if 0 then just once / else if 1 then infinite
length: 0
});
// Link the spring, particle and surface together
PE.attach(rotationalSpring, content.particle);
PE.addBody(content.particle);
var translateMod = new Modifier({ origin: [.5,.5] });
//Get Mouse input
var position = [0, 0];
var mouseSync = new MouseSync(function() {
return position;
});
Engine.pipe(mouseSync);
// Get start when there is a Mouse input
mouseSync.on("start", function() {
FaceSequence.particle.applyForce(new Vector(0, 1.0, 0));
});
translateMod.transformFrom(function() {
var transM = FaceSequence.particle.getTransform();
return transM;
});
How can I make it works?? :)
After poking at the above code a bit, I realized that the function calls of RotationalSpring matched up with Body, not Particle. After a quick rewrite I got a number of odd results which made me think of rotational balances. They are extremely sensitive and extremely delicate, so I guessed that this might work the same way. After dropping the torque a bit I started to get some almost reasonable results. I'll leave the fine tuning, but here is some almost reasonable rotational spring code. I have tilted a surface of angle a little bit to make the rotation a little more clear. You can also see it run live at codefamo.us
Here is the code...
/* globals define */
define(function(require, exports, module) {
var Engine = require("famous/core/Engine");
var Surface = require("famous/core/Surface");
var ImageSurface = require("famous/surfaces/ImageSurface");
var Modifier = require("famous/core/Modifier");
var StateModifier = require("famous/modifiers/StateModifier");
var Transform = require("famous/core/Transform");
var Transitionable = require("famous/transitions/Transitionable");
var PhysicsEngine = require("famous/physics/PhysicsEngine");
var Spring = require("famous/physics/forces/Spring");
var RotationalSpring = require("famous/physics/forces/RotationalSpring");
var Particle = require("famous/physics/bodies/Particle");
var Body = require("famous/physics/bodies/Body");
var Constraint = require("famous/physics/constraints/Constraint");
var Vector = require('famous/math/Vector');
var MouseSync = require("famous/inputs/MouseSync");
var mainContext = Engine.createContext();
var PE = new PhysicsEngine();
var surf = new Surface({
size: [200,200],
properties: {
backgroundColor:'red',
'-webkit-backface-visibility':'visible'
}
});
var tilt = new StateModifier({
transform: Transform.rotateX(Math.PI/8)
});
// Create a spring that will act on the particle
var rotationalSpring = new RotationalSpring({
anchor: new Vector(0, 1, 0),
period: 3000, // <= Play with these values :-)
dampingRatio: 0.1// <= if 0 then just once / else if 1 then infinite
});
var body = new Body({
mass: 10
});
// Link the spring, particle and surface together
PE.addBody(body);
PE.attach(rotationalSpring, body);
var translateMod = new Modifier({
origin: [.5,.5]
});
translateMod.transformFrom(function() {
return body.getTransform();
});
Engine.on('click', function() {
body.applyTorque(new Vector(0,.001,0));
});
mainContext.add(translateMod).add(tilt).add(surf);
});