Game Development Reference
In-Depth Information
Using a rigid body (collider) for each
scene object
Now, we will modify our StageObject class again to create a bounding box. If we
revisit the constructor of our jigLib.JBox class, then its constructor takes the width,
depth, and height of the corresponding 3D model as follows:
var JBox=function(skin, width, depth, height)
When we add our 3D model to the stage, we do not know its height, width, and
depth. Hence, we first need to create its bounding box in order to compute the
preceding values.
A bounding box consists of the minimum and maximum values of the components
( x , y , z ) of the vertices of a mesh. Note that it is not the minimum and maximum
vertices but the minimum/maximum values of the components of a vertex. Open the
StageObject.js file from the primitive folder in your editor. The following code
snippet is present in this file:
StageObject.prototype.calculateBoundingBox=function (){
if(this.geometry.vertices.length>0){
var point=vec3.fromValues(this.geometry.vertices[0],
this.geometry.vertices[1],this.geometry.vertices[2]);
vec3.copy(this.min, point );
vec3.copy(this.max, point );
for ( var i = 3; i<this.geometry.vertices.length; i=i+3) {
point=vec3.fromValues(this.geometry.vertices[i],
this.geometry.vertices[i+1],this.geometry.vertices[i+2])
if ( point[0] <this.min[0] ) {
this.min[0] = point[0];
} else if ( point[0] >this.max[0] ) {
this.max[0] = point[0];
}
if ( point[1] <this.min[1] ) {
this.min[1] = point[1];
} else if ( point[1] >this.max[1] ) {
this.max[1] = point[1];
}
if ( point[2] <this.min[2] ) {
this.min[2] = point[2];
} else if ( point[2] >this.max[2] ) {
this.max[2] = point[2];
}
}
}
}
 
Search WWH ::




Custom Search