This is sort of a beginner's question.
I am building an arcade flying game in top-down perspective with SurfaceView and Canvas. I ran into a problem trying to set up the viewport (game camera). The idea is that it is always centered on the player character (which is always in the center bottom of the screen) and rotates when the player's plane starts turning in order to maintain a "behind the tail" perspective on the plane.
I have tried using canvas.translate() and canvas.rotate() but I can't seem to get a hold of the logic.
My attempts looked somewhat like this
- Code: Select all
canvas.rotate(orientation, PLANE_X, PLANE_Y); // orientation is the angle to which the canvas should be rotated and the other two variables set up the plane's position as the pivot point
canvas.translate(px, py); //px and py were supposed to be the new position where the camera must scroll
drawPlane(canvas, PLANE_X, PLANE_Y);
The idea is that since the translation and rotation happen before I draw the player's plane, everything else except it should change position on the screen. In reality, it turned out exactly the opposite. I managed to partially fix that by switching places of the drawPlane and drawTerrain/drawNPC methods, but in this case other problems emerged. Apparently the whole axis of the canvas is rotated and the plane starts moving sideways depending on the angle.
Any help is greatly appreciated. Thanks in advance.
I am aware that using Open GL is going to be faster, but since I am relatively inexperienced in working with Java and Android I would prefer to keep that off the table.