Fork me on GitHub

API Documentation

  1 /*
  2 GLGE WebGL Graphics Engine
  3 Copyright (c) 2011, Paul Brunt
  4 All rights reserved.
  5 
  6 Redistribution and use in source and binary forms, with or without
  7 modification, are permitted provided that the following conditions are met:
  8     * Redistributions of source code must retain the above copyright
  9       notice, this list of conditions and the following disclaimer.
 10     * Redistributions in binary form must reproduce the above copyright
 11       notice, this list of conditions and the following disclaimer in the
 12       documentation and/or other materials provided with the distribution.
 13     * Neither the name of GLGE nor the
 14       names of its contributors may be used to endorse or promote products
 15       derived from this software without specific prior written permission.
 16 
 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 */
 28 
 29 /**
 30  * @fileOverview
 31  * @name glge_physicsext.js
 32  * @author me@paulbrunt.co.uk
 33  */
 34 
 35 
 36 (function(GLGE){
 37 
 38 
 39 GLGE.Scene.prototype.physicsGravity=[0,0,-9.8,0];
 40 
 41 /**
 42 * retrives the phsyics assets from the scene
 43 * @returns {array} the physics assets
 44 */
 45 GLGE.Scene.prototype.getPhysicsNodes=function(ret){
 46 	if(!ret) ret=[];
 47 	if(this.jigLibObj) ret.push(this);
 48 	if(this.children){
 49 		for(var i=0;i<this.children.length;i++){
 50 			GLGE.Scene.prototype.getPhysicsNodes.call(this.children[i],ret);
 51 		}
 52 	}
 53 	return ret;
 54 }
 55 
 56 /**
 57 * Picks within the physics system
 58 * @param {number} x screen x coord
 59 * @param {number} y screen y coord
 60 * @param {object} self optionally don't pick self
 61 * @returns picking result
 62 */
 63 GLGE.Scene.prototype.physicsPick=function(x,y,self){
 64 	if(!this.physicsSystem) this.physicsTick(0,true); //make sure the physics is set up
 65 	var ray=this.makeRay(x,y);
 66 	if(!ray) return;
 67 	
 68 	var cs=this.physicsSystem.getCollisionSystem();
 69 	var seg=new jigLib.JSegment(ray.origin,GLGE.scaleVec3(ray.coord,-1000));
 70 	var out={};
 71 	if(cs.segmentIntersect(out, seg, self ? self.jigLibObj : null)){
 72 		return {object:out.rigidBody.GLGE,normal:out.normal,distance:out.frac*1000,position:out.position};
 73 	}else{
 74 		return false;
 75 	}
 76 }
 77 
 78 /**
 79 * Picks a single objectwithin the physics system
 80 * @param {number} x screen x coord
 81 * @param {number} y screen y coord
 82 * @param {object} self  the object to perform the pick on
 83 * @returns picking result
 84 */
 85 GLGE.Scene.prototype.physicsPickObject=function(x,y,self){
 86 	if(!this.physicsSystem) this.physicsTick(0,true); //make sure the physics is set up
 87 	var ray=this.makeRay(x,y);
 88 	if(!ray) return;
 89 	
 90 	var cs=self.jigLibObj;
 91 	var seg=new jigLib.JSegment(ray.origin,GLGE.scaleVec3(ray.coord,-1000));
 92 	var out={};
 93 	if(cs.segmentIntersect(out, seg)){
 94 		return {normal:out.normal,distance:out.frac*1000,position:out.position};
 95 	}else{
 96 		return false;
 97 	}
 98 }
 99 
100 /**
101 * Does and intesection test on a given segment
102 * @param {array} start starting position of segment
103 * @param {array} delta the segment delta
104 * @returns segment test result object {object,normal,distance,position}
105 */
106 GLGE.Scene.prototype.segmentTest=function(start, delta,self){
107 	if(!this.physicsSystem || !this.physicsSystem._collisionSystem) return false;
108 	
109 	var seg=new jigLib.JSegment(start,delta);
110 	var out={};
111 	
112 	if(this.physicsSystem._collisionSystem.segmentIntersect(out,seg, self ? self.jigLibObj : null)){
113 		var length=Math.sqrt(delta[0]*delta[0]+delta[1]*delta[1]+delta[2]*delta[2]);
114 		return {object:out.rigidBody.GLGE,normal:out.normal,distance:out.frac*length,position:out.position};
115 	}
116 	return false
117 	
118 }
119 
120 
121 /**
122 * Integrate the phsyics system
123 * @param {number} dt the delta time to integrate for
124 */
125 GLGE.Scene.prototype.physicsTick=function(dt,noIntegrate){
126 	var objects=this.getPhysicsNodes();
127 	if(!this.physicsSystem){
128 		//create the physics system
129 		this.physicsSystem=jigLib.PhysicsSystem.getInstance();
130 		//this.physicsSystem.setCollisionSystem(true,-1000,-1000,-1000,2000,1000,2000,1,1,1);
131 		this.physicsSystem.setGravity(this.physicsGravity);
132 		for(var i=0;i<objects.length;i++){
133 			if(objects[i].jigLibObj) this.physicsSystem.addBody(objects[i].jigLibObj);
134 		}
135 		var that=this;
136 		this.addEventListener("childAdded",function(data){
137 			if(data.obj.jigLibObj) that.physicsSystem.addBody(data.obj.jigLibObj);
138 		});
139 		this.addEventListener("childRemoved",function(data){
140 			if(data.obj.jigLibObj) that.physicsSystem.removeBody(data.obj.jigLibObj);
141 		});
142 	}
143 	for(var i=0;i<objects.length;i++){
144 		if(objects[i].jigLibObj) {
145 			objects[i].preProcess(dt);
146 		}
147 	}
148 	if(!noIntegrate) this.physicsSystem.integrate(dt);
149 }
150 
151 
152 /**
153 * Sets the gravity of the physics system
154 * @param {number} gravity the gravity to apply to the physics system
155 */
156 GLGE.Scene.prototype.setGravity=function(gravity){
157 	this.physicsGravity=gravity;
158 	if(this.physicsSystem){
159 		this.physicsSystem.setGravity(gravity);
160 	}
161 	return this;
162 }
163 /**
164 * Gets the gravity of the physics system
165 * @returns {number}
166 */
167 GLGE.Scene.prototype.getGravity=function(gravity){
168 	return this.physicsSystem.getGravity(gravity);
169 }
170 
171 GLGE.Group.prototype.addPhysicsPlane=GLGE.Group.prototype.addChild;
172 GLGE.Group.prototype.addPhysicsBox=GLGE.Group.prototype.addChild;
173 GLGE.Group.prototype.addPhysicsSphere=GLGE.Group.prototype.addChild;
174 GLGE.Group.prototype.addPhysicsMesh=GLGE.Group.prototype.addChild;
175 GLGE.Scene.prototype.addPhysicsPlane=GLGE.Group.prototype.addChild;
176 GLGE.Scene.prototype.addPhysicsBox=GLGE.Group.prototype.addChild;
177 GLGE.Scene.prototype.addPhysicsSphere=GLGE.Group.prototype.addChild;
178 GLGE.Scene.prototype.addPhysicsMesh=GLGE.Group.prototype.addChild;
179 
180 })(GLGE);