Creating a viewport with SurfaceView and Canvas

Problems with Canvas, OpenGL, etc...

Creating a viewport with SurfaceView and Canvas

Postby kirilz » Mon Sep 27, 2010 12:33 am

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.
Once Poster
Once Poster
Posts: 1
Joined: Sun Sep 26, 2010 11:25 pm


Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest