Hi Eric,
That's pretty much spot on with what i'm trying to achieve.
Below is the revised code I have so far. The lookAt method seems to be working fine and I can orient my camera to display the world correctly from it's perspective. What I want to do now is be able to rotate the camera around a vector by calling a method similar to rotateZ etc etc.
//create matrix to orient camera towards current look at vector- (void)lookAt{ //calculate matrix row z SMVector3D vector_z = SMVector3DMake((position.x - lookAt.x), (position.y - lookAt.y), (position.z - lookAt.z)); SMVector3DNormalize(&vector_z); //set matrix row y SMVector3D vector_y = SMVector3DMake(axis.x, axis.y, axis.z); //calculate marix row x SMVector3D vector_x = SMVector3DCrossProduct(vector_y, vector_z); //recalculate matrix row y vector_y = SMVector3DCrossProduct(vector_z, vector_x); //normalise matrix rows x and y SMVector3DNormalize(&vector_x); SMVector3DNormalize(&vector_y); //inverse dot product of camera position SMVector3D translation = SMVector3DMake(-SMVector3DDotProduct(vector_x, position), -SMVector3DDotProduct(vector_y, position), -SMVector3DDotProduct(vector_z, position)); //set identity matrix SMMatrix4DSetIdentity(&matrix); //create matrix from vectors matrix = SMMatrix4DMakeWithVectors(vector_x, vector_y, vector_z, translation); //transpose matrix SMMatrix4DTranspose(&matrix); //multiply modelview matrix glMultMatrixf(matrix.data); //translate to camera position glTranslatef(-position.x, -position.y, -position.z);}
//rotate around a point along the y axis- (void)rotateZ:(GLfloat)degrees aroundVector:(SMVector3D)vector{ //determine relative distance of spheres GLfloat distance = SMVector3DRelativeDistance(position, vector); //calculate desired angle in radians GLfloat desiredAngle = DEGREES_TO_RADIANS(degrees); //determine current angle in radians GLfloat currentAngle = atan2f(position.y, position.x); //accumulate current angle desiredAngle += currentAngle; //calculate and set new camera position position.x = vector.x + (distance * cosf(desiredAngle)); position.y = vector.y + (distance * sinf(desiredAngle));}
I think i've been having trouble with the rotate methods as i'm guessing it's not really possible to rotate the camera around the Z axis. Currently any call to the method just renders everything offscreen (I assume) so i've not gotten anywhere with it at all.
I can understand the application of a spherical coordinate system as all angles of traversal along the surface of a sphere using latitude and longitude. What i'm struggling to understand is how I can determine the current angles and increment/decrement them to actually orbit around a vector :/