Choppy movement with certain key bindings

I’ve made some custom key binding to move the camera up and down like so:

inputManager.addMapping("up", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_LSHIFT));
inputManager.addListener(this, "up");
inputManager.addListener(this, "down");

and in the onAction() method:

if (name == "up")
  goingUp = isPressed;
if (name == "down")
  goingDown = isPressed;

and in the simpleUpdate() method:

Vector3f loc = cam.getLocation();
Vector3f up = cam.getUp().mult(walkSpeed * tpf);
if (goingUp)
  loc.addLocal(up);
if (goingDown)
  loc.addLocal(up.mult(-1));

This produces some very choppy movement. However, if I map keys that have been mapped by default ( eg, ‘q’ and ‘z’ ) then the movement is very smooth. Why is this so? Did I miss something? (also, I’m new to JMonkey)

Thanks,

Should all be the same… maybe something else is wrong.

I just striped the code to be as basic as possible and it still produces the choppy movements.

See the full code there: http://pastebin.com/UKXJVsNU

I’m using Ubuntu and Eclipse.

Add a cam.setLocation() to the end of the method where you modify location.

In general, you can’t just grab vectors out of things and change them without setting them back again. It doesn’t update things properly.

2 Likes

Also, while this:
if( name == “up” )
…works ok when “up” is all in the same class. It will potentially fail if that’s ever not the case as == means they have to be the exact same reference. It’s dangerous to compare strings this way and the SDK is probably telling you that with a warning.

It’s not your issue but it’s a bad practice that will bite you later.

This is correct, just to provide some more information, if you have a look at the camera class you will see that when setLocation() is called it will call the onFrameChange() method which in turn actually updates the view projection. Without that bit even though the location vector of the camera is changing, the view isn’t being updated.

1 Like

btw,

loc.addLocal(up.mult(-1));

can be written as (which does not create unnecessary vector)

loc.subtractLocal(up);
1 Like

Thanks all. It works great now :smile: