Archived

This topic is now archived and is closed to further replies.

jeffersonalegria

Total 3D Navigation/Camera

Recommended Posts

It isn''t too complex, if you have a fair knowledge of basic vector math. A rotation matrix can be interpreted as an orientation matrix. If this orientation matrix held a cameras orientation, it could be encoded as a 3x3 matrix like this:
[u]
[v]
[n]
where u, v, and n are 3-vectors (x-y-z). If the matrix where held at the origin having not moved and pointing in the "default" direction in your 3d space, it would basically be the i, j, and k vectors, <1,0,0>, <0,1,0>, and <0,0,1>, respectively. If you rotate this camera, you''re basically rotating the i-j-k axes into a new orientation. The knew orientation is encoded in the u, v, and n vectors, where u is right, v is up, and n is forward. Sometimes, you can automatically generate an oriention for a camera given an up vector and a target to look at. This is how the look-at function is thusly named. Basically, there are two vector operations involved, subtraction and the cross product. Vector subtraction yields a vector pointing in the direction toward the first operand, from the second, reading left-to-right.
a-b = (a0-b0, a1-b1, a2-b2)
and geometrically:
a<--------b
The cross product returns a vector that is perpendicular to the two vectors crossed (mathmatically denoted axb or a^b). This is useful for defining a plane given the three vertices of a triangle or for making your camera look at something like you''re doing. The definition of cross product you''ll have to teach yourself. But given the above abstract definitions, you should be able to understand the operations involved in the following piece of code:

//returns camera matrix

//note that you''ll also need the position point of your camera

//which is not part of the 3x3 matrix but part of the same struct

matrix3 look_at(vector3 &up, vector3 &cam_pos, vector3 &target)
{
vector3 forward = target - cam_pos; //you defined overloaded

//operator -

vector3 right = forward ^ up; //you defined overloaded

//operator ^ (cross).

//^ is not commutative, so

//up ^ forward would return

//"left" vector.

//redefine up vector since it may or may not be perpendicular

//to found forward vector:

up = right ^ forward;

//there are many places that you can choose to normalize your

//uvn vectors at. Right now would work. Note that this func

//inputs an up vector that''s not necessarily of unit length

//like j (<0,1,0>) is. Unit length vectors are required for

//some operations like rotating objects by multiplying their

//their vertices by this camera orientation. This is where

//judgement based on knowledge of vector math comes into play.


#ifdef NORMALIZE_AFTER_MATH //your choice

normalize(up);
normalize(right);
normalize(forward);
#endif

//return temporary object of look-at matrix!

return matrix3(right, up, forward); //you defined matrix3

}

I don''t know if that code works, lol

edit: damn source tags

Share this post


Link to post
Share on other sites