Moving a Box - different speeds for up/down!?

Hello there!



This is my first post on this wonderfully active forum. I'm very happy to be here, get help and hopefully help others as soon as I get a better grasp on things.



I've been programming in Java for about 1 year and started out with JMonkey a few day ago. The tutorials are superb and I'm learning quickly. I just have one problem with my code and I can't seem to figure out what I'm doing wrong.



So, I have a cube and want to be able to move it up and down (along the Y axis) with my "z" and "h"-key. I'm using a German keyboard (I'm Swiss btw), so the "z" key is "y" on US keyboards, I think. Just so you don't get confused.



Anyway, the cube moves up a LOT faster than down when pressing the respective keys…although the code doesn't say it should.

could anyone explain why this is happening?





here's the part where i set up the movement in my simpleUpdate method:


   protected void simpleUpdate(){
   
      boolean updateCube = false;
      
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("UP", true)){
         moveY += .01;
         updateCube = true;
   
         if (updateCube){
            box.setLocalTranslation(new Vector3f(0, moveY, 0));
         }
      }
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("DOWN", true)){
         moveY -= .01;
         updateCube = true;
   
         if (updateCube){
            box.setLocalTranslation(new Vector3f(0, moveY, 0));
         }
      }
   }



and here's the whole code, should my mistake lie elsewhere:

package com.jmedemos;

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



public class JME_KeyInput extends SimpleGame {

   float moveY = 0f;
   Box box = new Box("MyBox", new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));


   @Override
   protected void simpleInitGame() {

      box.setLocalTranslation(new Vector3f(0, 0, 0));
      box.setModelBound(new BoundingBox());
      box.updateModelBound();
      rootNode.attachChild(box);
      
      KeyBindingManager.getKeyBindingManager().set("UP", KeyInput.KEY_Z);
      KeyBindingManager.getKeyBindingManager().set("DOWN", KeyInput.KEY_H);
      
      }
   
   protected void simpleUpdate(){
   
      boolean updateCube = false;
      
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("UP", true)){
         moveY += .01;
         updateCube = true;
   
         if (updateCube){
            box.setLocalTranslation(new Vector3f(0, moveY, 0));
         }
      }
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("DOWN", true)){
         moveY -= .01;
         updateCube = true;
   
         if (updateCube){
            box.setLocalTranslation(new Vector3f(0, moveY, 0));
         }
      }
   }

   public static void main(String[] args) {
      JME_KeyInput game = new JME_KeyInput();
      game.setConfigShowMode(ConfigShowMode.AlwaysShow);
      game.start();
   }
}




I hope you can point out what I'm doing wrong.

Thank you very much!

Jonas

I woudl say that one of the calcs needs mroe tiem wheyever. Try activating vsync (to have a stable framerate) and see if it still happens

thanks man. i tried to do it but seem to be too dumb to activate that…but i'll try some more…maybe implementing (the GameState interface, i think it was!?) isn't the way to go?

anyway, i'll spend some more time with you idea as soon is i'm back at my programming pc. but did you copy/paste and compile my code on your machine? do you have the same problem?



Could someone please try out my code and see if the same thing happens?



Thanks though, hopefully vsync helps (even though the "one calc needs more time than the other" explenation isn't all that satisfying;)

SimpleGame has already many keybindings.



'Z' is already used in SimpleGame (look in KeyboardLookHandler), it elevates the camera down, that why you think your cube moves up faster :wink:



use this to remove the mouse and keyboard look handler in SimpleGame:


   protected void simpleInitGame() {
      input.removeAllFromAttachedHandlers();




also for movement independent of the frames per secon, you should multiply the movement amount by time per frame:

moveY += 5 * tpf;



(tpf is already computed by BaseSimpleGame)

oh, how stupid of me! i actually looked that up because i was using "t" for the up-movement first and then it always switched into wireframe-mode…so i looked it up…and with the "z" i saw it working and never figured of checking if the key was in use already.



perfect, thanks a lot! and also for the tpf-tip. didn't know that!

so…another really stupid question, I’m guessing - so i don’t wanna open a new thread and spam this place up. I’ve been looking for an answer for 3(!!) hours now…but it seems this is so trivial that nobody ever mentions it anywhere!?

All I need is to get the X, Y and Z coordinates of my box. How do I do that? Isn’t there a command like float coordX = box.getLocalXCoords;? Do I have to make the calculations myself? I’m using Physics 2, btw., only gravity is affecting my cube as of now. how would i calculate that!?



and another btw.: is there any interest in making a better Physics wiki? I’d be more than willing to post the things I learn into the wiki, since it is pretty horrible - empty!? - at the moment, imho. Either way, as soon as I get a grasp on things I want to contribute this info to other newcommers having a hard time with some of the things (like material- and lightstates).



thanks for any help and try not to laugh and shake your head at me :’(

You can get the location of any Spatial (Node, Geometry etc.) in form of a Vector3f like this:


Vector3f myVector=spatial.getWorldTranslation();



The Vector3f contains all three parts of the tuple. You can access them like myVector.x myVector.y myVector.z

For physics, you might want to have a look at jbullet-jme instead of jmePhysics2, theres lots of info in the wiki ;) See my signature for a link to the post which contains links to the download, wiki etc.

Cheers,
Normen

Hey Normen



Thanks for your answer, couldn't find the .getWorldTranslation - method described anywhere. This is absolutely essential and thus very good to know;) thanks!



And I just downloaded and set up jbullet and will spend some time getting to know it today. The tests look great, let's see if I can work this better than jmePhysics2…your documentation looks far superior, as far as i can tell from the little time I've spent with it just now.



Thanks a bunch, lovely to see a forum with people that care, for a change!:wink:



Cheers!