Sign in to follow this  

Question on cameras

Recommended Posts

Hi, this is my first post here, so sorry about the real basic question...

I've googled around but could not find an exact answer for this.

I'm modeling a 3D app on android using openGL ES 1.1. I would like to have my camera to move just like a camera on a replay for instance (a look at camera) so I'm using glLookAt function.

Starting with the real basics I tried to get the rotation angle on the Y axis using the delta of the X pixels touched by the user. then I just reposition the camera on x,z (keeping y for now).

I'm reading the Beginning Android Games, and borrowed some of their classes (LookAtCamera, Vertices3 and Vector3)

I know I could be using Matrix and quartenions, but for now, just this simple example is not working, and I'd just would like to know why. Here's snippets of my code:

//Model is a rectangle with center on 0,0,0 and w=4,h=3

this.ratio = (float) width / height; = new LookAtCamera(67, ratio, 1, 10);, 6, 2);, 0, -1)

//my draw method:

gl.glViewport(0, 0, this.width, this.height);;

//setMatrices on LookAtCamera
public void setMatrices(GL10 gl) {
GLU.gluPerspective(gl, fieldOfView, aspectRatio, near, far);
GLU.gluLookAt(gl, position.x, position.y, position.z, lookAt.x, lookAt.y, lookAt.z, up.x, up.y, up.z);
//continue with the rendering of models

On the touchEvent of android api I'm updating the positions of the camera's eye. What I'm using here is just the projection of the eye vector on the x/z plane:

public void recomputePosition() {
// radX altera theta
// radY altera phi
// theta = angle in horizontal plane
// phi
if (Math.abs(mAngleX) >= 0.05f || Math.abs(mAngleY) >= 0.05) {
Log.d(TAG,"current camera: " + camera.getPosition());
Vector3 p = camera.getPosition();

float r = FloatMath.sqrt((p.x * p.x) + (p.z * p.z));
float theta = (float) Math.atan2(p.x, p.z);
float radX = mAngleX * DEG2RAD;

float newTheta = theta + radX;
float x = r * FloatMath.cos(newTheta);
float z = r * FloatMath.sin(newTheta);
camera.getPosition().x = x;
camera.getPosition().z = z;
String debug = "AngleX: " + mAngleX + " AngleY: " + mAngleY + " theta: " + theta + " position " + p;
Log.d(TAG, debug);

The code is "working" but, with a real odd effect, I see 2 poligons being draw (flickering) at the same time. And values for theta are ranging from positive to negative in alternance, it seems that I have some problems with angles and quadrants . Here's an output of the log:


current camera: [0.0,6.0,-2.0]
AngleX: 1.7584991 AngleY: 0.0 theta: 3.1415927 position [-1.9990581,6.0,-0.061373785]

current camera: [-1.9990581,6.0,-0.061373785]
AngleX: 2.0398564 AngleY: 0.0 theta: -1.601488 position [0.00982105,6.0,-1.9999759]

current camera: [0.00982105,6.0,-1.9999759]
AngleX: 2.321218 AngleY: 0.4221325 theta: 3.136682 position [-1.9987326,6.0,-0.07118932]


The odd thing here, is the lines on the middle. I was expecting theta to be 3.14 as my angle it's 0 after all on the start of moving of the finger, but as I move, the theta just changes to a negative value and then comes back to a closer value (3.13)

My first question would be, why is this happening, and the final one is: What would be the right way of implementing such camera?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this