Mouse Axis trigger

I am currently working on a project and trying to recreate some of my unity 4 projects into Jmonkey. Unity has this function, Input.getAxis(“Mouse X”) (you can use any defined axis in the string), and it will return essentially the speed that the mouse is moving and the positive or negative direction. It would look as so…if(Input.GetAxis(“Mouse X”)<0){

print("Mouse moved left");

}
if(Input.GetAxis(“Mouse X”)>0){

print("Mouse moved right");

}
In jmonkey, I created a mouseAxisTrigger…inputManager.addMapping(“MouseLeft”, new MouseAxisTrigger(MouseInput.AXIS_X, true)), one true and one false to map both left and right. In the analog listener, i do…
if (name.equals(“MouseLeft”)) {

        mousex = value;

    } else if (name.equals("MouseRight")) {
        mousex = value;
    } else if (!name.equals("MouseLeft") &amp;&amp; !name.equals("MouseRight")){
        mousex = 0;
    }


}

so theoretically, mousex should be similar to the value in unity. However, I discovered that if left is the last direction you moved your mouse, “Mouseleft” is still “true”. Same with right. Also, .625 is the max number it can go to. Sometimes, the number gets stuck at 9.765625E-4 for an unknown reason. Is there a better way to do this? If I am on the right track, could someone possibly tell me what I am doing wrong?

1 Like

I think you will have to post more of your code since the strangeness you describe is actually outside of what has been posted.

Also, you might want to describe what you are actually trying to do since there may be a better way than trying to directly adapt idioms from another platform.

I literally posted all the code that does it.
public void Keys() {
inputManager.addMapping(“MouseLeft”, new MouseAxisTrigger(MouseInput.AXIS_X, true));
inputManager.addMapping(“MouseRight”, new MouseAxisTrigger(MouseInput.AXIS_X, false));
inputManager.addListener(this, “MouseLeft”);
inputManager.addListener(this, “MouseRight”);
}
double mousex = 0;

    public void update(float tpf) {
            

            System.out.println(mousex);

    }


    @Override
    public void onAnalog(String name, float value, float tpf) {
            if (name.equals("MouseLeft")) {

                    mousex = value;

            } else if (name.equals("MouseRight")) {
                    mousex = value;
            } else if (!name.equals("MouseLeft") &amp;&amp; !name.equals("MouseRight")) {
                    mousex = 0;
            }


    }

So the mouse axis is used a lot in Unity, because anything can be ascribed to the mouse movement. Say I move the mouse left, the value moves up based on the speed I am moving the mouse. If I move it right, the number goes negative and does the same thing. I essentially did it in JMonkey but it is much less reliable.

1 Like

Well, onAnalog is only called when things change so it will never be 0.

Edit: in the case of mouse axis that is. It’s only called for changes in x or y.

@Jmonkier said:

So the mouse axis is used a lot in Unity, because anything can be ascribed to the mouse movement. Say I move the mouse left, the value moves up based on the speed I am moving the mouse. If I move it right, the number goes negative and does the same thing. I essentially did it in JMonkey but it is much less reliable.

In JME, onAnalog() the ‘speed’ is inherent in the value received. For the mouse, the value is relative to the last physical x or y value. So a big value means the mouse moved very fast. A small value means the mouse moved very slow.

The values are the equivalent of MouseEvent.getDX() or getDY() divided (somewhat arbitrarily) by 1024.0. They are the amount the mouse has moved since the last time it was polled.

@pspeed said: In JME, onAnalog() the 'speed' is inherent in the value received. For the mouse, the value is relative to the last physical x or y value. So a big value means the mouse moved very fast. A small value means the mouse moved very slow.

The values are the equivalent of MouseEvent.getDX() or getDY() divided (somewhat arbitrarily) by 1024.0. They are the amount the mouse has moved since the last time it was polled.


