Skip to main content
added 45 characters in body
Source Link
public class CameraScroller extends InputAdapter implements GestureListener {
    public static final String TAG = "SCROLL_TAG";
    public static final float TIME_TO_SCROLL = 2.0f;

    private Camera mCamera;
    private final float mLowerPosition;
    private final float mUpperPosition;

    private GestureDetector mGestureDetector;
    private float mTimer;
    private float mVelocityY;

    /**
     * @param camera        {@link Camera} that have to be scrolled
     * @param upperPosition the upper limit position of camera
     * @param lowerPosition the lower limit position of camera
     */
    public CameraScroller(Camera camera, float lowerPosition, float upperPosition) {
        mUpperPosition = upperPosition;
        mLowerPosition = lowerPosition;
        mGestureDetector = new GestureDetector(this);
        mCamera = camera;
    }

    /**
     * Call in Screen.render()
     */
    public void act(float deltaTime) {
        if (mTimer > 0) {// if timer is not 0
            float acceleration_y = mVelocityY * deltaTime;// calculate acceleration (the rate of change of velocity)
            mTimer -= deltaTime;// decreasing timer
            mVelocityY -= acceleration_y;// decreasing velocity

          mCamera.position.y += mVelocityY;
          checkCameraPosition();// check if camera position is in not less or more than some value else stop camera (mTimer = 0)
        }
    }

    private void checkCameraPosition() {
        if (mCamera.position.y > mUpperPosition) {
            mCamera.position.y = mUpperPosition;
            mTimer = 0;
        }
        if (mCamera.position.y < mLowerPosition) {
            mCamera.position.y = mLowerPosition;
            mTimer = 0;
        }
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        mTimer = TIME_TO_SCROLL;
        mVelocityY = velocityY * Gdx.graphics.getDeltaTime();

        return true;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        float cam_y = mCamera.position.y;
        System.out.println();
        if (cam_y >= mLowerPosition && cam_y <= mUpperPosition) {
            mCamera.position.y += deltaY;
        }
        if (mCamera.position.y > mUpperPosition) mCamera.position.y = mUpperPosition;
        if (mCamera.position.y < mLowerPosition) mCamera.position.y = mLowerPosition;

        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        mVelocityY = 0;
        return true;
    }

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        mTimer = 0;
        return true;
    }

    public GestureDetector getGestureDetector() {
        return mGestureDetector;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        return false;
    }
}
public class CameraScroller extends InputAdapter implements GestureListener {
    public static final String TAG = "SCROLL_TAG";
    public static final float TIME_TO_SCROLL = 2.0f;

    private Camera mCamera;
    private final float mLowerPosition;
    private final float mUpperPosition;

    private GestureDetector mGestureDetector;
    private float mTimer;
    private float mVelocityY;

    /**
     * @param camera        {@link Camera} that have to be scrolled
     * @param upperPosition the upper limit position of camera
     * @param lowerPosition the lower limit position of camera
     */
    public CameraScroller(Camera camera, float lowerPosition, float upperPosition) {
        mUpperPosition = upperPosition;
        mLowerPosition = lowerPosition;
        mGestureDetector = new GestureDetector(this);
        mCamera = camera;
    }

    /**
     * Call in Screen.render()
     */
    public void act(float deltaTime) {
        if (mTimer > 0) {// if timer is not 0
            float acceleration_y = mVelocityY * deltaTime;// calculate acceleration (the rate of change of velocity)
            mTimer -= deltaTime;// decreasing timer
            mVelocityY -= acceleration_y;// decreasing velocity

            checkCameraPosition();// check if camera position is in not less or more than some value else stop camera (mTimer = 0)
        }
    }

    private void checkCameraPosition() {
        if (mCamera.position.y > mUpperPosition) {
            mCamera.position.y = mUpperPosition;
            mTimer = 0;
        }
        if (mCamera.position.y < mLowerPosition) {
            mCamera.position.y = mLowerPosition;
            mTimer = 0;
        }
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        mTimer = TIME_TO_SCROLL;
        mVelocityY = velocityY * Gdx.graphics.getDeltaTime();

        return true;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        float cam_y = mCamera.position.y;
        System.out.println();
        if (cam_y >= mLowerPosition && cam_y <= mUpperPosition) {
            mCamera.position.y += deltaY;
        }
        if (mCamera.position.y > mUpperPosition) mCamera.position.y = mUpperPosition;
        if (mCamera.position.y < mLowerPosition) mCamera.position.y = mLowerPosition;

        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        mVelocityY = 0;
        return true;
    }

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        mTimer = 0;
        return true;
    }

    public GestureDetector getGestureDetector() {
        return mGestureDetector;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        return false;
    }
}
public class CameraScroller extends InputAdapter implements GestureListener {
    public static final String TAG = "SCROLL_TAG";
    public static final float TIME_TO_SCROLL = 2.0f;

