1 /* 2 Copyright (c) 2007 Danny Chapman 3 http://www.rowlhouse.co.uk 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely, subject to the following restrictions: 11 1. The origin of this software must not be misrepresented; you must not 12 claim that you wrote the original software. If you use this software 13 in a product, an acknowledgment in the product documentation would be 14 appreciated but is not required. 15 2. Altered source versions must be plainly marked as such, and must not be 16 misrepresented as being the original software. 17 3. This notice may not be removed or altered from any source 18 distribution. 19 */ 20 21 (function(jigLib){ 22 var Vector3DUtil=jigLib.Vector3DUtil; 23 var JMatrix3D=jigLib.JMatrix3D; 24 var JNumber3D=jigLib.JNumber3D; 25 var RigidBody=jigLib.RigidBody; 26 27 /** 28 * @author Muzer(muzerly@gmail.com) 29 * 30 * @name JPlane 31 * @class JPlane 32 * @extends RigidBody 33 * @requires Vector3DUtil 34 * @requires JMatrix3D 35 * @requires JNumber3D 36 * @property {array} _initNormal the length of this JCapsule 37 * @property {array} _normal the radius of this JCapsule 38 * @property {number} _distance 39 * @constructor 40 * @param {ISkin3D} skin 41 * @param {array} initNormal 42 **/ 43 var JPlane=function(skin, initNormal){ 44 this.Super(skin); 45 if (initNormal == undefined) { 46 this._initNormal = [0, 0, -1, 0]; 47 this._normal = this._initNormal.slice(0); 48 }else{ 49 this._initNormal = initNormal.slice(0); 50 this._normal = this._initNormal.slice(0); 51 } 52 53 this._distance = 0; 54 this._type = "PLANE"; 55 this._movable=false; 56 }; 57 jigLib.extend(JPlane,jigLib.RigidBody); 58 59 JPlane.prototype._initNormal=null; 60 JPlane.prototype._normal=null; 61 JPlane.prototype._distance=null; 62 63 /** 64 * @function get_normal gets the normal 65 * @type array 66 **/ 67 JPlane.prototype.get_normal=function(){ 68 return this._normal; 69 }; 70 71 /** 72 * @function get_normal gets the distance 73 * @type number 74 **/ 75 JPlane.prototype.get_distance=function(){ 76 return this._distance; 77 }; 78 79 /** 80 * @function pointPlaneDistance gets the distance from a given point 81 * @param {array} pt the point expressed as a 3D vector 82 * @type array 83 **/ 84 JPlane.prototype.pointPlaneDistance=function(pt){ 85 return Vector3DUtil.dotProduct(this._normal, pt) - this._distance; 86 }; 87 88 /** 89 * @function segmentIntersect tests for intersection with a JSegment 90 * @param {object} out 91 * @param {JSegment} seg 92 * @param {PhysicsState} state 93 * @type boolean 94 **/ 95 JPlane.prototype.segmentIntersect=function(out, seg, state){ 96 out.fracOut = 0; 97 out.posOut = [0,0,0,0]; 98 out.normalOut = [0,0,0,0]; 99 100 var frac = 0; 101 102 var t; 103 104 var denom = Vector3DUtil.dotProduct(this._normal, seg.delta); 105 if (Math.abs(denom) > JNumber3D.NUM_TINY){ 106 t = -1 * (Vector3DUtil.dotProduct(this._normal, seg.origin) - this._distance) / denom; 107 108 if (t < 0 || t > 1){ 109 return false; 110 }else{ 111 frac = t; 112 out.fracOut = frac; 113 out.posOut = seg.getPoint(frac); 114 out.normalOut = this._normal.slice(0); 115 Vector3DUtil.normalize(out.normalOut); 116 return true; 117 } 118 }else{ 119 return false; 120 } 121 }; 122 123 /** 124 * @function updateState updates the current PhysicsState 125 * @type void 126 **/ 127 JPlane.prototype.updateState=function(){ 128 this.Super.prototype.updateState.call(this); 129 this._normal = this._initNormal.slice(0); 130 JMatrix3D.multiplyVector(this._currState._orientation, this._normal); 131 //_normal = _currState.orientation.transformVector(new Vector3D(0, 0, -1)); 132 this._distance = Vector3DUtil.dotProduct(this._currState.position, this._normal); 133 }; 134 135 jigLib.JPlane=JPlane; 136 137 })(jigLib); 138