Game Development Reference
In-Depth Information
In the preceding code, we first store the first vertex in the min and max class variables
of the StageObject class. We then iterate over the list of vertices of our geometry,
compare each component ( x , y , z ) of a vertex with the min and max components, and
store the minimum and maximum values in the corresponding components. You
must have noticed that the min and max vectors are not actual vertices in the mesh,
but they just hold the minimum and maximum values of x , y , and z .
The width of the 3D model is calculated using the following formula. Here, max[0] is
the maximum value of x and min[0] is the minimum value of x :
Width = max[0] - min[0]
The height of the 3D model is calculated using the following formula. Here, max[1] is
the maximum value of y and min[1] is the minimum value of y :
Height = max[1] - min[1]
The depth of the 3D model is calculated using the following formula. Here, max[2] is
the maximum value of z and min[2] is the minimum value of z :
Depth = max[2] - min[2]
We create a collider which closely resembles the shape and real world physics of the
3D model. For example, JCapsule colliders are used for cylindrical objects which roll
in a direction. For example, we have a ball, so we would not want its collider to be a
box because a box collider would not roll. Hence, we also create a bounding sphere if
the object resembles a sphere. The constructor of jigLib.JSphere=function(skin,
r) takes the radius as an argument. Hence, we first need to compute its center to
compute its radius. In the following code, we first compute the bounding box to
calculate the center:
StageObject.prototype.calculateBoundingSphere=function (){
this.calculateBoundingBox();
this.center=this.calculateCenter();
var squaredRadius=this.radius*this.radius;
for ( var i = 0; i<this.geometry.vertices.length; i=i+3) {
var point=vec3.fromValues(this.geometry.vertices[i],
this.geometry.vertices[i+1],this.geometry.vertices[i+2]);
squaredRadius=Math.max(squaredRadius,vec3.squaredDistance
(this.center,point));
}
this.radius=Math.sqrt(squaredRadius);
}
StageObject.prototype.calculateCenter=function (){
var center=vec3.create();
 
Search WWH ::




Custom Search