Game Development Reference
In-Depth Information
Open
primitive/orbitcamera.js
in your favorite text editor. In the
implementation of the orbit camera, we have added three new parameters:
•
closestDistance
: This parameter defines the closest the camera can go to
the orbit point.
•
FarthestDistance
: This parameter defines the farthest distance the camera
can go away from the orbit point.
•
orbitPoint
: If you remember, in our free camera, we only used
lookAtPoint
to calculate the initial vector (up, dir, and left). In the case of the orbit camera,
we will need to store the orbit point and use it throughout to calculate all
the vectors.
OrbitCamera = inherit(Camera, function ()
{
superc(this);
// this value cannot be set to less than 0.
this.closestDistance = 0;
// typically larger than the closest distance, but set to
// 0 here since the user will likely overwrite it anyway.
// this value will always be greater or equal to the closest
// distance.
this.farthestDistance = 0;
// the point in space the camera will 'orbit'.
this.orbitPoint = vec3.fromValues(0, 0, 0);
});
The next function is
getDistance
; it returns the distance between the orbit point and
position, as shown in the following code snippet:
OrbitCamera.prototype.getDistance = function (){
return vec3.distance(this.pos, this.orbitPoint);
}
The
goCloser
function takes the
distance
parameter and moves the camera closer
to the orbit point. We move the camera towards the orbit point relative to the
position of the camera. The value of
vec3.scale(shiftAmt,this.dir,
distance)
is basically the value we get by multiplying the direction vector with the distance.
The camera will not move if we attempt to move it closer than the closest allowed
distance. A negative value for
goCloser
could be allowed and would mean moving
farther using a positive value, but this could create some confusion and is therefore
not permitted. The
goCloser
function is defined as follows:
OrbitCamera.prototype.goCloser = function (distance)
{
if (distance > 0)
{