Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions jme3-core/src/main/java/com/jme3/input/FlyByCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.TouchListener;
import com.jme3.input.controls.TouchTrigger;
import com.jme3.input.event.TouchEvent;
import com.jme3.math.Matrix3f;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
Expand All @@ -54,12 +57,14 @@
* - WASD keys for moving forward/backward and strafing
* - QZ keys raise or lower the camera
*/
public class FlyByCamera implements AnalogListener, ActionListener, JoystickConnectionListener {
public class FlyByCamera implements AnalogListener, ActionListener, JoystickConnectionListener, TouchListener {

private static final String FLYCAM_JOYSTICK_LEFT = "FLYCAM_JoystickLeft";
private static final String FLYCAM_JOYSTICK_RIGHT = "FLYCAM_JoystickRight";
private static final String FLYCAM_JOYSTICK_UP = "FLYCAM_JoystickUp";
private static final String FLYCAM_JOYSTICK_DOWN = "FLYCAM_JoystickDown";
private static final String FLYCAM_TOUCH = "FLYCAM_Touch";
private static final float TOUCH_ROTATION_SCALE = 1f / 1024f;

private static final String[] mappings = new String[]{
CameraInput.FLYCAM_LEFT,
Expand All @@ -84,7 +89,8 @@ public class FlyByCamera implements AnalogListener, ActionListener, JoystickConn
FLYCAM_JOYSTICK_LEFT,
FLYCAM_JOYSTICK_RIGHT,
FLYCAM_JOYSTICK_UP,
FLYCAM_JOYSTICK_DOWN
FLYCAM_JOYSTICK_DOWN,
FLYCAM_TOUCH
};
/**
* camera controlled by this controller (not null)
Expand Down Expand Up @@ -306,6 +312,7 @@ private void registerInputMappings() {
inputManager.addMapping(CameraInput.FLYCAM_ZOOMIN, new MouseAxisTrigger(MouseInput.AXIS_WHEEL, false));
inputManager.addMapping(CameraInput.FLYCAM_ZOOMOUT, new MouseAxisTrigger(MouseInput.AXIS_WHEEL, true));
inputManager.addMapping(CameraInput.FLYCAM_ROTATEDRAG, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping(FLYCAM_TOUCH, new TouchTrigger(TouchInput.ALL));

// keyboard only WASD for movement and WZ for rise/lower height
inputManager.addMapping(CameraInput.FLYCAM_STRAFELEFT, new KeyTrigger(KeyInput.KEY_A));
Expand Down Expand Up @@ -567,4 +574,14 @@ public void onAction(String name, boolean isPressed, float tpf) {
}
}
}

@Override
public void onTouch(String name, TouchEvent event, float tpf) {
if (!enabled || !name.equals(FLYCAM_TOUCH) || event.getType() != TouchEvent.Type.MOVE) {
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-touch is not handled: AndroidTouchInput.onTouch() converts every pointer in an ACTION_MOVE into its own MOVE event, so with two fingers moving both deltas are applied here and the camera rotates at roughly double speed / jerks. The emulated-mouse path explicitly avoids this (generateMouseEvent() returns null when numPointers > 1), and TouchTrigger(TouchInput.ALL) delivers all pointers to this listener.

Suggested fix: latch the pointer that started the gesture on DOWN and ignore MOVE events from other pointers (DOWN/UP also have keyCode == 0, so they reach this mapping):

private int touchPointerId = -1;
...
case DOWN: if (touchPointerId == -1) touchPointerId = event.getPointerId(); break;
case UP:   if (event.getPointerId() == touchPointerId) touchPointerId = -1; break;
case MOVE: if (event.getPointerId() != touchPointerId) return;  // then rotate

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a code suggestion to fix this issue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committable minimal fix: drop the secondary pointers before the deltas are applied.

Suggested change
// Only the initial (primary) finger may rotate the camera: AndroidTouchInput
// emits one MOVE event per pointer, so without this filter every finger's
// delta would reach the two rotateCamera() calls below.
if (event.getPointerId() != 0) {
return;
}

Android assigns pointer id 0 to the first finger of a gesture (event.getPointerId(getPointerIndex(event)) in AndroidTouchInput.onTouch), and TouchEvent defaults pointerId to 0, so existing callers and the current tests keep working. Trade-off: if the primary finger is lifted while a second finger keeps dragging, rotation pauses until the next gesture (a new DOWN with id 0).

The stricter variant described above (latch on DOWN, clear on UP, ignore other pointers) needs a second edit site that cannot be folded into this suggestion: the event.getType() != TouchEvent.Type.MOVE early return must be relaxed so DOWN/UP reach the handler, plus a private int touchPointerId = -1; field next to inputMappingsRegistered. Both hunks are non-adjacent, so a single-suggestion apply is not possible for that version.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propose both changes with two different suggestions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both hunks of the stricter variant, applied together (the switch will not compile without the field). This supersedes the minimal pointerId != 0 variant.

1. State — applied at the field declaration (line 127, next to inputMappingsRegistered):

Suggested change
private boolean inputMappingsRegistered;
private int touchPointerId = -1;

2. Guard/dispatch — replaces lines 580–582:

Suggested change
if (!enabled || !name.equals(FLYCAM_TOUCH)) {
return;
}
switch (event.getType()) {
case DOWN:
if (touchPointerId == -1) {
touchPointerId = event.getPointerId();
}
return;
case UP:
if (event.getPointerId() == touchPointerId) {
touchPointerId = -1;
}
return;
case MOVE:
if (event.getPointerId() != touchPointerId) {
return;
}
break;
default:
return;
}

InputManager.onTouchEventQueued() dispatches on evt.getKeyCode(), which is 0 for every DOWN/UP/MOVE/gesture event, so all of them reach the FLYCAM_Touch mapping; the switch is what separates them. Only the finger latched on DOWN rotates (Android assigns pointer id 0 to the first finger of a gesture); if that finger lifts while a second one keeps dragging, rotation pauses until the next DOWN.

Since this thread is anchored at line 583, block 1 has to be applied at the field declaration, not here.

rotateCamera(-event.getDeltaX() * TOUCH_ROTATION_SCALE, initialUpVec, true);
rotateCamera(-event.getDeltaY() * TOUCH_ROTATION_SCALE * (invertY ? -1 : 1), cam.getLeft(tempLeft), true);
}
}
77 changes: 77 additions & 0 deletions jme3-core/src/test/java/com/jme3/input/FlyByCameraTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2026 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.
*
* * 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.jme3.input;

import com.jme3.input.event.TouchEvent;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

class FlyByCameraTest {

@Test
void touchDragRotatesCamera() {
Camera camera = new Camera(640, 480);
FlyByCamera flyCam = new FlyByCamera(camera);
Vector3f initialDirection = camera.getDirection().clone();

flyCam.onTouch("FLYCAM_Touch",
new TouchEvent(TouchEvent.Type.MOVE, 0f, 0f, 128f, 0f), 0f);

assertFalse(initialDirection.equals(camera.getDirection()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests invoke onTouch() directly, so they pass even if the FLYCAM_Touch mapping, the TouchTrigger, or the TouchListener registration were missing — the wiring this PR fixes is untested, and the direction/scale of the rotation is not asserted either.

Drive the event through the input pipeline instead, e.g. build an InputManager (dummy mouse/key inputs), call flyCam.registerWithInput(inputManager), then inputManager.onTouchEventQueued(new TouchEvent(TouchEvent.Type.MOVE, 0f, 0f, 128f, 0f)) and assert the sign of the resulting yaw change. That also gives you a place to cover the multi-touch case and the non-MOVE event filter.

Also note touchDragRotatesCameraWhenDragToRotateIsEnabled currently asserts exactly the same thing as touchDragRotatesCamera.


@Test
void touchDragRotatesCameraWhenDragToRotateIsEnabled() {
Camera camera = new Camera(640, 480);
FlyByCamera flyCam = new FlyByCamera(camera);
flyCam.setDragToRotate(true);
Vector3f initialDirection = camera.getDirection().clone();

flyCam.onTouch("FLYCAM_Touch",
new TouchEvent(TouchEvent.Type.MOVE, 0f, 0f, 128f, 0f), 0f);

assertFalse(initialDirection.equals(camera.getDirection()));
}

@Test
void disabledFlyByCameraIgnoresTouchDrag() {
Camera camera = new Camera(640, 480);
FlyByCamera flyCam = new FlyByCamera(camera);
flyCam.setEnabled(false);
Vector3f initialDirection = camera.getDirection().clone();

flyCam.onTouch("FLYCAM_Touch",
new TouchEvent(TouchEvent.Type.MOVE, 0f, 0f, 128f, 0f), 0f);

assertEquals(initialDirection, camera.getDirection());
}
}
Loading