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
\$\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\$
  • \$\begingroup\$ OnTouch method is never called \$\endgroup\$ – Curio Jun 11 '17 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 Jun 11 '17 at 19:50
  • \$\begingroup\$ But with final View view = initializeForView(game, config); how can I know which view is touched? \$\endgroup\$ – Curio Jun 11 '17 at 20:51
  • \$\begingroup\$ @Curio As far as I know there should only be one view active at all. \$\endgroup\$ – Charanor Jun 13 '17 at 11:10
  • \$\begingroup\$ It doesn't work :(, is there another way? \$\endgroup\$ – Curio Jun 13 '17 at 15:24
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\$

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.