Analog time pressed not giving crescent value

Hello all,



I’m getting strange values for the variable that holds how long a key is pressed.

My code

[java]import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.math.ColorRGBA;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;



/** Sample 5 - how to map keys and mousebuttons to actions /

public class AccelerateOnKeyHold extends SimpleApplication {



public static void main(String[] args) {

AccelerateOnKeyHold app = new AccelerateOnKeyHold();

app.start();

}

protected Geometry player;

Boolean isRunning=true;



@Override

public void simpleInitApp() {

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

player = new Geometry("Player", b);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

mat.setColor("Color", ColorRGBA.Blue);

player.setMaterial(mat);

rootNode.attachChild(player);

initKeys(); // load my custom keybinding

}



/
* Custom Keybinding: Map named actions to inputs. /

private void initKeys() {

// You can map one or several inputs to one named action

inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));

inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));

inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));

inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));

inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up", "Down"});



}

private AnalogListener analogListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

final float speedAccelerate = value
speed;

System.out.println("value:"+value);



if (isRunning) {

if (name.equals("Right")) {

Vector3f v = player.getLocalTranslation();

player.setLocalTranslation(v.x + speedAccelerate, v.y, v.z);

}

if (name.equals("Left")) {

Vector3f v = player.getLocalTranslation();

player.setLocalTranslation(v.x - speedAccelerate, v.y, v.z);

}

if (name.equals("Up")) {

Vector3f v = player.getLocalTranslation();

player.setLocalTranslation(v.x, v.y + speedAccelerate, v.z);

}

if (name.equals("Down")) {

Vector3f v = player.getLocalTranslation();

player.setLocalTranslation(v.x, v.y - speedAccelerate, v.z);

}

}

}

};

}[/java]



The some of the output:

value:0.0040248153
value:0.004018989
value:0.0013992949
value:0.003238394
value:0.004093857
value:0.003508292
value:0.003499759
value:0.0035413338
value:0.010297073
value:0.0035649568
value:0.013532254
value:0.0035566308
value:0.003542821
value:0.0033367758
value:0.003544376
value:0.00346571
value:0.0036646859


The output in a graph so you can see what I mean:
Photobucket

It never goes beyond 0.1 I think.
Is it from the OS? Is it my problem? More importantly, why is this happening?

The value is framerate-decoupled, so its pre-multiplied with tpf, use value/tpf to get the absolute value.

value/tpf is almost always 1.

If you want to see how long a button is held for you should total up the tpf. Or use an actionlistener and listen for key down and key up events and do the same thing but in simpleUpdate(float tpf);

Ok, but then what is this value for?

for analogue input like moving the mouse, joystick or pressure sensing devices. A key is more binary, it’s either on/off as opposed to how much a much a mouse has moved from its initial position. Perhaps it makes sense that value could store how long a key is pressed, but im not sure

Yeah I did just like @wezrule said, just go sum the tpf value, and you’ll get your result.

Yeah, I haven’t been able to try since I’m having a little mix up between camera and flyByCamera.

I totally removed flyByCamera from my game. I’m using a custom one.

Value gives you how far you moved the mouse, for example. I used value to create a small engine with which I can choose my own cursor.

1 Like

I’m totally confused by cameras and controls right now…



What I wanted was a first person camera, in a world with collisions and to have a costume control class to control the movement.

I think this is the standard first person shooter setup.



Now I don’t know which camera to use (default camera, flyby camera or costume one), don’t know how to attach a costume control to the shape used for collisions or any other spacial if it’s not a model…



sight I have to dig my nose in the internet

I was rereading Hello Input System and in Analog, Pressed, or Released? it says

JME gives you access to a gradual value how long the key has been pressed.




Is there some other parameter?

Well, the default is the flyby camera, and I don’t see how you can use it without modifying it and expect to work as a character. You’ll need to move only in X and Z axis, right? You could a lot of things here that came to my mind:


  • Edit flyby camera so you don’t move in the Y axis
  • Or you could attach a ChaseCamera to an object, and move that object. Just change the ChaseCamera maximum values so you can freely rotate it. Hide the spatial where you attached the chase camera and zoom in to the max.

You are right, that section of the manual does seem misleading…

I’ll change it to

JME gives you access to a gradual value how much the input has changed (Mouse movement for example).




What do you think?

@shirkit Sorry I missed your post earlier.

Yeah, I was going to look into the following camera approach. Still have to work out some details though

@kotoko, I agree with you. It’s misleading. Since we’re talking about this, I have an example anyone can try to demonstrate how value works:



This is in where I initialize mouse movements:



[java]inputManager.addMapping(“mouse_moveright”, new MouseAxisTrigger(MouseInput.AXIS_X, true));

inputManager.addMapping(“mouse_moveleft”, new MouseAxisTrigger(MouseInput.AXIS_X, false));

inputManager.addMapping(“mouse_moveup”, new MouseAxisTrigger(MouseInput.AXIS_Y, true));

inputManager.addMapping(“mouse_movedown”, new MouseAxisTrigger(MouseInput.AXIS_Y, false));

inputManager.addListener(analogListener, “mouse_moveup”);

inputManager.addListener(analogListener, “mouse_movedown”);

inputManager.addListener(analogListener, “mouse_moveleft”);

inputManager.addListener(analogListener, “mouse_moveright”);[/java]



[java]if (name.equals(“mouse_movedown”)) {

board2.setLocalTranslation(board2.getLocalTranslation().add(new Vector3f(0, movementvalue, 0)));

} else if (name.equals(“mouse_moveleft”)) {

board2.setLocalTranslation(board2.getLocalTranslation().add(new Vector3f(movement
value, 0, 0)));

} else if (name.equals(“mouse_moveup”)) {

board2.setLocalTranslation(board2.getLocalTranslation().subtract(new Vector3f(0, movementvalue, 0)));

} else if (name.equals(“mouse_moveright”)) {

board2.setLocalTranslation(board2.getLocalTranslation().subtract(new Vector3f(movement
value, 0, 0)));

}[/java]



That is in the onAnalog. ‘board2’ is a quad attached to the guiNode. The variable ‘movement’ is large (1000 is what I use). The higher it is, the faster ‘board2’ moves. Since I use the ‘movement’ variable, ‘board2’ isn’t in the same position as the mouse’s cursor, so I use the inputManager to make it invisible.



Hope I helped someone ^^

That’s still a bit unclear. I’d change it to:



JME gives you access to a gradual value showing the strength of that input. In the case of a keypress that will be the tpf value for which it was pressed since the last frame. For other inputs such as a joystick which give analogue control though then the value will also indicate the strength of the input.



In order to see the total time that a key has been pressed for then the incoming value can be accumulated. The analogue listener may also need to be combined with an action listener so that you are notified when they key is released.

I think change:

“then the tpf value will also indicate the strength of the input.”



To:

“then the value will also indicate the strength of the input premultiplied by tpf.”



I haven’t looked at the code, that’s just what I gather from this discussion.

I can’t do it today but next week I’ll take this discussion and put it in the wiki.

I’ll warn here so someone can validate.