Jump to content
  • Advertisement
Sign in to follow this  

Question on cameras

This topic is 2611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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;
this.camera = new LookAtCamera(67, ratio, 1, 10);
this.camera.getPosition().set(0, 6, 2);
this.camera.getLookAt().set(0, 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!