Game Development Reference
In-Depth Information
We will calculate the up, left, and direction vectors using the preceding setting values.
Also, we have added two more properties to the FreeCamera class: the angular
and linear velocities. Although we will not use these parameters in our present
implementation of the camera class, they will be very useful when we discuss
animations in Chapter 6 , Applying Textures and Simple Animations to Our Scene .
Implementing the free camera
Open freecamera.js in your favorite text editor. The first step is to inherit the
Camera class in FreeCamera so that it gets access to basic functions, such as apply,
and all the properties such as up, left, direction, position, and FOV that are important
for any type of camera.
FreeCamera = inherit(Camera, function ()
{
superc(this);
// Delta Values for Animations
this.linVel = vec3.fromValues(0.0, 0.0, 0.0); // Animation of
positions
this.angVel = vec3.fromValues(0.0, 0.0, 0.0); // Animations of
rotation around (side Vector, up Vector, dir Vector)
});
The preceding code uses the inherit function defined in the utils.js file present
in the primitive directory. The inherit function uses JavaScript prototyping for
inheritance. Let's touch upon the inheritance of the Camera class quickly. Open
utils.js in your favorite text editor. In the file, you will find two functions:
inherit and superc . The inherit function takes the name of the parent class and
the constructor of the child class as parameters and uses prototyping for inheritance.
JavaScript is a prototype-based language, which means that every object can be
a prototype of another object. The process forms a prototype chain, which is a
linked list of objects, from which an object inherits the properties and methods
of its parent object.
A nonstandard magical property, __proto__ , is a mimic of the internal Prototype
property. We can modify __proto__ to dynamically change the prototype chain of
an existing object. See the following figure to understand the hierarchy:
Prototype
Prototype
Prototype
Object.Prototype
FreeCamera
Camera
Null
 
Search WWH ::




Custom Search