Game Development Reference
In-Depth Information
place the camera.
*/
FreeCamera.prototype.setPosition =function (newVec)
{
this.pos=vec3.fromValues(newVec[0],newVec[1],newVec[2])
}
The setLookAtPoint function uses the position vector and uses point ( newVec )
values to calculate the up, left, and direction vectors. The code starts with a check
to see whether the position or lookAtPoint is at the world origin. If it is, it should
not do anything, but simply use the values for up [0, 1, 0], left [1, 0, 0], and dir [0, 0,
1] from the parent constructor. If any of the values are different, then calculate the
direction vector by subtracting the value of the position from the look at point vector
( vec3.subtract(this.dir, newVec, this.pos) ) and normalizing the direction
vector ( vec3.normalize(this.dir, this.dir) ) to a unit vector.
Then, we adjust our left and up vectors. We calculate the left vector assuming that
the up vector is [0,1,0] and compute the cross product between the direction and
up vectors ( vec3.cross(this.left, vec3.fromValues(0, 1, 0), this.dir
) ). Then, we calculate the up vector by computing the cross product of the left and
direction vectors ( vec3.cross(this.up, this.dir, this.left) ).
All the mathematics involved is based on the fact that all the three vectors are
perpendicular to each other, that is, they are orthogonal. If we have two vectors,
we can calculate the third by calculating the cross product of the two vectors. The
cross product yields a vector that is perpendicular to the original two vectors. The
setLookAtPoint function is defined as follows:
FreeCamera.prototype.setLookAtPoint =function (newVec)
{
// if the position hasn't yet been changed and they want the
// camera to look at [0,0,0], that will create a problem.
if (isVectorEqual(this.pos, [0, 0, 0]) && isVectorEqual(newVec, [0,
0, 0]))
{
}
else
{
// Figure out the direction of the point we are looking at.
vec3.subtract(this.dir, newVec, this.pos);
vec3.normalize(this.dir, this.dir);
// Adjust the Up and Left vectors accordingly
vec3.cross(this.left, vec3.fromValues(0, 1, 0), this.dir );
vec3.normalize(this.left, this.left);
vec3.cross(this.up, this.dir, this.left);
vec3.normalize(this.up, this.up);
}
}
 
Search WWH ::




Custom Search