Saturday, January 24, 2009

JME Mouse Movement #10

In this post I am going to show you how to move the camera with the mouse. I only want to allow the user to move the camera from left to right, and not up and down. I am going to use the FirstPersonHandler class. This class contains a class called MouseLookHandler and with this class you are suppose to be able to lock the axis. However, this doesn't let me lock the axis in the y direction. When I look at the code, I only see the the rotateLeft and the rotateRight lockable.

rotateLeft.setLockAxis(lockAxis);
rotateRight.setLockAxis(lockAxis);

Therefore, I modified the code so the camera won't rotate in the y direction.

This is the modified FirstPersonHandler class. The only difference is that I changed the MouseLookHandler to be my modified MouseLookHandler.

NewFirstPersonHandler.java


/*
* Copyright (c) 2003-2006 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.guid.asteroid3D;

import com.jme.input.InputHandler;
import com.jme.input.KeyboardLookHandler;
import com.jme.input.MouseLookHandler;
import com.jme.renderer.Camera;

/**
* <code>FirsPersonController</code> defines an InputHandler that sets
* input to be controlled similar to First Person Shooting games. By default the
* commands are, WSAD moves the camera forward, backward and strafes. The
* arrow keys rotate and tilt the camera and the mouse also rotates and tilts
* the camera. <br>
* This is a handler that is composed from {@link KeyboardLookHandler} and {@link MouseLookHandler}.
* @author Mark Powell
* @version $Id: FirstPersonHandler.java,v 1.17 2007/08/26 08:44:20 irrisor Exp $
*/
public class NewFirstPersonHandler extends InputHandler {
private NewMouseLookHandler mouseLookHandler;
private KeyboardLookHandler keyboardLookHandler;

/**
* @return handler for keyboard controls
*/
public KeyboardLookHandler getKeyboardLookHandler() {
return keyboardLookHandler;
}

/**
* @return handler for mouse controls
*/
public NewMouseLookHandler getMouseLookHandler() {
return mouseLookHandler;
}

public void setButtonPressRequired(boolean value) {
mouseLookHandler.requireButtonPress(value);
}

/**
* Creates a first person handler.
* @param cam The camera to move by this handler.
*/
public NewFirstPersonHandler( Camera cam ) {
mouseLookHandler = new NewMouseLookHandler( cam, 1 );
addToAttachedHandlers( mouseLookHandler );
keyboardLookHandler = new KeyboardLookHandler( cam, 0.5f, 0.01f );
addToAttachedHandlers( keyboardLookHandler );
}

/**
* Creates a first person handler.
* @param cam The camera to move by this handler.
* @param moveSpeed action speed for move actions
* @param turnSpeed action speed for rotating actions
*/
public NewFirstPersonHandler(Camera cam, float moveSpeed, float turnSpeed ) {
mouseLookHandler = new NewMouseLookHandler( cam, turnSpeed );
addToAttachedHandlers( mouseLookHandler );
keyboardLookHandler = new KeyboardLookHandler( cam, moveSpeed, turnSpeed );
addToAttachedHandlers( keyboardLookHandler );
}
}



In the NewMouseLookHandler class, the only thing I changed was the MouseLook. I am using my modified MouseLook class.

NewMouseLookHandler.java

package com.guid.asteroid3D;

/*
* Copyright (c) 2003-2006 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/



import com.jme.input.InputHandler;
import com.jme.input.RelativeMouse;
import com.jme.input.action.MouseLook;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;

/**
* <code>MouseLookHandler</code> defines an InputHandler that allows to rotate the camera via the mouse.
*/
public class NewMouseLookHandler extends InputHandler {

private NewMouseLook mouseLook;

public NewMouseLookHandler( Camera cam, float speed ) {
RelativeMouse mouse = new RelativeMouse("Mouse Input");
mouse.registerWithInputHandler( this );

mouseLook = new NewMouseLook(mouse, cam, speed );
mouseLook.setLockAxis(new Vector3f(cam.getUp()));
addAction(mouseLook);
}

public void requireButtonPress(boolean value) {
mouseLook.setButtonPressRequired(value);
}

public void setLockAxis(Vector3f lock) {
mouseLook.setLockAxis(new Vector3f(lock));
}

public MouseLook getMouseLook() {
return mouseLook;
}
}




