Game Development Reference
In-Depth Information
The
JTerrain
object takes the
ITerrain
interface's object as a parameter. The
ITerrain
interface's object holds data such as height, width, width segments, and
height segments. We will discuss this object in detail in the section
Extending our
terrain with physics
.
The
JTriangleMesh
object is another exciting object of the rigid body. It has the
createMesh(this.vertices, this.indices)
function that creates any geometry
in the physics system. The vertices and indices define the shape of the 3D geometry.
They are absolutely similar to our vertices and indices data. Each element of the
vertices
array represents the vertex (
x
,
y
,
z
). Each triangle [a, b, c] of the mesh is
stored as an element of the
indices
array. This object is computationally expensive
to use as we have to check each polygon of the mesh for collisions while in other
cases, we use a bounding box. We generally avoid using it.
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));
this.system.addBody(this.rigidBody);
}