How to move the object following the mouse?

I got problem with this: What I want to implement is making the object moving with the mouse, but my code cannot make the object move to the area where z<0 and x<0… can anyone help to figure this out?

    inputManager.addMapping("Right", 
            new MouseAxisTrigger(MouseInput.AXIS_X, true)
    );

    inputManager.addMapping("Left", 
            new MouseAxisTrigger(MouseInput.AXIS_X, false)
    );
    inputManager.addMapping("Up", 
            new MouseAxisTrigger(MouseInput.AXIS_Y, false)
    );
    inputManager.addMapping("Down", 
            new MouseAxisTrigger(MouseInput.AXIS_Y, true)
    );

// Add the names to the action listener.
inputManager.addListener(analogListener,"Right", "Left", "Up","Down");
inputManager.getCursorPosition();

}

private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
Vector3f v = tool.getLocalTranslation();
if (name.equals(“Up”)) {
tool.setLocalTranslation(v.x, v.y, v.z - (float)value30speed);
}
else if (name.equals(“Down”)) {
tool.setLocalTranslation(v.x, v.y, v.z + (float)value30speed);
}
else if (name.equals(“Right”)) {
tool.setLocalTranslation(v.x - (float)value30speed, v.y, v.z);
}
else if (name.equals(“Left”)) {
tool.setLocalTranslation(v.x + (float)value30speed, v.y, v.z);
}
}
};

This is a case where very basic debugging will probably tell you what is happening.

For example, I bet if you simply printed out the “value” in your onAnalog() method then you’d see what’s going on. This kind of “verifying assumptions” is the cornerstone to debugging so its important to figure it out early as you learn Java.

Yes, I have printed them out… but they are always not less than -1… I don’t know why…
cam.setLocation(new Vector3f(0.0f,13.0f,28.0f));
cam.lookAt(new Vector3f(0,0,9f), new Vector3f(0,10,0));
this is the camera I have set, is something wrong with then?..

Print VALUE the VALUE that is passed, ie: “value” as in the “value” argument in the onAnalog() method… pretty much exactly how I said.

Here is the code:
System.out.println(“The value:”+ value);

Your assumption is that value is always positive. When you run into problems, you need to question every assumption… but especially those kind.

Edit: actually it’s possible that I misread your response. So I will only deal with actual code from now on.

What is “tool”, is it a 3d object in the scene?
What is the value of the “speed” variable used in the code to translate the tool?

You might want to debug print the sum of (float)value*30*speed and also have a look at Spatial.move()

Well, working in one direction and not the other implies that he expects value to always be positive but value will definitely be negative sometimes… for example, going left. I was hoping to show him how to find the problem himself but that’s definitely the issue: + negative = - positive in this case… so the object will only move in one direction.

That’s interesting because the javadoc states the following :smile:

void onAnalog(java.lang.String name, float value, float tpf)
Called to notify the implementation that an analog event has occurred. The results of KeyTrigger and MouseButtonTrigger events will have tpf == value.

Parameters:
name - The name of the mapping that was invoked
value - Value of the axis, from 0 to 1.
tpf - The time per frame value.

Yeah, you are right… I looked in the code and it looks like it’s passing the deltas directly but then I looked at flybycamera and it’s treating them as always positive.

Anyway, OP, you can look at FlyByCamera.java to see how it does things and then see what’s different.

System.out.println() is still a good idea to find things. An when you do, post the output and the code so that we don’t have to make assumptions.

1 Like

Thanks dude, I have got he problem that I didn’t do getInputManager().setCursorVisible(false);, now it works pretty well~ haha