-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add native touch rotation to FlyByCamera #2943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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())); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests invoke Drive the event through the input pipeline instead, e.g. build an Also note |
||
|
|
||
| @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()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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 anACTION_MOVEinto its ownMOVEevent, 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()returnsnullwhennumPointers > 1), andTouchTrigger(TouchInput.ALL)delivers all pointers to this listener.Suggested fix: latch the pointer that started the gesture on
DOWNand ignoreMOVEevents from other pointers (DOWN/UP also havekeyCode == 0, so they reach this mapping):There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Android assigns pointer id 0 to the first finger of a gesture (
event.getPointerId(getPointerIndex(event))inAndroidTouchInput.onTouch), andTouchEventdefaultspointerIdto 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 newDOWNwith id 0).The stricter variant described above (latch on
DOWN, clear onUP, ignore other pointers) needs a second edit site that cannot be folded into this suggestion: theevent.getType() != TouchEvent.Type.MOVEearly return must be relaxed soDOWN/UPreach the handler, plus aprivate int touchPointerId = -1;field next toinputMappingsRegistered. Both hunks are non-adjacent, so a single-suggestion apply is not possible for that version.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 != 0variant.1. State — applied at the field declaration (line 127, next to
inputMappingsRegistered):2. Guard/dispatch — replaces lines 580–582:
InputManager.onTouchEventQueued()dispatches onevt.getKeyCode(), which is 0 for everyDOWN/UP/MOVE/gesture event, so all of them reach theFLYCAM_Touchmapping; the switch is what separates them. Only the finger latched onDOWNrotates (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 nextDOWN.Since this thread is anchored at line 583, block 1 has to be applied at the field declaration, not here.