Coding the camera
This class is completely new to this project. The key to understanding it is to remember that all our game objects have sizes and positions in virtual meters. All the movement and collision detection will be done using these virtual meters. Here is the key-key point:
Tip
These virtual meters currently bare no relation to the pixels on the screen. The Camera
class will know the screen's resolution and translate these virtual meters into pixel coordinates. Not all game objects will be on the screen every frame. In fact, most game objects will not be visible, most frames.
Add a new class called Camera
and add the following member variables and the constructor.
import android.graphics.PointF; import android.graphics.Rect; class Camera { private PointF mCurrentCameraWorldCentre; private Rect mConvertedRect; private int mPixelsPerMetre; private int mScreenCentreX; private int mScreenCentreY; Camera(int screenXResolution, int screenYResolution){ ...