# death of the brain and matrices

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

## Recommended Posts

I have what I think is probably a simple question. I have a camera that can rotate 360 degrees around the Z axis and from 10 to 80 from the XY plane towards the Z axis. I'm using gluLookAt to position my camera. I'm trying to find the eyex,eyey, and eyez. My centerx, centery, centerz coords are always 0,0,0.

##### Share on other sites
Ok for gluLookAt:
The eye is the location of the camers, and the center is the position the camera would be looking at. I'm guessing you have the center/eye mixed up.

I assume your camera knows it's position and those 2 angles. Simply use trig on your viewing angles to find the viewing vector. Then simply add it onto the position coords to get the center points.

##### Share on other sites
My camera center and eye positions are correct. I want the center to always be 0,0,0. I'm rotating the camera around the center.

The trig is the braindead part. I'm having trouble working the vector out.

I can work it out on the XY plane, but throw the Z direction in there and I'm all messed up.

vector length = 1
angle around z axis = a
x = cos(a)
y = sin(a)

now what?

##### Share on other sites
you've got me confused...

knowing that the z axis is the one that points out from the screen then the x,y plane is the plane that would make up the front of the screen. So your camera needs to be able to "roll" around the z axis? If this is the case then its all easy...

gluLookAt(position.x, position.y, position.z, lookat.x, lookat.y, lookat.z, up.x, up.y, up.z);

gluLookAt(0,0,0, 0,0,-1, 0,1,0);

This positions the camera at 0,0,0 and sets it looking straight down the z axis.

Now, to get your roll angle around the z axis:

float theta = atan2f(y,x);

theta is in radians so we need to convert to degrees:

theta = theta * (180.0f/3.1415962f);

now that we have that we can do the roll:

glRotatef(theta,0,0,1);

Having rotated the matrix, we can now look at the problem again as completely new coordinate space so if you know the angle you want to pitch then you just do:

float angle = <insert your value here>; // make sure its in degrees

glRotatef(angle,1,0,0);

this will rotate you around the newly rotated x axis.

Once you have the camera set with these then you can draw all of the other stuff as normal, just enclose all of the drawing code for each other object in glPushMatrix()/glPopMatrix() pairs so that you dont chage the viewing rotations.

I hope this answers your questions, course mebbe I just really dont understand...

##### Share on other sites
Got it working. What I ended up doing was as simple as I thought. My brain finally began to work again.

The vector length is 1 so:
First I take the elevation, the angle from the XY plane towards the Z axis, and get X' from that by 1*cos(elevation) and the Z component by 1*sin(elevation). Then I take the degrees around the Z axis (azimuth) and get the Y component by X'*sin(azimuth) and the final X component by X'*cos(azimuth).

Z = 1*sin(elev)
X1 = 1*cos(elev)
Y = X1*sin(azim)
X = X1*cos(azim)

Don't know if that is the "correct" or fastest way of doing it, but it works and I don't have to worry about pushing and popping matrices.

##### Share on other sites
its all good as long as you get the desired picture, just realize that when talking to others you may get some folks confused....

Traditionally you start with your camera at 0,0,0 and looking down the negative z axis ie. 0,0,-1 and the positive y axis pointing straight up ie upvector = 0,0,1

With your starting point your camera position is at 1,0,0 which is one unit away from the origin on the x axis. Rather than looking down the negative z axis you decide to look down the negative x axis back at the origin and you are rotated around that x axis by 90deg counter clockwise so that your up vector points down the z axis.

Anyways, glad you got it working, hope the project turns out well...

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002139
×