Commits

Jason S committed aebccef

added relative motion with shift key

  • Participants
  • Parent commits 1a5be8a

Comments (0)

Files changed (2)

File demos/gravity1/gravity1.html

     var k = 16;
     var dtsim = 1.0/k/fps;
     var keymap = {37: 'left', 38: 'up', 39: 'right', 40: 'down', 73: 'e+', 74:'E-', 75:'e-', 76:'E+'};
-    var keystate = {'up':false, 'down':false, 'left':false, 'right':false, 'e+':false, 'e-': false, 'E+':false, 'E-':false};
-    function modkey(keycode,isdown)
+    var keystate = {'up':false, 'down':false, 'left':false, 'right':false, 'e+':false, 'e-': false, 'E+':false, 'E-':false, 'shift':false, 'alt':false, 'ctrl':false};
+    function modkey(event,isdown)
     {
+        var keycode = event.keyCode;
+        keystate['shift'] = event.shiftKey;
+        keystate['ctrl'] = event.ctrlKey;
+        keystate['alt'] = event.altKey;
         if (keycode in keymap)
         {
             keystate[keymap[keycode]] = isdown;
         else
             return false;
     }    
-    document.addEventListener('keydown',function(event) { if (modkey(event.keyCode, true)) { event.preventDefault(true); return false;} });
-    document.addEventListener('keyup',function(event) { if (modkey(event.keyCode, false)) {  event.preventDefault(true); return false;} });
+    document.addEventListener('keydown',function(event) { 
+        if (modkey(event, true)) { event.preventDefault(true); return false;} });
+    document.addEventListener('keyup',function(event) { 
+        if (modkey(event, false)) {  event.preventDefault(true); return false;} });
     function settextvalue(id,value)
     {
         var s = isNaN(value) ? '---' : (''+value.toFixed(5)); 

File demos/gravity1/gravity1.js

     {
         var ax = 0;
         var ay = 0;
-        if (keymap.up)
-            ay += this.thrusterstrength;
-        if (keymap.down)
-            ay -= this.thrusterstrength;
-        if (keymap.right)
-            ax += this.thrusterstrength;
-        if (keymap.left)
-            ax -= this.thrusterstrength;
+        var K = this.thrusterstrength;
+        console.log(keymap);
+        if (keymap.shift)
+        {
+            var v = hypot(this.velpos[0],this.velpos[1]);
+            if (v > 1e-6)
+            {
+                var ux = this.velpos[0]/v;
+                var uy = this.velpos[1]/v;
+                if (keymap.up)
+                {
+                    ax += K*ux;
+                    ay += K*uy;
+                }
+                if (keymap.down)
+                {
+                    ay -= K*ux;
+                    ay -= K*uy;
+                }
+                if (keymap.right)
+                {
+                    ax += K*uy;
+                    ay -= K*ux;
+                }
+                if (keymap.left)
+                {
+                    ax -= K*uy;
+                    ay += K*ux;
+                }
+            }
+        }
+        else if (keymap.alt)
+        {
+        }
+        else
+        {
+            if (keymap.up)
+                ay += K;
+            if (keymap.down)
+                ay -= K;
+            if (keymap.right)
+                ax += K;
+            if (keymap.left)
+                ax -= K;
+        }
         if (keymap['E-'])
         {
             ax -= this.velpos[2];