Determine current angle of rotation around vector

Started by
27 comments, last by CaptainRedmuff 13 years, 9 months ago
Quote:Original post by CaptainRedmuff
I can create a matrix or quaternion, but how do I derive the values for the updated camera position from either data structure?


The matrix or the quaternion are the only representation you need of the camera position. You don't need to convert it to angles at all.

Think of what operations you want to perform on the camera position. Is there anything you can't do with the matrix or quaternion representation?

Advertisement
Is there any chance you can give me an example of what you mean, please? :)

I'm not sure i comprehend how I can use a matrix and/or quaternion to store the position of the camera?

I thought I was getting somewhere but it seems my implementation is wrong. I only want to be able to point the camera towards a vector and have it create the matrix. Rotating around a vector would be the end goal but for the time being, if you can give me an example of how I can extract the position vector and orientation from the appropriate structures that would be most appreciated.

Thanks for reading!
Quote:I'm not sure i comprehend how I can use a matrix and/or quaternion to store the position of the camera?
Typically you would not store the position using a quaternion. (In most practical applications in games, graphics, etc., quaternions are used exclusively to represent orientations.)

You can store the position in a 4x4 (or 4x3 or 3x4) transform matrix, but for representing the orientation alone only a 3x3 matrix is required.

Typical representations for a 'camera object' include a 4x4 (or 4x3 or 3x4) transform matrix, a 3x3 matrix and a position vector, a quaternion and a position vector, three Euler angles and a position vector, or two Euler angles (or spherical angles, e.g. azimuth/elevation) and a position vector. All of these representations are viable, so which to use just depends on your requirements.

I haven't read the entire thread, but based on what I have read, it sounds like maybe it's an orbital camera of some sort that you're after. If so, a pair of spherical angles and a position vector would probably be a reasonable representation.

Note also that regardless of what representation you choose, it will probably need to be converted to a 4x4 matrix at some point (assuming you're working with a rendering API such as OpenGL or Direct3D).
Hi jyk, thanks for you reply.

I had a good coding session last night and managed to fix the issues with my camera not orienting itself properly. I wasn't setting up the matrix properly but i've now managed to fix this by setting the 4th column of the matrix to be the inverse dot product of the eye vector. This orients my camera properly around the lookAt vector, so i'm pleased that works at least.

I've had a chat with some friends and co-workers but there doesn't seem to be a practical example of what it is I want to achieve. I was hoping to find an image or youtube video of something similar but haven't come across anything so far. Many people have pointed out that I need to use a spherical coordinate system but I'm not convinced this will yield the correct results.

As far as I can tell, a spherical coordinate system rotates around the surface of a sphere with the camera always pointing inwards towards the spheres center. In essence, the rotation is correct, but I don't want to the camera to be pointing inwards all the time. It would be handy to rotate around the surface of the sphere, but be able to look outwards instead.

I'm thinking of a few ideas to achieve what I want but am currently stumbling with the camera orientation. I'll post my code when I get home tonight and see if anyone can shed some light for me. I know i'm getting close so if I can just make it past this last hurdle i'll be elated beyond all belief.

Thanks to everyone for your responses so far and thanks for reading!
I re-read the thread, but wasn't able to understand what it is exactly that you're trying to do. However:
Quote:As far as I can tell, a spherical coordinate system rotates around the surface of a sphere with the camera always pointing inwards towards the spheres center.
No, use of spherical coordinates to position the camera implies nothing about the camera's orientation; you can use spherical coordinates to move the camera around the surface of a sphere, while still having the camera 'look at' points other than the sphere center.
Hey Cap'n

I would avoid using sphereical coordinates to represent the position of the camera on the surface of a sphere. Rather, store the center of the sphere, and the current sphere offset vector. If the magnitude of the offset vector is fixed, then the position of the camera is always on the sphere




p is the vector representing the world space position of the camera, c is the vector representing the world space center of the sphere, and o is the vector representing the sphere space position of the camera.

This representation is especially simple if you want to have a smooth blend from the current sphere space position of the camera o to a new desired location od.

The blend is done in two steps. The first step is:



Where b is between 0 and 1 and determines the blend rate. If b is 0 then there is no blend. If b is 1 there is an instantaneous blend. The second step is:



Which constrains the blended position to lie on the surface of the sphere. This blend is non-linear, but it is smooth. Also, changing the destination point before the blend is complete does not cause problems.

Store this blended value as the new offset vector, and recalculate the world space position of the camera. Then find the lookat vector and so forth.

Eric Brown
Hi Eric,

Thank you for your response.

Just a few questions, for now... ;)

Quote:Original post by Eric_Brown
I would avoid using spherical coordinates to represent the position of the camera on the surface of a sphere. Rather, store the center of the sphere, and the current sphere offset vector. If the magnitude of the offset vector is fixed, then the position of the camera is always on the sphere


What do you mean by 'current sphere offset vector'? I currently have a vector for the center of the sphere and the camera's position vector, from which I can determine the radius via the magnitude of the two vectors. Would this be enough to suffice to allow me to determine the angle of rotation around the sphere along all three axis?

Quote:Original post by Eric_Brown
p is the vector representing the world space position of the camera, c is the vector representing the world space center of the sphere, and o is the vector representing the sphere space position of the camera.


Also, how do I determine O in your equation for the 'sphere space' position of the camera? Is this simply the camera position vector in relation to the sphere, rather than world coordinates?

Perhaps you could post a small example, in code, rather than formulaic representation?

I forgot to post my code on here last night but shall try to remember when I get home tonight so at least you can see what i'm working from currently.

I appreciate your response, i'm struggling with this one :/
Captain,

Yes, o is the position of the camera relative to the center of the sphere. When I say sphere space, I mean that the center of the sphere is the origin of this space, and the offset vector o is given relative to this origin.

I would like to try and verify that I know what it is you are trying to do:

From what I can tell, you are storing p and c, and using them to calculate o. You want to represent the location of the camera on the sphere by using a set of angles. You then use these angles to rotate o. Then you want to recalculate p using the rotated o. Is this correct?
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 :/

This topic is closed to new replies.

Advertisement