# Cross Product woes

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

## Recommended Posts

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!

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Just curious, why not just translate them independant from camera position/view direction?

##### Share on other sites
Because Im making a modelling program. I want the same king of translation you would get in 3d studio max etc.

##### Share on other sites
Quote:
 Original post by CacksYeah 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)

##### Share on other sites
What do you mean by x-vector?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by someusernameExtract 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...)

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

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

• Total Topics
631766
• Total Posts
3002232
×