Game Development Reference
In-Depth Information
Using an orbit camera
Open
05-Loading-Scene-OrbitCamera.html
in your favorite text editor. The only
change we have in this file is to replace the free camera object with the orbit camera
object. Also, set the basic properties for the orbit camera.
cam=new OrbitCamera();
cam.setFarthestDistance(300);
cam.setClosestDistance(60);
cam.setOrbitPoint([0.0, 20.0, 0.0]);
cam.setDistance(100);
We added some changes to
primitive/KeyboardInteractor.js
to handle the
orbit camera.
If the camera object is of the
OrbitCamera
type, then only two keys are functional,
the up and down arrow keys. We invoke the
goCloser
and
goFarther
functions of
the camera object.
if(this.cam instanceof OrbitCamera) {
switch(event.keyCode) {//determine the key pressed
case 65://a key
case 37://left arrow
break;
case 68://d key
case 39://right arrow
break;
case 83://s key
break;
case 40://down arrow
this.cam.goFarther(10);//move + on the Z axis
break;
case 38://up arrow
this.cam.goCloser(10);//move - along the Y axis (down)
break;
}
}
The changes in
primitive/MouseInteractor.js
are listed as follows:
MouseInteractor.prototype.translate = function(value) {
...
if(c instanceof OrbitCamera) {
if(dv>0) {
c.goFarther(dv);
}