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

Adding keyboard and mouse interactions


Generally we move our characters using the mouse and keyboard, but in our case, we will rotate or move the camera instead. Keyboard interactions are easier to build compared to mouse interactions. In mouse interactions, we have to calculate the angle of rotation based on the mouse movement on the 2D screen, while in the case of the keyboard, we have to directly modify our camera position with each key press. When a key is pressed, we move the camera by multiplying pi with a constant factor (this.cam.roll(-Math.PI * 0.025)) for each step.

So, let's quickly look at the KeyBoardInteractor.js file present in the primitive directory:

function KeyBoardInteractor(camera, canvas){
  this.cam = camera;
  this.canvas = canvas;
  this.setUp();
}
KeyBoardInteractor.prototype.setUp=function(){
  var self=this;
  this.canvas.onkeydown = function(event) {
    self.handleKeys(event);
  }
}

The constructor of the KeyBoardInteractor class takes the canvas and camera objects...