Game Development Reference
In-Depth Information
// scale it
var shiftAmt =vec3.create();
vec3.scale(shiftAmt,this.dir, distance);
var renameMe =vec3.create();
vec3.subtract(renameMe,this.pos, this.orbitPoint);
var maxMoveCloser = vec3.length(renameMe) - this.
getClosestDistance();
if (vec3.length(shiftAmt) <= maxMoveCloser)
{
vec3.add(this.pos,this.pos, shiftAmt);
return true;
}
}
return false;
}
Similar to
goCloser
, the
goFarther
function sets the camera at a new radius
(distance) and position. What is different from
goCloser
is that in
goFarther
we first
multiply the direction vector with a negative scalar (
vec3.scale(negativeDist,
this.dir, -1)
), and then we multiply the new direction vector with the distance
scalar (
vec3.scale(shiftAmt,
negativeDist,
distance)
). Then, we calculate
the new position by adding the calculated vector (
shiftAmt
) to the position, after
checking whether the new position (
newpos
) exceeds the maximum distance. The
goFarther
function is defined as follows:
OrbitCamera.prototype.goFarther = function (distance)
{
if (distance > 0)
{
//
var shiftAmt = vec3.create();
var negativeDist=vec3.create();
vec3.scale(negativeDist,this.dir, -1);
vec3.scale(shiftAmt,negativeDist, distance);
var newpos =vec3.create();
vec3.add(newpos,this.pos, shiftAmt);
var distanceBetweenCamAndOP = vec3.distance(newpos, this.
orbitPoint);
if (distanceBetweenCamAndOP <= this.getFarthestDistance())
{
this.pos = newpos;
return true;
}
}
return false;
}