This is the class that I modified. The only thing I changed was to set the mouse y axis to 0 so the user can't move the mouse in the y direction.

NewMouseLook.java

package com.guid.asteroid3D;

import com.jme.input.Mouse;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.MouseLook;
import com.jme.renderer.Camera;

public class NewMouseLook extends MouseLook{

public NewMouseLook(Mouse mouse, Camera camera, float speed) {
super(mouse, camera, speed);

}

public void performAction(InputActionEvent evt) {
super.mouse.getLocalTranslation().y=0;
super.performAction(evt);

}

}




In your Asteroid3DGame.java class, add this global variable: private InputHandler handler=null;

In the initGame method in your Asteroid3DGame.java class, you should add this coded.

NewFirstPersonHandler fpHandler = new NewFirstPersonHandler( cam, 50, 1 );
fpHandler.getKeyboardLookHandler().setEnabled( false );
fpHandler.getMouseLookHandler().setEnabled( true);
fpHandler.getMouseLookHandler().requireButtonPress(true);
handler = fpHandler;


This code will create the FirstPerson camera that will allow the scene to move with the mouse only in the x direction.

In your update method, you should also add this code to update your handler.
handler.update(interpolation);




Go To #11

8 comments:

Anonymous March 5, 2009 9:17 AM  

Hello

Thanks for this tutorial.

Is it possible to get the sources (gfx, ...) as a zip file please ?

Thanks a lot.
Best Regards.

Gauthier

Greg March 5, 2009 10:51 AM  

Post your email and I will send it to you via email.

Thanks.

Juno March 20, 2009 1:20 AM  

Hi!!

thank you very much for this tutorial..

well I'm new with jMonkey but not with java, so is easier for us to have a bit of knowledge before getting into with this technology.

I'd like to know if you can help me, because I'm trying to set a menu bar during the rendering on an Virtual Environment I'm designing, but It has been difficult, do you know a class that could help me with that?

For example, I need the one used in the videogame: The Sims, where you can see the feelings and friends that your avatar has.., which appears all the time during the game is running.

Thanks for your help.

here is my email: parkjuno@ymail.com

Greg March 21, 2009 2:15 PM  

You might want to try using FengGUI if you are using JME. FengGUI will allow you to create 3D Menu Bars.

Massimo February 15, 2010 6:43 PM  

Hi,
I am sorry, I know this post hasn't been active for a long time but I am an absolute beginner with JME and I was wandering if you could help.

What I'm trying to do is to remove the mouse control from the camera view. So, that I can be able to move my mouse without changing the current view.
Is this something that can be done?

Thanks a lot!

Greg February 16, 2010 11:17 AM  

How do you plan to move the camera, by the keyboard? You can remove the mouse functionality from the camera.

addToAttachedHandlers( mouseLookHandler );

pHandler.getMouseLookHandler().setEnabled( true);

Try setting the MouseLookHandler to false by the above line. If you are using my modified class, you can also try to remove the addToAttachedHandlers(mouseLookHandler) and see if that works.

I haven't tested this so let me know if this doesn't work, and I will then find the best solution for you.

Thanks.

Massimo February 20, 2010 3:57 AM  

Thanks a lot for your reply!

Ok, I still haven't managed to get it working..

What I'm trying to do is to use JME on a multitouch table connected to a microscope.
From the microscope I get the current view and then on the multi touch I want to be able to select "particles" and move them as I want.
I want to use a slider on screen or a joystick to move to different views of the specimen.
Instead, through the mouse or the multi touch I just want to select and/or move objects.

Do you think this is achievable in JME?

Sorry for being such a newbie! I am a physicist and getting my head around this stuff is really hard!

Thank you in advance!
massimo

Greg February 22, 2010 10:51 AM  

yes, this is possible in JME. It all depends on what you mount to the camera.

Did you add the mouse listener to your Camera Object? If you did, you need to remove it.

Do you have some code to post that I can look at?

Post the code where you create your Camera.

If you post code, make sure you use this website to convert it to HTML:
http://www.elliotswan.com/postable/