Updated matrix, position3d, keyboard, toggle to work with nx

From an airplane. I also noticed that several objects have i index
variables in for loops that are acting globally, when they should be
defined locally, causing some bugs. I started to fix them but all
objects need a "var i" statement to create i as local.