API Docs for: GoblinPhysics
Show:

File: src\classes\Shapes\PlaneShape.js

  1. /**
  2. * @class PlaneShape
  3. * @param orientation {Number} index of axis which is the plane's normal ( 0 = X, 1 = Y, 2 = Z )
  4. * @param half_width {Number} half width of the plane
  5. * @param half_length {Number} half height of the plane
  6. * @constructor
  7. */
  8. Goblin.PlaneShape = function( orientation, half_width, half_length ) {
  9. /**
  10. * index of axis which is the plane's normal ( 0 = X, 1 = Y, 2 = Z )
  11. * when 0, width is Y and length is Z
  12. * when 1, width is X and length is Z
  13. * when 2, width is X and length is Y
  14. *
  15. * @property half_width
  16. * @type {Number}
  17. */
  18. this.orientation = orientation;
  19.  
  20. /**
  21. * half width of the plane
  22. *
  23. * @property half_height
  24. * @type {Number}
  25. */
  26. this.half_width = half_width;
  27.  
  28. /**
  29. * half length of the plane
  30. *
  31. * @property half_length
  32. * @type {Number}
  33. */
  34. this.half_length = half_length;
  35.  
  36. this.aabb = new Goblin.AABB();
  37. this.calculateLocalAABB( this.aabb );
  38.  
  39.  
  40. if ( this.orientation === 0 ) {
  41. this._half_width = 0;
  42. this._half_height = this.half_width;
  43. this._half_depth = this.half_length;
  44. } else if ( this.orientation === 1 ) {
  45. this._half_width = this.half_width;
  46. this._half_height = 0;
  47. this._half_depth = this.half_length;
  48. } else {
  49. this._half_width = this.half_width;
  50. this._half_height = this.half_length;
  51. this._half_depth = 0;
  52. }
  53. };
  54.  
  55. /**
  56. * Calculates this shape's local AABB and stores it in the passed AABB object
  57. *
  58. * @method calculateLocalAABB
  59. * @param aabb {AABB}
  60. */
  61. Goblin.PlaneShape.prototype.calculateLocalAABB = function( aabb ) {
  62. if ( this.orientation === 0 ) {
  63. this._half_width = 0;
  64. this._half_height = this.half_width;
  65. this._half_depth = this.half_length;
  66.  
  67. aabb.min.x = 0;
  68. aabb.min.y = -this.half_width;
  69. aabb.min.z = -this.half_length;
  70.  
  71. aabb.max.x = 0;
  72. aabb.max.y = this.half_width;
  73. aabb.max.z = this.half_length;
  74. } else if ( this.orientation === 1 ) {
  75. this._half_width = this.half_width;
  76. this._half_height = 0;
  77. this._half_depth = this.half_length;
  78.  
  79. aabb.min.x = -this.half_width;
  80. aabb.min.y = 0;
  81. aabb.min.z = -this.half_length;
  82.  
  83. aabb.max.x = this.half_width;
  84. aabb.max.y = 0;
  85. aabb.max.z = this.half_length;
  86. } else {
  87. this._half_width = this.half_width;
  88. this._half_height = this.half_length;
  89. this._half_depth = 0;
  90.  
  91. aabb.min.x = -this.half_width;
  92. aabb.min.y = -this.half_length;
  93. aabb.min.z = 0;
  94.  
  95. aabb.max.x = this.half_width;
  96. aabb.max.y = this.half_length;
  97. aabb.max.z = 0;
  98. }
  99. };
  100.  
  101. Goblin.PlaneShape.prototype.getInertiaTensor = function( mass ) {
  102. var width_squared = this.half_width * this.half_width * 4,
  103. length_squared = this.half_length * this.half_length * 4,
  104. element = 0.0833 * mass,
  105.  
  106. x = element * length_squared,
  107. y = element * ( width_squared + length_squared ),
  108. z = element * width_squared;
  109.  
  110. if ( this.orientation === 0 ) {
  111. return new Goblin.Matrix3(
  112. y, 0, 0,
  113. 0, x, 0,
  114. 0, 0, z
  115. );
  116. } else if ( this.orientation === 1 ) {
  117. return new Goblin.Matrix3(
  118. x, 0, 0,
  119. 0, y, 0,
  120. 0, 0, z
  121. );
  122. } else {
  123. return new Goblin.Matrix3(
  124. y, 0, 0,
  125. 0, z, 0,
  126. 0, 0, x
  127. );
  128. }
  129. };
  130.  
  131. /**
  132. * Given `direction`, find the point in this body which is the most extreme in that direction.
  133. * This support point is calculated in world coordinates and stored in the second parameter `support_point`
  134. *
  135. * @method findSupportPoint
  136. * @param direction {vec3} direction to use in finding the support point
  137. * @param support_point {vec3} vec3 variable which will contain the supporting point after calling this method
  138. */
  139. Goblin.PlaneShape.prototype.findSupportPoint = function( direction, support_point ) {
  140. /*
  141. support_point = [
  142. sign( direction.x ) * _half_width,
  143. sign( direction.y ) * _half_height,
  144. sign( direction.z ) * _half_depth
  145. ]
  146. */
  147.  
  148. // Calculate the support point in the local frame
  149. if ( direction.x < 0 ) {
  150. support_point.x = -this._half_width;
  151. } else {
  152. support_point.x = this._half_width;
  153. }
  154.  
  155. if ( direction.y < 0 ) {
  156. support_point.y = -this._half_height;
  157. } else {
  158. support_point.y = this._half_height;
  159. }
  160.  
  161. if ( direction.z < 0 ) {
  162. support_point.z = -this._half_depth;
  163. } else {
  164. support_point.z = this._half_depth;
  165. }
  166. };
  167.  
  168. /**
  169. * Checks if a ray segment intersects with the shape
  170. *
  171. * @method rayIntersect
  172. * @property start {vec3} start point of the segment
  173. * @property end {vec3{ end point of the segment
  174. * @return {RayIntersection|null} if the segment intersects, a RayIntersection is returned, else `null`
  175. */
  176. Goblin.PlaneShape.prototype.rayIntersect = (function(){
  177. var normal = new Goblin.Vector3(),
  178. ab = new Goblin.Vector3(),
  179. point = new Goblin.Vector3(),
  180. t;
  181.  
  182. return function( start, end ) {
  183. if ( this.orientation === 0 ) {
  184. normal.x = 1;
  185. normal.y = normal.z = 0;
  186. } else if ( this.orientation === 1 ) {
  187. normal.y = 1;
  188. normal.x = normal.z = 0;
  189. } else {
  190. normal.z = 1;
  191. normal.x = normal.y = 0;
  192. }
  193.  
  194. ab.subtractVectors( end, start );
  195. t = -normal.dot( start ) / normal.dot( ab );
  196.  
  197. if ( t < 0 || t > 1 ) {
  198. return null;
  199. }
  200.  
  201. point.scaleVector( ab, t );
  202. point.add( start );
  203.  
  204. if ( point.x < -this._half_width || point.x > this._half_width ) {
  205. return null;
  206. }
  207.  
  208. if ( point.y < -this._half_height || point.y > this._half_height ) {
  209. return null;
  210. }
  211.  
  212. if ( point.z < -this._half_depth || point.z > this._half_depth ) {
  213. return null;
  214. }
  215.  
  216. var intersection = Goblin.ObjectPool.getObject( 'RayIntersection' );
  217. intersection.object = this;
  218. intersection.t = t * ab.length();
  219. intersection.point.copy( point );
  220. intersection.normal.copy( normal );
  221.  
  222. return intersection;
  223. };
  224. })();