i haven't found anything in jme to prevent the "turn the world upside down"-behaviour of the mouselook+camera-combination when looking upwards too much. i've dug through the code a bit and found these two guys:
private KeyLookDownAction lookDown;
private KeyLookUpAction lookUp;
inside these, there i an update method.
/**
* <code>performAction</code> adjusts the view of the camera to tilt up a
* given angle. This angle is determined by the camera's speed and the time
* which has passed.
*
* @see com.jme.input.action.KeyInputAction#performAction(InputActionEvent)
*/
public void performAction(InputActionEvent evt) {
incr.fromAngleNormalAxis(-speed * evt.getTime(), camera.getLeft());
incr.mult(camera.getLeft(), camera.getLeft());
incr.mult(camera.getDirection(), camera.getDirection());
incr.mult(camera.getUp(), camera.getUp());
camera.normalize();
camera.update();
}
i'm pretty sure a oneliner can fix my problem, but since i don't know how quaternions work, i'm lost and need your help.
An alternative is to make the mouse affect a "pitch" and a "yaw" scalar variable, where you can lock the pitch to +/-1.2. Then, when you actually need the rotation, you create it from the pitch and yaw angles, instead of trying to multiply in new rotations all the time, which may require re-normalization, and may accumulate some roll over time.
/*
* 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.
*/
/**
* <code>MouseLook</code> defines a mouse action that detects mouse movement
* and converts it into camera rotations and camera tilts.
*
* @author Mark Powell
* @version $Id: MouseLook.java,v 1.18 2007/08/02 21:38:55 nca Exp $
*/
public class LockedMouseLook extends MouseInputAction {
/**
* Developed with pleasure :)<br>
*
* @author HamsterofDeath
* Created 07.12.2007 @ 21:14:37
*/
public class KeyLookUpAndDownAction extends KeyInputAction {
//maintains the rotation matrix
private static final Quaternion spin = new Quaternion();
//the camera to manipulate
@NotNull private final Camera m_camera;
private float m_speed;
private float m_angle = 0.0F;
@NotNull private final Vector3f m_tmp = new Vector3f();
public KeyLookUpAndDownAction(final Camera p_camera, final float p_speed) {
super();
this.m_camera = p_camera;
this.m_speed = p_speed;
}
public void setSpeed(final float p_speed) {
m_speed = p_speed;
}
I am seeking a solution to prevent this flip of the camera, but this code is outdated ? Anyone can help me to fixe it ?
I have already override the FlyByCam to lock movement on Y axis (can’t use the physics engine for now in my project, so no gravity…), is it the better solution? or one other exist?
I have looked this document, i understand how work vector and approximatly quaternion but i dont understand how limit the rotation to up/down cause it’s on axe x and z… and where this lock must be implement ? in the rotateCamera method of flybycamera class?
you just use quaternion.lookAt(direction, Vector3f.UNIT_Y); then its always y-up… Then if direction has no x component (direction.x=0) it won’t rotate in that direction.
I see about the lookAt but, i don’t know how use this to lock rotation to a certain degree… it’s certainly easy, but i tryed different thing and nothing work…
thanks, i replaced (always in the class “FlyByCamera”) the argument of the method call rotateCamera by the vector3f.UNIT_Y like you said, but nothing change…
The above code won’t prevent the cam to turn upside down.
The clue here is to add another condition:
As a matter of fact, we have the “up” vector. So if up.y turns negative, the camera is positioned upside down (I also made degrees out of the radians):