O'Reilly logo

WebGL Game Development by Sumeet Arora

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding gravity and a rigid body to the game scene

Well, it is time to add some physics to our game scene. In the example, we will simply load a JSON sphere and add it to the scene. The 3D object's motion will be controlled by the physics engine and gravity.

Open the Sphere.js file from primitive/game in your favorite editor. The sphere object inherits the StageObject class and has a new function, initializePhysics; we have also overridden the update function. The initializePhysics function is given in the following code snippet:

Sphere= inherit(StageObject, function (){ superc(this); this.visible=false; }); Sphere.prototype.initializePhysics=function(){ var sphere = new jigLib.JSphere(null, 20); sphere.set_mass(50); this.rigidBody=sphere; this.rigidBody.moveTo(jigLib.Vector3DUtil.create(0,100,120)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required