API Documentation
1 /* 2 GLGE WebGL Graphics Engine 3 Copyright (c) 2010, 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_physicsabstract.js 32 * @author me@paulbrunt.co.uk 33 */ 34 35 36 (function(GLGE){ 37 38 39 40 /** 41 * @class An abstract class used when constructing jiglib rigidbodies 42 * @augments GLGE.Group 43 */ 44 GLGE.PhysicsAbstract=function(uid){ 45 this.children=[]; 46 } 47 GLGE.augment(GLGE.Group,GLGE.PhysicsAbstract); 48 49 /** 50 * Enumeration for copy of rotation and location 51 **/ 52 GLGE.PHYSICS_ALL=1; 53 /** 54 * Enumeration for copy of location 55 **/ 56 GLGE.PHYSICS_LOC=2; 57 58 GLGE.PhysicsAbstract.prototype.physicsType=GLGE.PHYSICS_ALL; 59 GLGE.PhysicsAbstract.prototype.sync=true; 60 61 62 /** 63 * Sets the physics type either GLGE.PHYSICS_ALL or GLGE.PHYSICS_LOC 64 * @param {number} value the enumerations for physics type 65 **/ 66 GLGE.PhysicsAbstract.prototype.setType=function(value){ 67 this.physicsType=value; 68 return this; 69 } 70 71 /** 72 * Gets the physics type either GLGE.PHYSICS_ALL or GLGE.PHYSICS_LOC 73 **/ 74 GLGE.PhysicsAbstract.prototype.getType=function(value){ 75 return this.physicsType; 76 } 77 78 /** 79 * function run before proceeding with the physics sim 80 */ 81 GLGE.PhysicsAbstract.prototype.preProcess=function(dt){ 82 if(this.sync){ 83 //update the oriantation and position within jiglib 84 var matrix=this.getModelMatrix(); 85 this.jigLibObj.moveTo([matrix[3],matrix[7],matrix[11],0]); 86 if(this.physicsType==1){ 87 var sx=Math.sqrt(matrix[0]*matrix[0]+matrix[1]*matrix[1]+matrix[2]*matrix[2]); 88 var sy=Math.sqrt(matrix[4]*matrix[4]+matrix[5]*matrix[5]+matrix[6]*matrix[6]); 89 var sz=Math.sqrt(matrix[8]*matrix[8]+matrix[9]*matrix[9]+matrix[10]*matrix[10]); 90 this.jigLibObj.setOrientation(new jigLib.Matrix3D([matrix[0]/sx,matrix[1]/sx,matrix[2]/sx,0,matrix[4]/sy,matrix[5]/sy,matrix[6]/sy,0,matrix[8]/sz,matrix[9]/sz,matrix[10]/sz,0,0,0,0,1])); 91 } 92 this.sync=false; 93 } 94 } 95 96 /** 97 * get_transform gets the transform matrix 98 * @type jigLib.Matrix3D 99 * @private 100 **/ 101 GLGE.PhysicsAbstract.prototype.get_transform=function(){ 102 return new jigLib.Matrix3D(this.getModelMatrix()); 103 } 104 105 /** 106 * Updates the model matrix and flag physics system to sync 107 * @private 108 */ 109 GLGE.PhysicsAbstract.prototype.updateMatrix=function(){ 110 this.globalMatrix=null; 111 this.sync=true; 112 GLGE.Placeable.prototype.updateMatrix.call(this); 113 } 114 115 /** 116 * Gets the model matrix to transform the model within the world 117 */ 118 GLGE.PhysicsAbstract.prototype.getModelMatrix=function(){ 119 if(this.globalMatrix) return this.globalMatrix; 120 return GLGE.Placeable.prototype.getModelMatrix.call(this); 121 } 122 123 /** 124 * set_transform sets the transform matrix 125 * @param {Matrix3D} value 126 * @private 127 **/ 128 GLGE.PhysicsAbstract.prototype.set_transform=function(value){ 129 value=value.glmatrix; 130 var matrix=[value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8],value[9],value[10],value[11],value[12],value[13],value[14],value[15]]; 131 this.locX=value[3]; 132 this.locY=value[7]; 133 this.locZ=value[11]; 134 matrix=GLGE.mulMat4(matrix,this.getScaleMatrix()); 135 if(this.physicsType!=1){ 136 var M=this.getModelMatrix(); 137 matrix[0]=M[0]; 138 matrix[1]=M[1]; 139 matrix[2]=M[2]; 140 matrix[4]=M[4]; 141 matrix[5]=M[5]; 142 matrix[6]=M[6]; 143 matrix[8]=M[8]; 144 matrix[9]=M[9]; 145 matrix[10]=M[10]; 146 } 147 this.globalMatrix=matrix; 148 if(this.children){ 149 for(var i=0;i<this.children.length;i++){ 150 this.children[i].updateMatrix(); 151 } 152 } 153 return this; 154 } 155 156 /** 157 * Sets the velocity of the physics body 158 * @param {array} value The velocity to set 159 */ 160 GLGE.PhysicsAbstract.prototype.setVelocity=function(value,local){ 161 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 162 this.jigLibObj.setVelocity(value,local); 163 return this; 164 } 165 /** 166 * Sets the x velocity of the physics body 167 * @param {number} value The x velocity to set 168 */ 169 GLGE.PhysicsAbstract.prototype.setVelocityX=function(value){ 170 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 171 var vel=this.jigLibObj.getVelocity([0,0,0]); 172 vel[0]=+value; 173 this.jigLibObj.setVelocity(vel); 174 return this; 175 } 176 /** 177 * Sets the y velocity of the physics body 178 * @param {number} value The y velocity to set 179 */ 180 GLGE.PhysicsAbstract.prototype.setVelocityY=function(value){ 181 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 182 var vel=this.jigLibObj.getVelocity([0,0,0]); 183 vel[1]=+value; 184 this.jigLibObj.setVelocity(vel); 185 return this; 186 } 187 /** 188 * Sets the z velocity of the physics body 189 * @param {number} value The z velocity to set 190 */ 191 GLGE.PhysicsAbstract.prototype.setVelocityZ=function(value){ 192 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 193 var vel=this.jigLibObj.getVelocity([0,0,0]); 194 vel[2]=+value; 195 this.jigLibObj.setVelocity(vel); 196 return this; 197 } 198 /** 199 * Gets the velocity of the physics body 200 * @returns {array} The velocity to set 201 */ 202 GLGE.PhysicsAbstract.prototype.getVelocity=function(){ 203 return this.jigLibObj.getVelocity([0,0,0]); 204 } 205 /** 206 * Gets the x velocity of the physics body 207 * @returns {number} The x velocity to set 208 */ 209 GLGE.PhysicsAbstract.prototype.getVelocityX=function(){ 210 return this.jigLibObj.getVelocity([0,0,0])[0]; 211 } 212 /** 213 * Gets the y velocity of the physics body 214 * @returns {number} The y velocity to set 215 */ 216 GLGE.PhysicsAbstract.prototype.getVelocityY=function(){ 217 return this.jigLibObj.getVelocity([0,0,0])[1]; 218 } 219 /** 220 * Gets the z velocity of the physics body 221 * @returns {number} The z velocity to set 222 */ 223 GLGE.PhysicsAbstract.prototype.getVelocityZ=function(){ 224 return this.jigLibObj.getVelocity([0,0,0])[2]; 225 } 226 227 /** 228 * Sets the angular velocity of the physics body 229 * @param {array} value The velocity to set 230 */ 231 GLGE.PhysicsAbstract.prototype.setAngularVelocity=function(value){ 232 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 233 this.jigLibObj.setAngVel(value); 234 return this; 235 } 236 /** 237 * Sets the x-axis angular velocity of the physics body 238 * @param {number} value The x velocity to set 239 */ 240 GLGE.PhysicsAbstract.prototype.setAngularVelocityX=function(value){ 241 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 242 var vel=this.jigLibObj.getAngVel(); 243 vel[0]=+value; 244 this.jigLibObj.setAngVel(vel); 245 return this; 246 } 247 /** 248 * Sets the y-axis angular velocity of the physics body 249 * @param {number} value The y velocity to set 250 */ 251 GLGE.PhysicsAbstract.prototype.setAngularVelocityY=function(value){ 252 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 253 var vel=this.jigLibObj.getAngVel(); 254 vel[1]=+value; 255 this.jigLibObj.setAngVel(vel); 256 return this; 257 } 258 /** 259 * Sets the z-axis angular velocity of the physics body 260 * @param {number} value The z velocity to set 261 */ 262 GLGE.PhysicsAbstract.prototype.setAngularVelocityZ=function(value){ 263 if(!this.getMovable()) GLGE.error("Cannot set velocity on static object"); 264 var vel=this.jigLibObj.getAngVel(); 265 vel[2]=+value; 266 this.jigLibObj.setAngVel(vel); 267 return this; 268 } 269 /** 270 * Gets the angular velocity of the physics body 271 * @returns {array} The velocity to set 272 */ 273 GLGE.PhysicsAbstract.prototype.getAngularVelocity=function(){ 274 return this.jigLibObj.getAngVel(); 275 } 276 /** 277 * Gets the x-axis angular velocity of the physics body 278 * @returns {number} The x velocity to set 279 */ 280 GLGE.PhysicsAbstract.prototype.getAngularVelocityX=function(){ 281 return this.jigLibObj.getAngVel()[0]; 282 } 283 /** 284 * Gets the y-axis angular velocity of the physics body 285 * @returns {number} The y velocity to set 286 */ 287 GLGE.PhysicsAbstract.prototype.getAngularVelocityY=function(){ 288 return this.jigLibObj.getAngVel()[1]; 289 } 290 /** 291 * Gets the z-axis angular velocity of the physics body 292 * @returns {number} The z velocity to set 293 */ 294 GLGE.PhysicsAbstract.prototype.getAngularVelocityZ=function(){ 295 return this.jigLibObj.getAngVel()[2]; 296 } 297 /** 298 * Sets the movable flag for the object 299 * @param {boolean} value The movable flag 300 */ 301 GLGE.PhysicsAbstract.prototype.setMovable=function(value){ 302 this.jigLibObj.set_movable(value); 303 return this; 304 } 305 /** 306 * Gets the movable flag for the object 307 * @returns {boolean} The movable flag 308 */ 309 310 GLGE.PhysicsAbstract.prototype.getMovable=function(){ 311 return this.jigLibObj.get_movable(); 312 } 313 314 /** 315 * Sets the friction for the object 316 * @param {number} value The friction 0-1 317 */ 318 GLGE.PhysicsAbstract.prototype.setFriction=function(value){ 319 this.jigLibObj.set_friction(value); 320 return this; 321 } 322 /** 323 * Gets the friction for the object 324 * @returns {number} The friction 325 */ 326 GLGE.PhysicsAbstract.prototype.getFriction=function(){ 327 return this.jigLibObj.get_friction(); 328 } 329 330 331 /** 332 * Sets the mass for the object 333 * @param {number} value The mass 334 */ 335 GLGE.PhysicsAbstract.prototype.setMass=function(value){ 336 this.jigLibObj.set_mass(value); 337 return this; 338 } 339 340 /** 341 * Gets the mass for the object 342 * @returns {number} The mass 343 */ 344 GLGE.PhysicsAbstract.prototype.getMass=function(){ 345 return this.jigLibObj.get_mass(); 346 } 347 348 349 /** 350 * Sets the restitution for the object 351 * @param {number} value The restitution 0-1 352 */ 353 GLGE.PhysicsAbstract.prototype.setRestitution=function(value){ 354 this.jigLibObj.set_restitution(value); 355 return this; 356 } 357 /** 358 * Gets the restitution for the object 359 * @returns {number} The restitution 360 */ 361 GLGE.PhysicsAbstract.prototype.getRestitution=function(){ 362 return this.jigLibObj.get_restitution(); 363 } 364 365 /** 366 * Add forces in the body coordinate frame 367 * @param {array} f force expressed as a 3D vector 368 * @param {array} p position of origin of the force expressed as a 3D vector 369 **/ 370 GLGE.PhysicsAbstract.prototype.addBodyForce=function(f, p){ 371 this.jigLibObj.addBodyForce(f,p); 372 return this; 373 } 374 375 /** 376 * Add forces in the world coordinate frame 377 * @param {array} f force expressed as a 3D vector 378 * @param {array} p position of origin of the force expressed as a 3D vector 379 **/ 380 GLGE.PhysicsAbstract.prototype.addWorldForce=function(f, p){ 381 this.jigLibObj.addWorldForce(f,p); 382 return this; 383 } 384 385 /** 386 * Add torque in the world coordinate frame 387 * @param {array} t torque expressed as a 3D vector 388 **/ 389 GLGE.PhysicsAbstract.prototype.addWorldTorque=function(t){ 390 this.jigLibObj.addWorldTorque(t); 391 return this; 392 } 393 394 /** 395 * Add torque in the body coordinate frame 396 * @param {array} t torque expressed as a 3D vector 397 **/ 398 GLGE.PhysicsAbstract.prototype.addBodyTorque=function(t){ 399 this.jigLibObj.addBodyTorque(t); 400 return this; 401 } 402 /** 403 * Sets the linear velocity damping 404 * @param {array} damping 3D vector for linear damping 405 **/ 406 GLGE.PhysicsAbstract.prototype.setLinearVelocityDamping=function(v){ 407 this.jigLibObj.set_linVelocityDamping(v); 408 return this; 409 } 410 411 /** 412 * Gets the rotational velocity Damping 413 * @returns 3D vector for rotational damping 414 **/ 415 GLGE.PhysicsAbstract.prototype.getRotationalVelocityDamping=function(v){ 416 return this.jigLibObj.get_rotVelocityDamping(); 417 } 418 419 /** 420 * Gets the linear velocity damping 421 * @returns 3D vector for linear damping 422 **/ 423 GLGE.PhysicsAbstract.prototype.getLinearVelocityDamping=function(v){ 424 return this.jigLibObj.get_linVelocityDamping(); 425 } 426 427 /** 428 * Sets the rotational velocity Damping 429 * @param {array} damping 3D vector for rotational damping 430 **/ 431 GLGE.PhysicsAbstract.prototype.setRotationalVelocityDamping=function(v){ 432 this.jigLibObj.set_rotVelocityDamping(v); 433 return this; 434 } 435 436 437 /** 438 * Remove active force and torque 439 **/ 440 GLGE.PhysicsAbstract.prototype.clearForces=function(){ 441 this.jigLibObj.clearForces(); 442 return this; 443 } 444 445 446 447 448 })(GLGE);