Thank you. That seems to be exactly what I am looking for. However, I am not sure what goes in the parameters. So far i have
MouseMotionEvent mm = new MouseMotionEvent(0, 0, 1024, 720, 0, 0);
public void update(float tpf) {

            mousex = mm.getDX() / 1024;

mousex doesnt change. Are my parameters wrong?

1 Like
@Jmonkier said: Thank you. That seems to be exactly what I am looking for. However, I am not sure what goes in the parameters. So far i have MouseMotionEvent mm = new MouseMotionEvent(0, 0, 1024, 720, 0, 0); public void update(float tpf) {
            mousex = mm.getDX() / 1024;

mousex doesnt change. Are my parameters wrong?

No, you don’t create your own mouse event. That would only have the values you give it.

My point is that the raw input events that JME processes to call the triggers are passing the getDX and getDY directly to the triggers. If you multiply them by 1024 then you get the exact values even.

…but nothing will call your onAnalog() with 0… you just won’t be called. So you either have to reset to zero some other way, user triggers as they are designed, or go the RawInputListener route and do your own processing.

Note: you are going to be doing something with your value. Whatever that ‘something’ is could probably just be done in the trigger itself and avoid the indirection. But I’m also not exactly sure what you are expecting the values to look like with respect to input. Usually triggers are a “Oh, I see the value is active or has changed so I will do something” kind of thing… which I presume is what your mousex is going to be used for in the end anyway.

@pspeed said: No, you don't create your own mouse event. That would only have the values you give it.

My point is that the raw input events that JME processes to call the triggers are passing the getDX and getDY directly to the triggers. If you multiply them by 1024 then you get the exact values even.

…but nothing will call your onAnalog() with 0… you just won’t be called. So you either have to reset to zero some other way, user triggers as they are designed, or go the RawInputListener route and do your own processing.

Note: you are going to be doing something with your value. Whatever that ‘something’ is could probably just be done in the trigger itself and avoid the indirection. But I’m also not exactly sure what you are expecting the values to look like with respect to input. Usually triggers are a “Oh, I see the value is active or has changed so I will do something” kind of thing… which I presume is what your mousex is going to be used for in the end anyway.

What I am going to be doing is moving an object based on the speed I am moving the mouse, so those DX and DY values are pretty crucial. Can I get acess to those values in the mouse axis trigger that I already made?

1 Like
@Jmonkier said: What I am going to be doing is moving an object based on the speed I am moving the mouse, so those DX and DY values are pretty crucial. Can I get acess to those values in the mouse axis trigger that I already made?

They are the value that is passed. Multiply it by 1024 and it’s exactly the same as DX or DY depending on which axis.

I’m not sure how state that differently but I feel like this is the third time I’ve said it. :wink:

@pspeed said: They are the value that is passed. Multiply it by 1024 and it's exactly the same as DX or DY depending on which axis.

I’m not sure how state that differently but I feel like this is the third time I’ve said it. :wink:


So basically I am doing the exact same thing I had before execpt multiplying my mousex by 1024. If I shouldn’t do It in analog listener then where should I set the values?

1 Like

I think your problem is that your value never goes to zero. It never gets reset to zero because there are no events that come through for zero. So once you set it to a value (onAnalog’s value, getDX, whatever) it will never change from that value until you get another left or right event… that sets it to a different value.

So what you end up with is some semi-complicated logic where you set your mousex value in onAnalog and then reset it to zero after you’ve used it. And this cycle would happen every update that there is an event (when the mouse moves) and nothing would happen on any other frames.

…which is more or less precisely what would happen if you moved your “move my object based on value” right into onAnalog. So what about that doesn’t work. I still don’t understand. If the user moves the mouse to the right very fast and then stops, what should happen on the screen?

I kind of got it working, however the problem I am having is setting the mousex to 0 when the mouse stops moving as you said. I did it in the analog listener. How should I combat this problem?

1 Like

Haha sorry I posted right when I got your reply. So I have a gun that is supposed to be smoothed movement so when I rotate it moves further from the location because it adds the Mousex value onto the translation. I got it working except when the mouse movement stops, the gun stays a certain distance from 0,0,0 because mousex has a value. So the input is working, the problem is just getting the mouse x to zero so the gun can interpolate back to its origin.

1 Like
@Jmonkier said: Haha sorry I posted right when I got your reply. So I have a gun that is supposed to be smoothed movement so when I rotate it moves further from the location because it adds the Mousex value onto the translation. I got it working except when the mouse movement stops, the gun stays a certain distance from 0,0,0 because mousex has a value. So the input is working, the problem is just getting the mouse x to zero so the gun can interpolate back to its origin.

You could always be interpolating the gun back to its origin and just let the mouse movement work against that. It would also let you knock it around for other reasons if you wanted, also.

…then you could just move the gun right in the onAnalog() call. If you aren’t getting mouse motion then it would settle back into origin. The other added benefit is that small slow movements wouldn’t really move the gun at all (depending on how strongly it snaps back to origin.)

Wow!! Thanks so much. The solution was so obvious I missed it. As u said, after I use mousex set it to 0 so if it doesn’t change, it’s zero. it worked! Thanks for bearing with me along the way!

1 Like
@Jmonkier said: Wow!! Thanks so much. The solution was so obvious I missed it. As u said, after I use mousex set it to 0 so if it doesn't change, it's zero. it worked! Thanks for bearing with me along the way!

Heheh. It was a relatively slow forum day. Apparently other people have lives and do stuff on the weekends other than programming games. How crazy.

1 Like
@pspeed said: Heheh. It was a relatively slow forum day. Apparently other people have lives and do stuff on the weekends other than programming games. How crazy.
There's a life outside of programming? :-o
1 Like
@pspeed said: Heheh. It was a relatively slow forum day. Apparently other people have lives and do stuff on the weekends other than programming games. How crazy.

O .o This… could not… be.

@Jmonkier said: There's a life outside of programming? :-o

Yeah, my wife keeps trying to tell me something like that but she forgets her closing braces so I don’t understand. :wink: