2
\$\begingroup\$

I override the pan method in ActorGestureListener to implement dragging actors in libgdx (scene2d).

When I move individual pieces on a board they move smoothly, but when moving the whole board, the x and y coordinates that is sent to pan is "jumping", and in an increasingly amount the longer it is dragged.

These are an example of the deltaY coordinates sent to pan when dragging smoothly downwards:

 1.1156368
-0.13125038
-1.0500145
 0.98439217
-1.0500202
 0.91877174
-0.984396
 0.9187679
-0.98439026
 0.9187641
-0.13125038

This is how I move the camera:

public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
    cam.translate(-deltaX, -deltaY);

I have been using both the delta values sent to pan and the real position values, but similar results. And since it is the coordinates that are wrong, it doesn't matter whether I move the board itself or the camera.

What could the cause be for this and what is the solution?

When I move camera only half the delta-values, it moves smoothly but only at half the speed of the mouse pointer:

cam.translate(-deltaX / 2, -deltaY / 2);

It seems like the moving of camera or board affects the mouse input coordinates. How can I drag at "mouse speed" and still get smooth movements?

(This question was also posted on stackoverflow: https://stackoverflow.com/questions/20693020/libgdx-actorgesturelistener-pan-parameters-not-moving-actor-in-smooth-line)

\$\endgroup\$
1
\$\begingroup\$

try this: for moving camera

public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
    camera.position.add((-x), (y), 0);
    camera.update();
}

for moving actor

public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
    actor.setPosition(actor.positionX + deltaX, actor.positionY + deltaY);
}
\$\endgroup\$
1
\$\begingroup\$

I'm aware this is really late and you've probably already solved the problem but for future readers:

This forum post helped me: http://badlogicgames.com/forum/viewtopic.php?f=11&t=12343

If you move the camera while part way through a pan or ANY gesture the coordinate system being used for the x and y coordinates is changed and that's why you get the jumpy deltas alternating between +ve and -ve values.

The fix I used is to not use the pan() gesture and implement it like so:

private Vector3 prevDragPos;
addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            x = Gdx.input.getX(pointer);
            y = Gdx.input.getY(pointer);
            if(prevDragPos == null) prevDragPos = new Vector3(x, y, 0);

            getStage().getCamera().position.add(prevDragPos.x - x), y - prevDragPos.y, 0);
            prevDragPos.set(x, y, 0);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            prevDragPos = null ;
        }
    });

Gdx.input.getX() and Gdx.input.getY() don't get converted to camera coordinates so using them fixes the problem.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.