Hey all,
My camera operates in the Position,View,Up Vector manner; has a vector for position, a vector that it's looking at, and an up vector perpendicular to both vectors. To calculate the up vector, I use the cross product. I wrote my own Vector class today to help cement my understanding of vectors, and thought everything worked fine, until I tried to navigate in 3D with my camera implementation, which is limited to the x/z plane. The cross product as I initially wrote it gave an up vector that was perpendicular to the y and z planes, resulting in the world looking like it was turned on its side. Here's my original cross-product code (where vectors have an x, y, and z float, and calcMagnitude() merely uses pythagorean methods and stores the mag in a global variable):
void Vector::crossProduct(Vector v1, Vector v2)
{
x = (v1.y*v2.z + v2.y*v1.z)*1.0f-(v1.x*v2.z + v2.x*v1.z)*0.0f+(v1.x*v2.y + v2.x*v1.y)*0.0f;
y = (v1.y*v2.z + v2.y*v1.z)*0.0f-(v1.x*v2.z + v2.x*v1.z)*1.0f+(v1.x*v2.y + v2.x*v1.y)*0.0f;
z = (v1.y*v2.z + v2.y*v1.z)*0.0f-(v1.x*v2.z + v2.x*v1.z)*0.0f+(v1.x*v2.y + v2.x*v1.y)*1.0f;
calcMagnitude();
}
However, I've discovered through tinkering around that reversing the x and z assignments results in what appears to be a normal camera view, like so:
void Vector::crossProduct(Vector v1, Vector v2)
{
z = (v1.y*v2.z + v2.y*v1.z)*1.0f-(v1.x*v2.z + v2.x*v1.z)*0.0f+(v1.x*v2.y + v2.x*v1.y)*0.0f;
y = (v1.y*v2.z + v2.y*v1.z)*0.0f-(v1.x*v2.z + v2.x*v1.z)*1.0f+(v1.x*v2.y + v2.x*v1.y)*0.0f;
x = (v1.y*v2.z + v2.y*v1.z)*0.0f-(v1.x*v2.z + v2.x*v1.z)*0.0f+(v1.x*v2.y + v2.x*v1.y)*1.0f;
calcMagnitude();
}
Does anybody have any idea why this might be happening? Really sorry if it's something obvious, I've been at this for about 12 hours straight so far and am not thinking as clearly as I should be.
Thanks so much,
Aviosity