1
\$\begingroup\$

I'm currently working on a top-down twin-stick shooter for Android. I've got my player moving around and shooting, but I'm looking to add a large Bitmap background which is larger than the device screen.

I'm looking for a way to position the background at 0,0 in 'world' co-ordinates, and have the view able to pan across the background with the player. I've tried using SurfaceHolder.Callbacks, but these do not seem to play nicely with my SurfaceView class. The view currently follows the player while keeping it centred, until it reaches an edge, where it will stop and the player will continue until he hits a wall.

My current thoughts are to store an offset from view co-ordinates to world co-ordinates in the update loop, and draw the bitmap using these offsets in the draw loop. Is there a more efficient way to do this, or a different way to think about it?

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

Moving the View is probably not going to work out well -- the Surface part of the SurfaceView is managed by the Window Manager, not the View hierarchy, so updates tend to lag behind (which is why putting a SurfaceView in a ListView works badly). You don't want to be hitting callbacks in your animation loop.

Leave the View and Surface in place and just render the background bitmap with a translation matrix.

\$\endgroup\$
0
\$\begingroup\$

I think you need to implement a viewport. Viewport can simply be thought of as a camera which pans around the game world with keeping it's center as the player's position (or, whatever you program it to follow). Here's a good tutorial on it, hope it helps
http://gamecodeschool.com/android/building-a-simple-android-2d-scrolling-shooter/.

\$\endgroup\$
  • \$\begingroup\$ A quick note that this answer may be relevant only 2d programming, and only in that specific context, as the "viewport" concept in 3d/OpenGL is not exactly that. \$\endgroup\$ – Vaillancourt Aug 21 '16 at 13:11
  • \$\begingroup\$ From the question it seems to be a 2D game :) "...at 0,0 world coordinate..." \$\endgroup\$ – Rishabh Gupta Aug 23 '16 at 3:51

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.