Game Development Reference
In-Depth Information
vec3.add(center,this.min,this.max);
vec3.scale(center,center,0.5);
return center;
}
Then, we compute the center of the sphere. The computation of the center is simple:
center = (min + max)/2
The bounding box gives us the
min
and
max
values, and we calculate the sphere's
center from it. Then, we iterate over the list of vertices to find the distance of each
vertex from the center and store the maximum radius using the
squaredRadius=Math.
max(squaredRadius, vec3.squaredDistance(this.center, point));
statement.
Now, as our code to compute our bounding box is complete, we now need to
initialize our rigid body using the
initializePhysics
function as follows:
StageObject.prototype.initializePhysics=function(sphere){
if(sphere){
this.calculateBoundingSphere();
this.rigidBody= new jigLib.JSphere(null,this.radius);
this.rigidBody.set_mass(this.radius*this.radius*this.radius);
}else{
this.calculateBoundingBox();
var subVector=vec3.create();
vec3.sub(subVector,this.max,this.min);
this.rigidBody=new jigLib.JBox(null,Math.abs(subVector[0]),
Math.abs(subVector[2]),Math.abs(subVector[1]));
this.rigidBody.set_mass(Math.abs(subVector[0])
*Math.abs(subVector[2])*Math.abs(subVector[1]));
}
this.rigidBody.moveTo(jigLib.Vector3DUtil.
create(this.position[0],this.position[1],this.position[2]));//
Move the object to the location of the object.
var matrix=mat4.create();
mat4.fromQuat(matrix,this.quaternion);
var orient=new jigLib.Matrix3D(matrix);
this.rigidBody.setOrientation(orient);
//Change orientation of the rigid body
this.rigidBody.set_movable(false);
this.system.addBody(this.rigidBody);
}