Cross Product woes

Started by
15 comments, last by someusername 18 years, 1 month ago
Hi guys, I'm using OPenGL to make a small modelling program. I want to translate my objects relative to my camera. I can get my objects to translate towards & away from my camera. I get the vectors of my object position & camera position, minus them, then normalise the result. Translating to & from the camera is the same as translating up & down the cameras relative Z-axis. But I am having trouble with translating my objects relative to my camera's x-axis. To find the camera's x-axis, I am using the code:

SceneVector3 cameraOrthogonalVector = cameraPositionVector.CrossProduct(cameraLookAtVector);
cameraOrthogonalVector.Normalise();

where

SceneVector3 SceneVector3::CrossProduct(const SceneVector3& vector) const
{
	return SceneVector3(	y*vector.z - z*vector.y,
				z*vector.x - x*vector.z,
				x*vector.y - y*vector.x );
}

But this is not giving the correct results. Say if my object is at position 0,0,0 & my camera is at 0,0,-20 my object won't translate at all. Can some1 help me here? Thanks for any help given!
Reject the basic asumption of civialisation especially the importance of material possessions
Advertisement
One potential problem here is that if your camera is looking directly at the object then the cross product you describe will always give the zero vector. Another, although this may not be such a problem, is that the cameras x-axis as you've defined it will change as the object position changes.
Yeah the camera's x-axis will change, I might change that when I get the code working and test what the functionality is like with the current code, that will be easy, I can just use the vector that the camera looks at.

Do you have any ideas how I can compensate for this zero vector?
Reject the basic asumption of civialisation especially the importance of material possessions
Just curious, why not just translate them independant from camera position/view direction?

Because Im making a modelling program. I want the same king of translation you would get in 3d studio max etc.
Reject the basic asumption of civialisation especially the importance of material possessions
Quote:Original post by Cacks
Yeah the camera's x-axis will change, I might change that when I get the code working and test what the functionality is like with the current code, that will be easy, I can just use the vector that the camera looks at.

Do you have any ideas how I can compensate for this zero vector?


When this happens, you need to choose an x-vector, as cameraLookAtVector and objectPositionVector - cameraPositionVector no longer define a plane that you can use to orient yourself.

In the absence of any other choice, and writing cameraPositionVector = (a,b,c), you could just take cameraOrthogonalVector = (-b,a,0)
What do you mean by x-vector?
Reject the basic asumption of civialisation especially the importance of material possessions
I'm not sure if this is going to help your overall problem, but it looks like your going about getting the cameras x-axis all wrong.
The function gluLookAt takes 2 points and an up vector as its arguments. With that information it sets up your MODELVIEW matrix with the cameras x, y, and z axis vectors and the cameras position, which is the first parameter of gluLookAt. To find the x, y, and z axis of the camera gluLookAt does something similar to the following.

Find the cameras z-axis vector, this is easy since it is just (camera_position - lookat_position).

Next it gets the x-axis vector, which is what you said you needed, it takes the cross product of the up vector(3rd argument to gluLookAt) and the new z-axis.

Last it takes the cross product of the z-axis vector and the x-axis vector to find the new y-axis.

Thats basically it, the up vector is usually something simple like 0, 1, 0, as in straight up. You want to make sure the z-axis vector you compute from the eye and lookat positions does not equal your up vector or your cross products won't work right.

You want to make sure your taking the cross product of vectors and not points and also the order in which you do the cross product affects the direction of the cross product. As in it has the opposite direction that it would have if you took the cross product with the 2 vectors reversed. Thats why I said gluLookAt does something similar to what I described.

Hope this helps.
Extract the first 3 column vectors from your view matrix. Ignore the 4th component. These are the camera's axes (as unit directions) in global coordinates.
Quote:Original post by someusername
Extract the first 3 column vectors from your view matrix.
Other issues aside, wouldn't it be the first three rows, rather than columns? (Since the OP is using OpenGL...)

This topic is closed to new replies.

Advertisement