Moving models by mouse

I am new to JME and just started with making a game. I have gone through the tutorials. I have two models in the scene and I want to move one model by mouse and check for the collision with the other model and if collision takes place I want to move the other object. I do not know how to do it. Can any one please help me?

leorius

Try this example:
https://code.google.com/p/jme-simple-examples/source/browse/JMESimpleExamples/src/com/intermediate/MoveNodeOnSurface.java

I have already gone through the above example. But there is no code to show how to move an object ( model ) by mouse. There is only click event listener by which you can initiate the shoot event. I have tried Analog listener , but no result. Is there any other way to get the mouse axis ( x, y ) when we move the mouse?

@leorius said: I have already gone through the above example. But there is no code to show how to move an object ( model ) by mouse. There is only click event listener by which you can initiate the shoot event. I have tried Analog listener , but no result. Is there any other way to get the mouse axis ( x, y ) when we move the mouse?

You say you have tried analog listener with no result. What does that mean? Was your listener never called? That’s a setup problem… because I can assure you that analog listeners are called when the mouse moves.

if you run the example you will see that box is moving if you click on a plane.
You can use Analog listener or Update() method instead.

Here is what I have trtied.
in simpleInitApp():
this is the code:
inputManager.addMapping(“MoveX”, new MouseAxisTrigger(MouseInput.AXIS_X, false));
inputManager.addListener(analogListener, “MoveX”);

After the simpleInitApp this is the code before simpleUpdate():
private AnalogListener analogListener = new AnalogListener()
{
public void onAnalog(String name, float value, float tpf)
{
mousex = MouseInput.AXIS_X;
mousey = MouseInput.AXIS_Y;

        if(name.equals(“MoveX” )) 
        {                
            Ball.setLocalTranslation(mousex, mousey, -4f);
            Ball.updateGeometricState();
        }        
    }
};

In the code itself I got a red line in the following line:

if(name.equals(“MoveX” ))

and the tip says: “illegal character \ 8220
not a statement”

When I run run the app I got the following error message:

Sep 7, 2013 8:17:20 AM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at mygame.Main$1.onAnalog(Main.java:121)
at com.jme3.input.InputManager.invokeAnalogsAndActions(InputManager.java:280)
at com.jme3.input.InputManager.onMouseMotionEventQueued(InputManager.java:394)
at com.jme3.input.InputManager.processQueue(InputManager.java:831)
at com.jme3.input.InputManager.update(InputManager.java:885)
at com.jme3.app.Application.update(Application.java:606)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:230)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:662)
Sep 7, 2013 8:17:20 AM com.jme3.renderer.lwjgl.LwjglGL1Renderer cleanup
INFO: Deleting objects and invalidating state
Sep 7, 2013 8:17:20 AM com.jme3.scene.Node detachChildAt
INFO: Gui Node (Node): Child removed.
Sep 7, 2013 8:17:20 AM com.jme3.scene.Node detachChildAt
INFO: Gui Node (Node): Child removed.
Sep 7, 2013 8:17:20 AM com.jme3.input.lwjgl.LwjglMouseInput destroy
INFO: Mouse destroyed.
Sep 7, 2013 8:17:20 AM com.jme3.input.lwjgl.LwjglKeyInput destroy
INFO: Keyboard destroyed.
Sep 7, 2013 8:17:20 AM com.jme3.system.lwjgl.LwjglAbstractDisplay deinitInThread
INFO: Display destroyed.
BUILD SUCCESSFUL (total time: 22 seconds)

I do not know what and where is the mistake.

leorius

This:

java.lang.RuntimeException: Uncompilable source code – Erroneous tree type:

Is generally a sign that the netbeans incremental compiler is messing up. I got so tired of these sorts of errors that I eventually turned it off. You can do this in your project settings by going to the compile tab and turning off “Compile on save”. Then rebuild your project and see if that gets you past it.

Once you fix that… the reason your code wont work is this these lines here.

mousex = MouseInput.AXIS_X;
mousey = MouseInput.AXIS_Y;

MouseInput.AXIS_X isnt the x position of the mouse, it’s just some random integer assigned to that variable. Its just used as a common definition for that mouse axis, which in this case is used by MouseAxisTrigger.

To get the screen coordinates of the mouse use inputManager.getCursorPosition(). Then [if you need it for your game] use ray casting with your cursor position to find what geometry in the world your mouse is over and where its at in the 3d world etc. (which I think is in a previous link posted by @mifth)