Book Image

WebGL Game Development

By : Sumeet Arora
Book Image

WebGL Game Development

By: Sumeet Arora

Overview of this book

<p>WebGL, the web implementation of Open GL, is a JavaScript API used to render interactive 3D graphics within any compatible web browser, without the need for plugins. It helps you create detailed, high-quality graphical 3D objects easily. WebGL elements can be mixed with other HTML elements and composites to create high-quality, interactive, creative, innovative graphical 3D objects.</p> <p>This book begins with collecting coins in Super Mario, killing soldiers in Contra, and then quickly evolves to working out strategies in World of Warcraft. You will be guided through creating animated characters, image processing, and adding effects as part of the web page canvas to the 2D/3D graphics. Pour life into your gaming characters and learn how to create special effects seen in the most powerful 3D games. Each chapter begins by showing you the underlying mathematics and its programmatic implementation, ending with the creation of a complete game scene to build a wonderful virtual world.</p>
Table of Contents (17 chapters)
WebGL Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing the ModelSprite class


This class inherits the Sprite class. The objective of the class is twofold:

  • Set the position of the sprite at a delta distance from the model so that it moves with the model

  • Set the orientation of the sprite to face the camera

Open the ModelSprite.js file from client/primitive/game in your editor. The constructor takes two parameters: the model object (the sprite has to be associated with) and the camera object:

ModelSprite= inherit(Sprite, function (model,cam){
  superc(this);
  this.model=model;
  this.camera=cam;
  this.scale=vec3.fromValues(8,8,8);
  //The relative values to the model position

The delta distance from the model is initialized to zero as shown in the following code snippet:

  this.deltaX=0;
  this.deltaY=0;
  this.deltaZ=0;
});

We have overridden the update function of the StageObject class:

ModelSprite.prototype.update=function(){

We have discussed on numerous occasions that the model matrix is the inverse of the camera matrix. Hence, we set...