API Docs for: GoblinPhysics
Show:

File: src\classes\AABB.js

  1. /**
  2. * @class AABB
  3. * @param [min] {vec3}
  4. * @param [max] {vec3}
  5. * @constructor
  6. */
  7. Goblin.AABB = function( min, max ) {
  8. /**
  9. * @property min
  10. * @type {vec3}
  11. */
  12. this.min = min || new Goblin.Vector3();
  13.  
  14. /**
  15. * @property max
  16. * @type {vec3}
  17. */
  18. this.max = max || new Goblin.Vector3();
  19. };
  20.  
  21. Goblin.AABB.prototype.transform = (function(){
  22. var local_half_extents = new Goblin.Vector3(),
  23. local_center = new Goblin.Vector3(),
  24. center = new Goblin.Vector3(),
  25. extents = new Goblin.Vector3(),
  26. abs = new Goblin.Matrix3();
  27.  
  28. return function( local_aabb, matrix ) {
  29. local_half_extents.subtractVectors( local_aabb.max, local_aabb.min );
  30. local_half_extents.scale( 0.5 );
  31.  
  32. local_center.addVectors( local_aabb.max, local_aabb.min );
  33. local_center.scale( 0.5 );
  34.  
  35. matrix.transformVector3Into( local_center, center );
  36.  
  37. // Extract the absolute rotation matrix
  38. abs.e00 = Math.abs( matrix.e00 );
  39. abs.e01 = Math.abs( matrix.e01 );
  40. abs.e02 = Math.abs( matrix.e02 );
  41. abs.e10 = Math.abs( matrix.e10 );
  42. abs.e11 = Math.abs( matrix.e11 );
  43. abs.e12 = Math.abs( matrix.e12 );
  44. abs.e20 = Math.abs( matrix.e20 );
  45. abs.e21 = Math.abs( matrix.e21 );
  46. abs.e22 = Math.abs( matrix.e22 );
  47.  
  48. _tmp_vec3_1.x = abs.e00;
  49. _tmp_vec3_1.y = abs.e10;
  50. _tmp_vec3_1.z = abs.e20;
  51. extents.x = local_half_extents.dot( _tmp_vec3_1 );
  52.  
  53. _tmp_vec3_1.x = abs.e01;
  54. _tmp_vec3_1.y = abs.e11;
  55. _tmp_vec3_1.z = abs.e21;
  56. extents.y = local_half_extents.dot( _tmp_vec3_1 );
  57.  
  58. _tmp_vec3_1.x = abs.e02;
  59. _tmp_vec3_1.y = abs.e12;
  60. _tmp_vec3_1.z = abs.e22;
  61. extents.z = local_half_extents.dot( _tmp_vec3_1 );
  62.  
  63. this.min.subtractVectors( center, extents );
  64. this.max.addVectors( center, extents );
  65. };
  66. })();
  67.  
  68. Goblin.AABB.prototype.intersects = function( aabb ) {
  69. if (
  70. this.max.x < aabb.min.x ||
  71. this.max.y < aabb.min.y ||
  72. this.max.z < aabb.min.z ||
  73. this.min.x > aabb.max.x ||
  74. this.min.y > aabb.max.y ||
  75. this.min.z > aabb.max.z
  76. )
  77. {
  78. return false;
  79. }
  80.  
  81. return true;
  82. };
  83.  
  84. /**
  85. * Checks if a ray segment intersects with this AABB
  86. *
  87. * @method testRayIntersect
  88. * @property start {vec3} start point of the segment
  89. * @property end {vec3{ end point of the segment
  90. * @return {boolean}
  91. */
  92. Goblin.AABB.prototype.testRayIntersect = (function(){
  93. var direction = new Goblin.Vector3(),
  94. tmin, tmax,
  95. ood, t1, t2,
  96. axis;
  97.  
  98. return function( start, end ) {
  99. tmin = 0;
  100.  
  101. direction.subtractVectors( end, start );
  102. tmax = direction.length();
  103. direction.scale( 1 / tmax ); // normalize direction
  104.  
  105. for ( var i = 0; i < 3; i++ ) {
  106. axis = i === 0 ? 'x' : ( i === 1 ? 'y' : 'z' );
  107. var extent_min = ( i === 0 ? this.min.x : ( i === 1 ? this.min.y : this.min.z ) ),
  108. extent_max = ( i === 0 ? this.max.x : ( i === 1 ? this.max.y : this.max.z ) );
  109.  
  110. if ( Math.abs( direction[axis] ) < Goblin.EPSILON ) {
  111. // Ray is parallel to axis
  112. if ( start[axis] < extent_min || start[axis] > extent_max ) {
  113. return false;
  114. }
  115. } else {
  116. ood = 1 / direction[axis];
  117. t1 = ( extent_min - start[axis] ) * ood;
  118. t2 = ( extent_max - start[axis] ) * ood;
  119. if ( t1 > t2 ) {
  120. ood = t1; // ood is a convenient temp variable as it's not used again
  121. t1 = t2;
  122. t2 = ood;
  123. }
  124.  
  125. // Find intersection intervals
  126. tmin = Math.max( tmin, t1 );
  127. tmax = Math.min( tmax, t2 );
  128.  
  129. if ( tmin > tmax ) {
  130. return false;
  131. }
  132. }
  133. }
  134.  
  135. return true;
  136. };
  137. })();