API Docs for: 0.6.1
Show:

File: src/shapes/Box.js

  1. module.exports = Box;
  2.  
  3. var Shape = require('./Shape');
  4. var Vec3 = require('../math/Vec3');
  5. var ConvexPolyhedron = require('./ConvexPolyhedron');
  6.  
  7. /**
  8. * A 3d box shape.
  9. * @class Box
  10. * @constructor
  11. * @param {Vec3} halfExtents
  12. * @author schteppe
  13. * @extends Shape
  14. */
  15. function Box(halfExtents){
  16. Shape.call(this);
  17.  
  18. this.type = Shape.types.BOX;
  19.  
  20. /**
  21. * @property halfExtents
  22. * @type {Vec3}
  23. */
  24. this.halfExtents = halfExtents;
  25.  
  26. /**
  27. * Used by the contact generator to make contacts with other convex polyhedra for example
  28. * @property convexPolyhedronRepresentation
  29. * @type {ConvexPolyhedron}
  30. */
  31. this.convexPolyhedronRepresentation = null;
  32.  
  33. this.updateConvexPolyhedronRepresentation();
  34. this.updateBoundingSphereRadius();
  35. }
  36. Box.prototype = new Shape();
  37. Box.prototype.constructor = Box;
  38.  
  39. /**
  40. * Updates the local convex polyhedron representation used for some collisions.
  41. * @method updateConvexPolyhedronRepresentation
  42. */
  43. Box.prototype.updateConvexPolyhedronRepresentation = function(){
  44. var sx = this.halfExtents.x;
  45. var sy = this.halfExtents.y;
  46. var sz = this.halfExtents.z;
  47. var V = Vec3;
  48.  
  49. var vertices = [
  50. new V(-sx,-sy,-sz),
  51. new V( sx,-sy,-sz),
  52. new V( sx, sy,-sz),
  53. new V(-sx, sy,-sz),
  54. new V(-sx,-sy, sz),
  55. new V( sx,-sy, sz),
  56. new V( sx, sy, sz),
  57. new V(-sx, sy, sz)
  58. ];
  59.  
  60. var indices = [
  61. [3,2,1,0], // -z
  62. [4,5,6,7], // +z
  63. [5,4,0,1], // -y
  64. [2,3,7,6], // +y
  65. [0,4,7,3], // -x
  66. [1,2,6,5], // +x
  67. ];
  68.  
  69. var axes = [
  70. new V(0, 0, 1),
  71. new V(0, 1, 0),
  72. new V(1, 0, 0)
  73. ];
  74.  
  75. var h = new ConvexPolyhedron(vertices, indices);
  76. this.convexPolyhedronRepresentation = h;
  77. h.material = this.material;
  78. };
  79.  
  80. /**
  81. * @method calculateLocalInertia
  82. * @param {Number} mass
  83. * @param {Vec3} target
  84. * @return {Vec3}
  85. */
  86. Box.prototype.calculateLocalInertia = function(mass,target){
  87. target = target || new Vec3();
  88. Box.calculateInertia(this.halfExtents, mass, target);
  89. return target;
  90. };
  91.  
  92. Box.calculateInertia = function(halfExtents,mass,target){
  93. var e = halfExtents;
  94. target.x = 1.0 / 12.0 * mass * ( 2*e.y*2*e.y + 2*e.z*2*e.z );
  95. target.y = 1.0 / 12.0 * mass * ( 2*e.x*2*e.x + 2*e.z*2*e.z );
  96. target.z = 1.0 / 12.0 * mass * ( 2*e.y*2*e.y + 2*e.x*2*e.x );
  97. };
  98.  
  99. /**
  100. * Get the box 6 side normals
  101. * @method getSideNormals
  102. * @param {array} sixTargetVectors An array of 6 vectors, to store the resulting side normals in.
  103. * @param {Quaternion} quat Orientation to apply to the normal vectors. If not provided, the vectors will be in respect to the local frame.
  104. * @return {array}
  105. */
  106. Box.prototype.getSideNormals = function(sixTargetVectors,quat){
  107. var sides = sixTargetVectors;
  108. var ex = this.halfExtents;
  109. sides[0].set( ex.x, 0, 0);
  110. sides[1].set( 0, ex.y, 0);
  111. sides[2].set( 0, 0, ex.z);
  112. sides[3].set( -ex.x, 0, 0);
  113. sides[4].set( 0, -ex.y, 0);
  114. sides[5].set( 0, 0, -ex.z);
  115.  
  116. if(quat!==undefined){
  117. for(var i=0; i!==sides.length; i++){
  118. quat.vmult(sides[i],sides[i]);
  119. }
  120. }
  121.  
  122. return sides;
  123. };
  124.  
  125. Box.prototype.volume = function(){
  126. return 8.0 * this.halfExtents.x * this.halfExtents.y * this.halfExtents.z;
  127. };
  128.  
  129. Box.prototype.updateBoundingSphereRadius = function(){
  130. this.boundingSphereRadius = this.halfExtents.norm();
  131. };
  132.  
  133. var worldCornerTempPos = new Vec3();
  134. var worldCornerTempNeg = new Vec3();
  135. Box.prototype.forEachWorldCorner = function(pos,quat,callback){
  136.  
  137. var e = this.halfExtents;
  138. var corners = [[ e.x, e.y, e.z],
  139. [ -e.x, e.y, e.z],
  140. [ -e.x, -e.y, e.z],
  141. [ -e.x, -e.y, -e.z],
  142. [ e.x, -e.y, -e.z],
  143. [ e.x, e.y, -e.z],
  144. [ -e.x, e.y, -e.z],
  145. [ e.x, -e.y, e.z]];
  146. for(var i=0; i<corners.length; i++){
  147. worldCornerTempPos.set(corners[i][0],corners[i][1],corners[i][2]);
  148. quat.vmult(worldCornerTempPos,worldCornerTempPos);
  149. pos.vadd(worldCornerTempPos,worldCornerTempPos);
  150. callback(worldCornerTempPos.x,
  151. worldCornerTempPos.y,
  152. worldCornerTempPos.z);
  153. }
  154. };
  155.  
  156. var worldCornersTemp = [
  157. new Vec3(),
  158. new Vec3(),
  159. new Vec3(),
  160. new Vec3(),
  161. new Vec3(),
  162. new Vec3(),
  163. new Vec3(),
  164. new Vec3()
  165. ];
  166. Box.prototype.calculateWorldAABB = function(pos,quat,min,max){
  167.  
  168. var e = this.halfExtents;
  169. worldCornersTemp[0].set(e.x, e.y, e.z);
  170. worldCornersTemp[1].set(-e.x, e.y, e.z);
  171. worldCornersTemp[2].set(-e.x, -e.y, e.z);
  172. worldCornersTemp[3].set(-e.x, -e.y, -e.z);
  173. worldCornersTemp[4].set(e.x, -e.y, -e.z);
  174. worldCornersTemp[5].set(e.x, e.y, -e.z);
  175. worldCornersTemp[6].set(-e.x, e.y, -e.z);
  176. worldCornersTemp[7].set(e.x, -e.y, e.z);
  177.  
  178. var wc = worldCornersTemp[0];
  179. quat.vmult(wc, wc);
  180. pos.vadd(wc, wc);
  181. max.copy(wc);
  182. min.copy(wc);
  183. for(var i=1; i<8; i++){
  184. var wc = worldCornersTemp[i];
  185. quat.vmult(wc, wc);
  186. pos.vadd(wc, wc);
  187. var x = wc.x;
  188. var y = wc.y;
  189. var z = wc.z;
  190. if(x > max.x){
  191. max.x = x;
  192. }
  193. if(y > max.y){
  194. max.y = y;
  195. }
  196. if(z > max.z){
  197. max.z = z;
  198. }
  199.  
  200. if(x < min.x){
  201. min.x = x;
  202. }
  203. if(y < min.y){
  204. min.y = y;
  205. }
  206. if(z < min.z){
  207. min.z = z;
  208. }
  209. }
  210.  
  211. // Get each axis max
  212. // min.set(Infinity,Infinity,Infinity);
  213. // max.set(-Infinity,-Infinity,-Infinity);
  214. // this.forEachWorldCorner(pos,quat,function(x,y,z){
  215. // if(x > max.x){
  216. // max.x = x;
  217. // }
  218. // if(y > max.y){
  219. // max.y = y;
  220. // }
  221. // if(z > max.z){
  222. // max.z = z;
  223. // }
  224.  
  225. // if(x < min.x){
  226. // min.x = x;
  227. // }
  228. // if(y < min.y){
  229. // min.y = y;
  230. // }
  231. // if(z < min.z){
  232. // min.z = z;
  233. // }
  234. // });
  235. };
  236.