Problem with key inputs!

I have been working through the tutorials on the Learning jME page and I am trying to get the hang of keyInputs so I tried making my own example, here is my code:


/**
 * @author Josh Branchaud
 * @version 12/2/2008
 */

// imported classes
import com.jme.app.SimpleGame;
import com.jme.scene.shape.Box;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;

public class moveableBox1 extends SimpleGame
{
    // make a variable for the KeyBindingManager
    KeyBindingManager KBM = KeyBindingManager.getKeyBindingManager();
    Box box1, box2;

    protected void simpleInitGame()
    {
        // create boxes, box1 to be moved, box2 to be a point of reference
        box1 = new Box("moverBox", new Vector3f(0,0,0), new Vector3f(5,5,5));
        box2 = new Box("staticBox", new Vector3f(0,0,0), new Vector3f(1,1,1));

        // give bounding to both of the boxes
        box1.setModelBound(new BoundingBox());
        box1.updateModelBound();
        box2.setModelBound(new BoundingBox());
        box2.updateModelBound();

        // attach the boxes to the rootNode
        rootNode.attachChild(box1);
        rootNode.attachChild(box2);

        // add some keybindings
        KBM.set("moveUp", KeyInput.KEY_T);
        KBM.set("moveDown", KeyInput.KEY_G);
        KBM.set("moveLeft", KeyInput.KEY_F);
        KBM.set("moveRight", KeyInput.KEY_H);
    }

    protected void simpleUpdate()
    {
        if(KBM.isValidCommand("moveUp", false))
        {
            Vector3f centerVector = box1.getCenter();
            box1.setLocalTranslation(centerVector.add(new Vector3f(0,1,0)));
        }

        if(KBM.isValidCommand("moveDown", false))
        {
            Vector3f centerVector = box1.getCenter();
            box1.setLocalTranslation(centerVector.add(new Vector3f(0,-1,0)));
        }

        if(KBM.isValidCommand("moveLeft", false))
        {
            Vector3f centerVector = box1.getCenter();
            box1.setLocalTranslation(centerVector.add(new Vector3f(-1,0,0)));
        }

        if(KBM.isValidCommand("moveRight", false))
        {
            Vector3f centerVector = box1.getCenter();
            box1.setLocalTranslation(centerVector.add(new Vector3f(1,0,0)));
        }
    }

    public static void main(String[] args)
    {
        moveableBox1 thisWorld = new moveableBox1();
        thisWorld.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
        thisWorld.start();
    }
}



When I run the above code, it appears fine and seems to run fine except I can't seem to move more than 1 unit in any direction... I was hoping to achieve the effect of moving as much as you wanted in any direction depending on the given key input.

T is up, G is Down, F is left, and H is right. I was hoping to ignore the fact that T has two functions if that is possible.

Any ideas on how to make this program do what it is suppose to?

I feel fairly certain that it is because I am calling for the center which doesn't change just because the object has been translated, any ideas on how I can gain the desired effect otherwise?

I thought if I was to change the simpleUpdate() method to something like this:


protected void simpleUpdate()
    {
        if(KBM.isValidCommand("moveUp", false))
        {
            Vector3f centerVector = box1.getCenter();
            centerVector.add(new Vector3f(0,1,0));
            box1.setLocalTranslation(centerVector);
            box1.setCenter(centerVector);
        }

        if(KBM.isValidCommand("moveDown", false))
        {
            Vector3f centerVector = box1.getCenter();
            centerVector.add(new Vector3f(0,-1,0));
            box1.setLocalTranslation(centerVector);
            box1.setCenter(centerVector);
        }

        if(KBM.isValidCommand("moveLeft", false))
        {
            Vector3f centerVector = box1.getCenter();
            centerVector.add(new Vector3f(-1,0,0));
            box1.setLocalTranslation(centerVector);
            box1.setCenter(centerVector);
        }

        if(KBM.isValidCommand("moveRight", false))
        {
            Vector3f centerVector = box1.getCenter();
            centerVector.add(new Vector3f(1,0,0));
            box1.setLocalTranslation(centerVector);
            box1.setCenter(centerVector);
        }



That it would work, but this didn't do it either... hmm?

try:


box1.getLocalTranslation().addLocal(new Vector3f(0,1,0));



What this does is grabs the vector object used to store the box's translation and then add to it the value of the new vector.
You do not need to change the center of the box (ever really, unless you have some very special case). The translation vector is all you need to change the location of it.

Thanks! That line of code did EXACTLY what I needed it to!