API Docs for: 0.6.1
Show:

File: src/shapes/Sphere.js

  1. module.exports = Sphere;
  2.  
  3. var Shape = require('./Shape');
  4. var Vec3 = require('../math/Vec3');
  5.  
  6. /**
  7. * Spherical shape
  8. * @class Sphere
  9. * @constructor
  10. * @extends Shape
  11. * @param {Number} radius The radius of the sphere, a non-negative number.
  12. * @author schteppe / http://github.com/schteppe
  13. */
  14. function Sphere(radius){
  15. Shape.call(this);
  16.  
  17. /**
  18. * @property {Number} radius
  19. */
  20. this.radius = radius!==undefined ? Number(radius) : 1.0;
  21. this.type = Shape.types.SPHERE;
  22.  
  23. if(this.radius < 0){
  24. throw new Error('The sphere radius cannot be negative.');
  25. }
  26.  
  27. this.updateBoundingSphereRadius();
  28. }
  29. Sphere.prototype = new Shape();
  30. Sphere.prototype.constructor = Sphere;
  31.  
  32. Sphere.prototype.calculateLocalInertia = function(mass,target){
  33. target = target || new Vec3();
  34. var I = 2.0*mass*this.radius*this.radius/5.0;
  35. target.x = I;
  36. target.y = I;
  37. target.z = I;
  38. return target;
  39. };
  40.  
  41. Sphere.prototype.volume = function(){
  42. return 4.0 * Math.PI * this.radius / 3.0;
  43. };
  44.  
  45. Sphere.prototype.updateBoundingSphereRadius = function(){
  46. this.boundingSphereRadius = this.radius;
  47. };
  48.  
  49. Sphere.prototype.calculateWorldAABB = function(pos,quat,min,max){
  50. var r = this.radius;
  51. var axes = ['x','y','z'];
  52. for(var i=0; i<axes.length; i++){
  53. var ax = axes[i];
  54. min[ax] = pos[ax] - r;
  55. max[ax] = pos[ax] + r;
  56. }
  57. };
  58.