    private Camera mCamera;
    private final float mLowerPosition;
    private final float mUpperPosition;

    private GestureDetector mGestureDetector;
    private float mTimer;
    private float mVelocityY;

    /**
     * @param camera        {@link Camera} that have to be scrolled
     * @param upperPosition the upper limit position of camera
     * @param lowerPosition the lower limit position of camera
     */
    public CameraScroller(Camera camera, float lowerPosition, float upperPosition) {
        mUpperPosition = upperPosition;
        mLowerPosition = lowerPosition;
        mGestureDetector = new GestureDetector(this);
        mCamera = camera;
    }

    /**
     * Call in Screen.render()
     */
    public void act(float deltaTime) {
        if (mTimer > 0) {// if timer is not 0
            float acceleration_y = mVelocityY * deltaTime;// calculate acceleration (the rate of change of velocity)
            mTimer -= deltaTime;// decreasing timer
            mVelocityY -= acceleration_y;// decreasing velocity

          mCamera.position.y += mVelocityY;
          checkCameraPosition();// check if camera position is in not less or more than some value else stop camera (mTimer = 0)
        }
    }

    private void checkCameraPosition() {
        if (mCamera.position.y > mUpperPosition) {
            mCamera.position.y = mUpperPosition;
            mTimer = 0;
        }
        if (mCamera.position.y < mLowerPosition) {
            mCamera.position.y = mLowerPosition;
            mTimer = 0;
        }
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        mTimer = TIME_TO_SCROLL;
        mVelocityY = velocityY * Gdx.graphics.getDeltaTime();

        return true;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        float cam_y = mCamera.position.y;
        System.out.println();
        if (cam_y >= mLowerPosition && cam_y <= mUpperPosition) {
            mCamera.position.y += deltaY;
        }
        if (mCamera.position.y > mUpperPosition) mCamera.position.y = mUpperPosition;
        if (mCamera.position.y < mLowerPosition) mCamera.position.y = mLowerPosition;

        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        mVelocityY = 0;
        return true;
    }

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        mTimer = 0;
        return true;
    }

    public GestureDetector getGestureDetector() {
        return mGestureDetector;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        return false;
    }
}
Source Link

Yes, you understood me exactly. The question is not already actual. But thank you very much for your answer, this is another implementation of one idea. I did probably the same things, now I show my realization

public class CameraScroller extends InputAdapter implements GestureListener {
    public static final String TAG = "SCROLL_TAG";
    public static final float TIME_TO_SCROLL = 2.0f;

    private Camera mCamera;
    private final float mLowerPosition;
    private final float mUpperPosition;

    private GestureDetector mGestureDetector;
    private float mTimer;
    private float mVelocityY;

    /**
     * @param camera        {@link Camera} that have to be scrolled
     * @param upperPosition the upper limit position of camera
     * @param lowerPosition the lower limit position of camera
     */
    public CameraScroller(Camera camera, float lowerPosition, float upperPosition) {
        mUpperPosition = upperPosition;
        mLowerPosition = lowerPosition;
        mGestureDetector = new GestureDetector(this);
        mCamera = camera;
    }

    /**
     * Call in Screen.render()
     */
    public void act(float deltaTime) {
        if (mTimer > 0) {// if timer is not 0
            float acceleration_y = mVelocityY * deltaTime;// calculate acceleration (the rate of change of velocity)
            mTimer -= deltaTime;// decreasing timer
            mVelocityY -= acceleration_y;// decreasing velocity

            checkCameraPosition();// check if camera position is in not less or more than some value else stop camera (mTimer = 0)
        }
    }

    private void checkCameraPosition() {
        if (mCamera.position.y > mUpperPosition) {
            mCamera.position.y = mUpperPosition;
            mTimer = 0;
        }
        if (mCamera.position.y < mLowerPosition) {
            mCamera.position.y = mLowerPosition;
            mTimer = 0;
        }
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        mTimer = TIME_TO_SCROLL;
        mVelocityY = velocityY * Gdx.graphics.getDeltaTime();

        return true;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        float cam_y = mCamera.position.y;
        System.out.println();
        if (cam_y >= mLowerPosition && cam_y <= mUpperPosition) {
            mCamera.position.y += deltaY;
        }
        if (mCamera.position.y > mUpperPosition) mCamera.position.y = mUpperPosition;
        if (mCamera.position.y < mLowerPosition) mCamera.position.y = mLowerPosition;

        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        mVelocityY = 0;
        return true;
    }

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        mTimer = 0;
        return true;
    }

    public GestureDetector getGestureDetector() {
        return mGestureDetector;
    }

    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        return false;
    }
}