3
\$\begingroup\$

How can I detect the intensity of touch pressure in libGDX? For example, low pressure, medium, medium-high, high and so on. There are some questions about it but there aren't answers for libGDX.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

you can call

Gdx.input.getPressure()

This has been implemented in LibGDX here in July 2018 https://github.com/libgdx/libgdx/issues/5241

\$\endgroup\$
2
\$\begingroup\$

There's currently (update 1.9.6) no way to get the touch pressure internally from within LibGDX but using callbacks from the android MotionEvent we can access the touch pressure anyways.

The code might look something like this:

// Inside your AndroidLauncher's onCreate method
final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
final MainClass game = new MainClass();
final View view = initializeForView(game, config);
view.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(final View v, final MotionEvent event) {
        // The easiest option would probably be to have a 
        // (static?) variable in 'game' that stores pressure.
        game.pressure = event.getPressure();
        return false;
    }
});

This will register an OnTouchListener that will update the pressure variable each time the screen is touched. You can then use this pressure variable in your code after a touch event has happened to query the touch pressure.

This is the method I've used the (few) times I've needed touch pressure in LibGDX games and it seems to work more or less flawlessly.

\$\endgroup\$
5
  • \$\begingroup\$ OnTouch method is never called \$\endgroup\$
    – Curio
    Commented Jun 11, 2017 at 15:30
  • \$\begingroup\$ That's very strange... I just tested it and it works fine. Are you sure it's not being called or is it the variable that's not updating? Because some devices doesn't support touch pressure and Android will instead try to simulate touch pressure, which sometimes doesn't work. Android virtual devices also sometimes have trouble identifying pressure. \$\endgroup\$
    – Charanor
    Commented Jun 11, 2017 at 19:50
  • \$\begingroup\$ But with final View view = initializeForView(game, config); how can I know which view is touched? \$\endgroup\$
    – Curio
    Commented Jun 11, 2017 at 20:51
  • \$\begingroup\$ @Curio As far as I know there should only be one view active at all. \$\endgroup\$
    – Charanor
    Commented Jun 13, 2017 at 11:10
  • \$\begingroup\$ It doesn't work :(, is there another way? \$\endgroup\$
    – Curio
    Commented Jun 13, 2017 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .