Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying physics to Android View objects

I currently have an android application that utilises the native android views for the UI (e.g. imageviews/buttons etc)

Although this app is not a game, nor requires any heavy graphics or OpenGL, I would like to incorporate a little physics-related interaction. Nothing too significant but maybe display minor collisions/bounces, deceleration/acceleration or possibly friction.

Is this possible to simulate either within the android framework or using an external physics library like jbox2d without having to utilize an entire game engine (like andengine, libgx etc).

P.S. this is for API 15+

Thanks all.

like image 731
Kurian Vithayathil Avatar asked Feb 06 '14 16:02

Kurian Vithayathil


1 Answers

As long as you can set/update the position of the UI elements every frame (e.g. with View.setTranslationX()), you can do it.

Animating the UI by hand would mean keeping a mapping between the UI elements and the physical (e.g. jbox2d) bodies that correspond to them. Then you update the respective positions of the UI elements to what the simulated bodies have each frame (such as body.getPosition()).

In event-driven apps, something like a Timer object is useful for scheduling the physics updates.

like image 173
Jonny D Avatar answered Oct 20 '22 19:10

Jonny D