From 40941ca27873b939d4b0814bf83675e8b6a184fb Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 24 Mar 2015 01:59:33 -0400
Subject: [PATCH] basic animation loop working

---
 js/cam/Assembler.js     |  8 +++-----
 js/cam/Machine.js       | 21 ++++++++++++++-------
 js/models/ThreeModel.js | 13 +++++++++++--
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/js/cam/Assembler.js b/js/cam/Assembler.js
index 1e8b8b1a..c5826617 100644
--- a/js/cam/Assembler.js
+++ b/js/cam/Assembler.js
@@ -77,9 +77,7 @@ Assembler = Backbone.Model.extend({
         this.listenTo(options.lattice, "change:cellType change:connectionType", this._updateCellType);
         this.listenTo(options.appState, "change:cellMode", this._updateCellMode);
 
-        this.listenTo(this, "change:machineName", this._changeMachine);
-
-        this._initOriginAndStock(options.lattice);
+        this._initOriginAndStock();
     },
 
     selectMachine: function(machineName){
@@ -142,7 +140,7 @@ Assembler = Backbone.Model.extend({
         dmaGlobals.three.render();
     },
 
-    _initOriginAndStock: function(lattice){//todo this is ugly
+    _initOriginAndStock: function(){//todo this is ugly
         var origin = new THREE.Mesh(new THREE.SphereGeometry(1),
             new THREE.MeshBasicMaterial({color:0xff0000}));
         dmaGlobals.three.sceneAdd(origin);
@@ -154,7 +152,7 @@ Assembler = Backbone.Model.extend({
         dmaGlobals.three.sceneAdd(stock);
         this.set("stock", stock);
         this._moveStock();
-        this._setCAMScale(lattice.get("scale"));
+        this._setCAMScale();
         this._setCAMVisibility();
     },
 
diff --git a/js/cam/Machine.js b/js/cam/Machine.js
index 657819ea..9d2bc95a 100644
--- a/js/cam/Machine.js
+++ b/js/cam/Machine.js
@@ -63,21 +63,28 @@ Machine.prototype.pause = function(){
 };
 
 Machine.prototype._animateMesh = function(mesh, axis, speed, target, callback){
+    var increment = (target-mesh.position[axis])/20;
+    if (increment == 0) {
+        if (callback) callback();
+        return;
+    }
+    var direction = 1;
+    if (increment<0) direction = -1;
+    increment = Math.max(Math.abs(increment), 0.0001)*direction;
     dmaGlobals.three.startAnimationLoop();
-    var increment = (target-mesh.position[axis])/10;
-    this._incrementalMove(mesh, axis, increment, mesh.position[axis]+increment*10, callback);
+    this._incrementalMove(mesh, axis, increment, target, direction, callback);
 };
 
-Machine.prototype._incrementalMove = function(mesh, axis, increment, target, callback){
+Machine.prototype._incrementalMove = function(mesh, axis, increment, target, direction, callback){
     var self = this;
     setTimeout(function(){
-        if (mesh.position[axis] == target) {
+        if ((target-mesh.position[axis])*direction <= 0) {
             if (callback) callback();
             return;
         }
-        mesh.position[axis] += increment;
-        console.log(mesh.position[axis]);
-        self._incrementalMove(mesh, axis, increment, target, callback)
+        if (Math.abs(target-mesh.position[axis]) < Math.abs(increment)) mesh.position[axis] = target;//don't overshoot
+        else mesh.position[axis] += increment;
+        self._incrementalMove(mesh, axis, increment, target, direction, callback)
     },100);
 };
 
diff --git a/js/models/ThreeModel.js b/js/models/ThreeModel.js
index 96ebdeae..55eae601 100644
--- a/js/models/ThreeModel.js
+++ b/js/models/ThreeModel.js
@@ -112,13 +112,22 @@ function ThreeModel(){
     }
 
     function _loop(){
-        if (stopAnimationFlag) return console.log("animation stopped");
-        render();
+        if (stopAnimationFlag) {
+            animationLoopRunning = false;
+            console.log("animation stopped");
+            return;
+        }
+//        console.log("loop");
+        _render();
         requestAnimationFrame(_loop);
     }
 
     function render(){
         if (animationLoopRunning) return;
+        _render();
+    }
+
+    function _render(){
         renderer.render(scene, camera);
     }
 
-- 
GitLab