diff --git a/build/game-lib-min.js b/build/game-lib-min.js index 1595b67..afdab8c 100644 --- a/build/game-lib-min.js +++ b/build/game-lib-min.js @@ -1,2 +1,2 @@ -function GameLib(){}"undefined"==typeof GameLib.D3&&(GameLib.D3=function(){}),GameLib.D3.BoneWeight=function(e,i){this.boneIndex=e,this.weight=i},GameLib.D3.Bone=function(e,i,t,n,s,o,a,r,h,l){this.id=e,this.name=t,this.boneId=i,"undefined"==typeof n&&(n=[]),this.childBoneIds=n,"undefined"==typeof s&&(s=null),this.parentBoneId=s,"undefined"==typeof o&&(o=new GameLib.D3.Vector4),this.quaternion=o,"undefined"==typeof a&&(a=new GameLib.D3.Vector3(0,0,0)),this.position=a,"undefined"==typeof r&&(r=new GameLib.D3.Vector3(0,0,0)),this.rotation=r,"undefined"==typeof h&&(h=new GameLib.D3.Vector3(1,1,1)),this.scale=h,"undefined"==typeof l&&(l=new GameLib.D3.Vector3(0,1,0)),this.up=l},GameLib.D3.Broadphase=function(e,i,t,n,s){this.id=e,"undefined"==typeof i&&(i="broadphase-"+t),this.name=i,"undefined"==typeof t&&(t=GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE),this.broadphaseType=t,"undefined"==typeof n&&(n=null),this.engine=n,this.instance=null,s&&this.createInstance()},GameLib.D3.Broadphase.prototype.createInstance=function(){if(!(this.engine instanceof GameLib.D3.Engine))throw console.warn("No Engine"),new Error("No Engine");this.engine.isNotCannonThrow();var e=null;if(this.broadphaseType==GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE)e=new this.engine.instance.NaiveBroadphase;else if(this.broadphaseType==GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID)e=new this.engine.instance.GridBroadphase;else{if(this.broadphaseType!=GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP)throw console.warn("Unsupported broadphase type: "+this.broadphaseType),new Error("Unsupported broadphase type: "+this.broadphaseType);e=new this.engine.instance.SAPBroardphase}return this.instance=e,e},GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE=1,GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID=2,GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP=3,GameLib.D3.Color=function(e,i,t,n){this.r=e,this.g=i,this.b=t,this.a=n},GameLib.D3.Engine=function(e,i){this.engineType=e,this.instance=i},GameLib.D3.Engine.prototype.isCannon=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_CANNON},GameLib.D3.Engine.prototype.isNotCannonThrow=function(){if(this.engineType!=GameLib.D3.Engine.ENGINE_TYPE_CANNON)throw console.warn("Only CANNON supported for this function"),new Error("Only CANNON supported for this function")},GameLib.D3.Engine.prototype.isAmmo=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_AMMO},GameLib.D3.Engine.prototype.isGoblin=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_GOBLIN},GameLib.D3.Engine.ENGINE_TYPE_CANNON=1,GameLib.D3.Engine.ENGINE_TYPE_AMMO=2,GameLib.D3.Engine.ENGINE_TYPE_GOBLIN=3,GameLib.D3.FlyControls=function(e,i,t){this.flySpeed=100,this.canvas=t,this.THREE=i,this.yaw=0,this.pitch=0,this.canRotate=!1,this.moveForward=!1,this.moveBackward=!1,this.moveLeft=!1,this.moveRight=!1,this.moveUp=!1,this.moveDown=!1,this.mouseUpCallback=this.onMouseUp.bind(this),this.mouseDownCallback=this.onMouseDown.bind(this),this.mouseMoveCallback=this.onMouseMove.bind(this),this.mouseWheelCallback=this.onMouseWheel.bind(this),this.keyDownCallback=this.onKeyDown.bind(this),this.keyUpCallback=this.onKeyUp.bind(this),this.camera=e,this.canvas.addEventListener("keydown",this.keyDownCallback,!1),this.canvas.addEventListener("keyup",this.keyUpCallback,!1),this.canvas.addEventListener("mousedown",this.mouseDownCallback,!1),this.canvas.addEventListener("mouseup",this.mouseUpCallback,!1),this.canvas.addEventListener("mousewheel",this.mouseWheelCallback,!1),this.havePointerLock="pointerLockElement"in document||"mozPointerLockElement"in document||"webkitPointerLockElement"in document,this.element=document.body,this.havePointerLock&&(this.element.requestPointerLock=this.element.requestPointerLock||this.element.mozRequestPointerLock||this.element.webkitRequestPointerLock,document.exitPointerLock=document.exitPointerLock||document.mozExitPointerLock||document.webkitExitPointerLock)},GameLib.D3.FlyControls.prototype.onMouseWheel=function(e){this.moveForward=!0,this.applyTranslation(.001*e.wheelDelta),e.preventDefault(),this.moveForward=!1},GameLib.D3.FlyControls.prototype.onMouseDown=function(e){1==e.button&&(this.canRotate=!0,this.canvas.addEventListener("mousemove",this.mouseMoveCallback,!1))},GameLib.D3.FlyControls.prototype.onMouseUp=function(e){1==e.button&&(this.canRotate=!1,this.canvas.removeEventListener("mousemove",this.mouseMoveCallback))},GameLib.D3.FlyControls.prototype.applyRotation=function(){this.camera.rotation.set(this.pitch,this.yaw,0,"YXZ")},GameLib.D3.FlyControls.prototype.applyTranslation=function(e){var i=new this.THREE.Vector3(0,0,-1),t=new this.THREE.Euler(0,0,0,"YXZ");t.set(this.pitch,this.yaw,0,"YXZ"),i=i.applyEuler(t);var n=i.normalize(),s=n.cross(new this.THREE.Vector3(0,1,0));this.moveForward?(this.camera.position.x+=n.x*(e*this.flySpeed),this.camera.position.y+=n.y*(e*this.flySpeed),this.camera.position.z+=n.z*(e*this.flySpeed)):this.moveBackward&&(this.camera.position.x-=n.x*(e*this.flySpeed),this.camera.position.y-=n.y*(e*this.flySpeed),this.camera.position.z-=n.z*(e*this.flySpeed)),this.moveLeft?(this.camera.position.x-=s.x*(e*this.flySpeed),this.camera.position.y-=s.y*(e*this.flySpeed),this.camera.position.z-=s.z*(e*this.flySpeed)):this.moveRight&&(this.camera.position.x+=s.x*(e*this.flySpeed),this.camera.position.y+=s.y*(e*this.flySpeed),this.camera.position.z+=s.z*(e*this.flySpeed)),this.moveUp?this.camera.position.y+=e*this.flySpeed:this.moveDown&&(this.camera.position.y-=e*this.flySpeed)},GameLib.D3.FlyControls.prototype.update=function(e){this.applyRotation(),this.applyTranslation(e)},GameLib.D3.FlyControls.prototype.onMouseMove=function(e){if(this.canRotate){var i=e.movementX||e.mozMovementX||e.webkitMovementX||0,t=e.movementY||e.mozMovementY||e.webkitMovementY||0;this.yaw-=.002*i,this.pitch-=.002*t}},GameLib.D3.FlyControls.prototype.onKeyDown=function(e){switch(e.keyCode){case 87:this.moveForward=!0;break;case 65:this.moveLeft=!0;break;case 83:this.moveBackward=!0;break;case 68:this.moveRight=!0;break;case 104:this.moveUp=!0;break;case 98:this.moveDown=!0}},GameLib.D3.FlyControls.prototype.onKeyUp=function(e){switch(e.keyCode){case 38:case 87:this.moveForward=!1;break;case 37:case 65:this.moveLeft=!1;break;case 40:case 83:this.moveBackward=!1;break;case 39:case 68:this.moveRight=!1;break;case 104:this.moveUp=!1;break;case 98:this.moveDown=!1}},GameLib.D3.Game=function(){this.scenes={},this.physicsWorlds=[],this.sceneToPhysicsWorldsMap={}},GameLib.D3.Game.prototype.AddScene=function(e){this.scenes[e.name]=e},GameLib.D3.Game.prototype.AddPhysicsWorld=function(e){this.physicsWorlds.push(e)},GameLib.D3.Game.prototype.LinkPhysicsWorldToScene=function(e,i){this.sceneToPhysicsWorldsMap[i.name]=this.sceneToPhysicsWorldsMap[i.name]||[],this.sceneToPhysicsWorldsMap[i.name].push(e)},GameLib.D3.Game.prototype.GetPhysicsWorldsForScene=function(e){return this.sceneToPhysicsWorldsMap[e.name]},GameLib.D3.Game.prototype.ProcessPhysics=function(e){for(var i in this.sceneToPhysicsWorldsMap){var t=this.sceneToPhysicsWorldsMap[i],n=this.scenes[i];if(n&&t)for(var s=0,o=t.length;s0){var s=this.loadMaps(e,n,t);Q.all(s).then(function(){i.resolve(t)}).catch(function(e){console.log(e),i.reject(e)})}else i.resolve(t);return i.promise},GameLib.D3.Matrix3=function(e,i,t){this.identity(),e&&(this.rows[0]=e),i&&(this.rows[1]=i),t&&(this.rows[2]=t)},GameLib.D3.Matrix3.prototype.identity=function(){this.rows=[new GameLib.D3.Vector4(1,0,0),new GameLib.D3.Vector4(0,1,0),new GameLib.D3.Vector4(0,0,1)]},GameLib.D3.Matrix4=function(e,i,t,n){this.identity(),e&&(this.rows[0]=e),i&&(this.rows[1]=i),t&&(this.rows[2]=t),n&&(this.rows[3]=n)},GameLib.D3.Matrix4.prototype.rotationMatrixX=function(e){return this.identity(),this.rows[1]=new GameLib.D3.Vector4(0,Math.cos(e),-1*Math.sin(e),0),this.rows[2]=new GameLib.D3.Vector4(0,Math.sin(e),Math.cos(e),0),this},GameLib.D3.Matrix4.prototype.rotationMatrixY=function(e){return this.identity(),this.rows[0]=new GameLib.D3.Vector4(Math.cos(e),0,Math.sin(e),0),this.rows[2]=new GameLib.D3.Vector4(-1*Math.sin(e),0,Math.cos(e),0),this},GameLib.D3.Matrix4.prototype.rotationMatrixZ=function(e){return this.identity(),this.rows[0]=new GameLib.D3.Vector4(Math.cos(e),-1*Math.sin(e),0,0),this.rows[1]=new GameLib.D3.Vector4(Math.sin(e),Math.cos(e),0,0),this},GameLib.D3.Matrix4.prototype.rotateX=function(e,i){return this.identity(),this.rotationMatrixX(e),this.multiply(i)},GameLib.D3.Matrix4.prototype.rotateY=function(e,i){return this.identity(),this.rotationMatrixY(e),this.multiply(i)},GameLib.D3.Matrix4.prototype.rotateZ=function(e,i){return this.identity(),this.rotationMatrixZ(e),this.multiply(i)},GameLib.D3.Matrix4.prototype.multiply=function(e){return e instanceof GameLib.D3.Vector4?new GameLib.D3.Vector4(this.rows[0].x*e.x+this.rows[0].y*e.y+this.rows[0].z*e.z+this.rows[0].w*e.w,this.rows[1].x*e.x+this.rows[1].y*e.y+this.rows[1].z*e.z+this.rows[1].w*e.w,this.rows[2].x*e.x+this.rows[2].y*e.y+this.rows[2].z*e.z+this.rows[2].w*e.w,this.rows[3].x*e.x+this.rows[3].y*e.y+this.rows[3].z*e.z+this.rows[3].w*e.w):e instanceof GameLib.D3.Vector3?new GameLib.D3.Vector3(this.rows[0].x*e.x+this.rows[0].y*e.y+this.rows[0].z*e.z,this.rows[1].x*e.x+this.rows[1].y*e.y+this.rows[1].z*e.z,this.rows[2].x*e.x+this.rows[2].y*e.y+this.rows[2].z*e.z):void 0},GameLib.D3.Matrix4.prototype.identity=function(){this.rows=[new GameLib.D3.Vector4(1,0,0,0),new GameLib.D3.Vector4(0,1,0,0),new GameLib.D3.Vector4(0,0,1,0),new GameLib.D3.Vector4(0,0,0,1)]},GameLib.D3.Matrix4.prototype.lookAt=function(e,i,t){var n=new GameLib.D3.Vector3(e.x,e.y,e.z),s=n.subtract(i).normalize();0===s.squared()&&(s.z=1);var o=t.cross(s).normalize();0===o.squared()&&(s.x+=1e-4,o=t.cross(s).normalize());var a=s.cross(o);return this.rows[0].x=o.x,this.rows[0].y=o.y,this.rows[0].z=o.z,this.rows[1].x=a.x,this.rows[1].y=a.y,this.rows[1].z=a.z,this.rows[2].x=s.x,this.rows[2].y=s.y,this.rows[2].z=s.z,this},GameLib.D3.Mesh=function(e,i,t,n,s,o,a,r,h,l,c,p,m,d,y,u,f,b,L,v){this.id=e,this.path=i,this.name=t,this.meshType=n,this.vertices=s,this.faces=o,"undefined"==typeof a&&(a=null),this.skeleton=a,"undefined"==typeof r&&(r=[]),this.faceVertexUvs=r,"undefined"==typeof h&&(h=[]),this.skinIndices=h,"undefined"==typeof l&&(l=[]),this.skinWeights=l,"undefined"==typeof c&&(c=[]),this.materials=c,"undefined"==typeof p&&(p=new GameLib.D3.Vector3(0,0,0)),this.position=p,"undefined"==typeof m&&new GameLib.D3.Vector4,this.quaternion=m,"undefined"==typeof d&&(d=new GameLib.D3.Vector3(0,0,0)),this.rotation=d,"undefined"==typeof y&&(y=new GameLib.D3.Vector3(1,1,1)),this.scale=y,"undefined"==typeof u&&(u=new GameLib.D3.Vector3(0,1,0)),this.up=u,this.physics=f,this.parentMeshId=b,this.parentSceneId=L,this.rawData=null},GameLib.D3.Mesh.TYPE_NORMAL=0,GameLib.D3.Mesh.TYPE_SKINNED=1,GameLib.D3.prototype.createThreeMesh=function(e,i,t){var n=null;if(e.meshType==GameLib.D3.Mesh.TYPE_NORMAL&&(n=new this.THREE.Mesh(i,t)),e.meshType==GameLib.D3.Mesh.TYPE_SKINNED){for(var s=e.skeleton.bones,o=e.skinIndices,a=e.skinWeights,r=[],h=0;h0;){var a=s.pop();if(a.triangle.v0==a.edge.x&&a.triangle.v1==a.edge.y||a.triangle.v1==a.edge.x&&a.triangle.v2==a.edge.y||a.triangle.v2==a.edge.x&&a.triangle.v0==a.edge.y){var r=a.triangle.v1;a.triangle.v1=a.triangle.v2,a.triangle.v2=r;var h=a.triangle.v1uv;a.triangle.v1uv=a.triangle.v2uv,a.triangle.v2uv=h}o.push(a);for(var l=[new GameLib.D3.Vector2(a.triangle.v0,a.triangle.v1),new GameLib.D3.Vector2(a.triangle.v1,a.triangle.v2),new GameLib.D3.Vector2(a.triangle.v2,a.triangle.v0)],c=0;c9||console.log("The vertices are not in the right length : "+e.length);for(var t=[],n=new GameLib.D3.Vector4.Points,s=0;s0)for(var a=0;a0},GameLib.D3.Vector3.normal=function(e,i,t){var n=i.copy(),s=t.copy();return n.subtract(e).cross(s.subtract(e))},GameLib.D3.Vector3.prototype.lookAt=function(e,i){var t=GameLib.D3.Matrix4.lookAt(this,e,i);this.multiply(t)},GameLib.D3.Vector3.prototype.translate=function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this},GameLib.D3.Vector3.prototype.squared=function(){return this.x*this.x+this.y*this.y+this.z*this.z},GameLib.D3.Vector3.prototype.copy=function(){return new GameLib.D3.Vector3(this.x,this.y,this.z)},GameLib.D3.Vector3.prototype.multiply=function(e){if(e instanceof GameLib.D3.Vector3)this.x*=e.x,this.y*=e.y,this.z*=e.z;else{if(!(e instanceof GameLib.D3.Matrix4))throw console.log("functionality not implemented - please do this"),new Error("not implemented");var i=e.rows[0].x*this.x+e.rows[0].y*this.y+e.rows[0].z*this.z,t=e.rows[1].x*this.x+e.rows[1].y*this.y+e.rows[1].z*this.z,n=e.rows[2].x*this.x+e.rows[2].y*this.y+e.rows[2].z*this.z;this.x=i,this.y=t,this.z=n}return this},GameLib.D3.Vector3.prototype.dot=function(e){return this.x*e.x+this.y*e.y+this.z*e.z},GameLib.D3.Vector3.prototype.normalize=function(){var e=1e-6,i=this.squared();if(io&&(o=h.x,n=i*e)}this.vectors=s;for(var l=(new GameLib.D3.Matrix4).rotationMatrixY(n),c=0;co&&(o=h.y,n=i*e)}this.vectors=s;for(var l=(new GameLib.D3.Matrix4).rotationMatrixX(n),c=0;cn&&(n=this.vectors[a].x),this.vectors[a].y>s&&(s=this.vectors[a].y),this.vectors[a].z>o&&(o=this.vectors[a].z);return new GameLib.D3.Vector3(Math.abs(n-e),Math.abs(s-i),Math.abs(s-t))},GameLib.D3.Vector4.Points.prototype.average=function(){for(var e=0,i=0,t=0,n=0;n0){var o=this.loadMaps(e,s,n);Q.all(o).then(function(){i.resolve(n)}).catch(function(e){console.log(e),i.reject(e)})}else i.resolve(n);return i.promise},GameLib.D3.Matrix3=function(e,t,i){this.identity(),e&&(this.rows[0]=e),t&&(this.rows[1]=t),i&&(this.rows[2]=i)},GameLib.D3.Matrix3.prototype.identity=function(){this.rows=[new GameLib.D3.Vector4(1,0,0),new GameLib.D3.Vector4(0,1,0),new GameLib.D3.Vector4(0,0,1)]},GameLib.D3.Matrix4=function(e,t,i,n){this.identity(),e&&(this.rows[0]=e),t&&(this.rows[1]=t),i&&(this.rows[2]=i),n&&(this.rows[3]=n)},GameLib.D3.Matrix4.prototype.rotationMatrixX=function(e){return this.identity(),this.rows[1]=new GameLib.D3.Vector4(0,Math.cos(e),-1*Math.sin(e),0),this.rows[2]=new GameLib.D3.Vector4(0,Math.sin(e),Math.cos(e),0),this},GameLib.D3.Matrix4.prototype.rotationMatrixY=function(e){return this.identity(),this.rows[0]=new GameLib.D3.Vector4(Math.cos(e),0,Math.sin(e),0),this.rows[2]=new GameLib.D3.Vector4(-1*Math.sin(e),0,Math.cos(e),0),this},GameLib.D3.Matrix4.prototype.rotationMatrixZ=function(e){return this.identity(),this.rows[0]=new GameLib.D3.Vector4(Math.cos(e),-1*Math.sin(e),0,0),this.rows[1]=new GameLib.D3.Vector4(Math.sin(e),Math.cos(e),0,0),this},GameLib.D3.Matrix4.prototype.rotateX=function(e,t){return this.identity(),this.rotationMatrixX(e),this.multiply(t)},GameLib.D3.Matrix4.prototype.rotateY=function(e,t){return this.identity(),this.rotationMatrixY(e),this.multiply(t)},GameLib.D3.Matrix4.prototype.rotateZ=function(e,t){return this.identity(),this.rotationMatrixZ(e),this.multiply(t)},GameLib.D3.Matrix4.prototype.multiply=function(e){return e instanceof GameLib.D3.Vector4?new GameLib.D3.Vector4(this.rows[0].x*e.x+this.rows[0].y*e.y+this.rows[0].z*e.z+this.rows[0].w*e.w,this.rows[1].x*e.x+this.rows[1].y*e.y+this.rows[1].z*e.z+this.rows[1].w*e.w,this.rows[2].x*e.x+this.rows[2].y*e.y+this.rows[2].z*e.z+this.rows[2].w*e.w,this.rows[3].x*e.x+this.rows[3].y*e.y+this.rows[3].z*e.z+this.rows[3].w*e.w):e instanceof GameLib.D3.Vector3?new GameLib.D3.Vector3(this.rows[0].x*e.x+this.rows[0].y*e.y+this.rows[0].z*e.z,this.rows[1].x*e.x+this.rows[1].y*e.y+this.rows[1].z*e.z,this.rows[2].x*e.x+this.rows[2].y*e.y+this.rows[2].z*e.z):void 0},GameLib.D3.Matrix4.prototype.identity=function(){this.rows=[new GameLib.D3.Vector4(1,0,0,0),new GameLib.D3.Vector4(0,1,0,0),new GameLib.D3.Vector4(0,0,1,0),new GameLib.D3.Vector4(0,0,0,1)]},GameLib.D3.Matrix4.prototype.lookAt=function(e,t,i){var n=new GameLib.D3.Vector3(e.x,e.y,e.z),s=n.subtract(t).normalize();0===s.squared()&&(s.z=1);var o=i.cross(s).normalize();0===o.squared()&&(s.x+=1e-4,o=i.cross(s).normalize());var a=s.cross(o);return this.rows[0].x=o.x,this.rows[0].y=o.y,this.rows[0].z=o.z,this.rows[1].x=a.x,this.rows[1].y=a.y,this.rows[1].z=a.z,this.rows[2].x=s.x,this.rows[2].y=s.y,this.rows[2].z=s.z,this},GameLib.D3.Mesh=function(e,t,i,n,s,o,a,r,h,l,c,p,m,d,y,u,f,b,L,v){this.id=e,this.path=t,this.name=i,this.meshType=n,this.vertices=s,this.faces=o,"undefined"==typeof a&&(a=null),this.skeleton=a,"undefined"==typeof r&&(r=[]),this.faceVertexUvs=r,"undefined"==typeof h&&(h=[]),this.skinIndices=h,"undefined"==typeof l&&(l=[]),this.skinWeights=l,"undefined"==typeof c&&(c=[]),this.materials=c,"undefined"==typeof p&&(p=new GameLib.D3.Vector3(0,0,0)),this.position=p,"undefined"==typeof m&&new GameLib.D3.Vector4,this.quaternion=m,"undefined"==typeof d&&(d=new GameLib.D3.Vector3(0,0,0)),this.rotation=d,"undefined"==typeof y&&(y=new GameLib.D3.Vector3(1,1,1)),this.scale=y,"undefined"==typeof u&&(u=new GameLib.D3.Vector3(0,1,0)),this.up=u,this.physics=f,this.parentMeshId=b,this.parentSceneId=L,this.rawData=null},GameLib.D3.Mesh.TYPE_NORMAL=0,GameLib.D3.Mesh.TYPE_SKINNED=1,GameLib.D3.prototype.createThreeMesh=function(e,t,i){var n=null;if(e.meshType==GameLib.D3.Mesh.TYPE_NORMAL&&(n=new this.THREE.Mesh(t,i)),e.meshType==GameLib.D3.Mesh.TYPE_SKINNED){for(var s=e.skeleton.bones,o=e.skinIndices,a=e.skinWeights,r=[],h=0;h0;){var a=s.pop();if(a.triangle.v0==a.edge.x&&a.triangle.v1==a.edge.y||a.triangle.v1==a.edge.x&&a.triangle.v2==a.edge.y||a.triangle.v2==a.edge.x&&a.triangle.v0==a.edge.y){var r=a.triangle.v1;a.triangle.v1=a.triangle.v2,a.triangle.v2=r;var h=a.triangle.v1uv;a.triangle.v1uv=a.triangle.v2uv,a.triangle.v2uv=h}o.push(a);for(var l=[new GameLib.D3.Vector2(a.triangle.v0,a.triangle.v1),new GameLib.D3.Vector2(a.triangle.v1,a.triangle.v2),new GameLib.D3.Vector2(a.triangle.v2,a.triangle.v0)],c=0;c9||console.log("The vertices are not in the right length : "+e.length);for(var i=[],n=new GameLib.D3.Vector4.Points,s=0;s0)for(var a=0;a0},GameLib.D3.Vector3.normal=function(e,t,i){var n=t.copy(),s=i.copy();return n.subtract(e).cross(s.subtract(e))},GameLib.D3.Vector3.prototype.lookAt=function(e,t){var i=GameLib.D3.Matrix4.lookAt(this,e,t);this.multiply(i)},GameLib.D3.Vector3.prototype.translate=function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this},GameLib.D3.Vector3.prototype.squared=function(){return this.x*this.x+this.y*this.y+this.z*this.z},GameLib.D3.Vector3.prototype.copy=function(){return new GameLib.D3.Vector3(this.x,this.y,this.z)},GameLib.D3.Vector3.prototype.multiply=function(e){if(e instanceof GameLib.D3.Vector3)this.x*=e.x,this.y*=e.y,this.z*=e.z;else{if(!(e instanceof GameLib.D3.Matrix4))throw console.log("functionality not implemented - please do this"),new Error("not implemented");var t=e.rows[0].x*this.x+e.rows[0].y*this.y+e.rows[0].z*this.z,i=e.rows[1].x*this.x+e.rows[1].y*this.y+e.rows[1].z*this.z,n=e.rows[2].x*this.x+e.rows[2].y*this.y+e.rows[2].z*this.z;this.x=t,this.y=i,this.z=n}return this},GameLib.D3.Vector3.prototype.dot=function(e){return this.x*e.x+this.y*e.y+this.z*e.z};GameLib.D3.Vector3.prototype.normalize=function(){var e=1e-6,t=this.squared();if(to&&(o=h.x,n=t*e)}this.vectors=s;for(var l=(new GameLib.D3.Matrix4).rotationMatrixY(n),c=0;co&&(o=h.y,n=t*e)}this.vectors=s;for(var l=(new GameLib.D3.Matrix4).rotationMatrixX(n),c=0;cn&&(n=this.vectors[a].x),this.vectors[a].y>s&&(s=this.vectors[a].y),this.vectors[a].z>o&&(o=this.vectors[a].z);return new GameLib.D3.Vector3(Math.abs(n-e),Math.abs(s-t),Math.abs(s-i))},GameLib.D3.Vector4.Points.prototype.average=function(){for(var e=0,t=0,i=0,n=0;n maxXDistance) { - - maxXDistance = distances.x; - totalRadians = multiplier * grain; - } - } - - this.vectors = backupVectors; - -// console.log("distance: " + maxXDistance + " radians : " + totalRadians); - - var maxRotationMatrix = new GameLib.D3.Matrix4().rotationMatrixY(totalRadians); - - for (var k = 0; k < this.vectors.length; k++) { - this.vectors[k] = maxRotationMatrix.multiply(this.vectors[k]); - } - -// console.log("vectors (after): " + JSON.stringify(this.vectors, null, 2)); - -}; - -GameLib.D3.Vector4.Points.prototype.maximizeYDistance = function (grain) { - -// console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); - - var multiplier = 0; - - var rotationMatrixX = new GameLib.D3.Matrix4().rotationMatrixX(grain); - - var totalRadians = 0; - - var backupVectors = this.copy(); - - var maxYDistance = 0; - - for (var i = 0; i < Math.PI * 2; i += grain) { - - multiplier++; - - for (var j = 0; j < this.vectors.length; j++) { - this.vectors[j] = rotationMatrixX.multiply(this.vectors[j]); - } - - var distances = this.distances(); - - if (distances.y > maxYDistance) { - maxYDistance = distances.y; - totalRadians = multiplier * grain; - } - } - - this.vectors = backupVectors; - -// console.log("distance: " + maxYDistance + " radians : " + totalRadians); - - var maxRotationMatrix = new GameLib.D3.Matrix4().rotationMatrixX(totalRadians); - - for (var k = 0; k < this.vectors.length; k++) { - this.vectors[k] = maxRotationMatrix.multiply(this.vectors[k]); - } - -// console.log("vectors (after): " + JSON.stringify(this.vectors, null, 2)); - -}; - - -GameLib.D3.Vector4.Points.prototype.lookAt = function (at, up) { - - var polyCenter = this.average(); - - console.log("poly center : " + JSON.stringify(polyCenter)); - - var lookAtMatrix = new GameLib.D3.Matrix4().lookAt(polyCenter, at, up); - - lookAtMatrix.rows[0] = new GameLib.D3.Vector4(1, 0, 0, 0); - lookAtMatrix.rows[1] = new GameLib.D3.Vector4(0, 0, 1, 0); - lookAtMatrix.rows[2] = new GameLib.D3.Vector4(0, 1, 0, 0); - - console.log("look at matrix : " + JSON.stringify(lookAtMatrix, null, 2)); - - for (var i = 0; i < this.vectors.length; i++) { - console.log("vector " + i + " (before): " + JSON.stringify(this.vectors[i])); - this.vectors[i] = lookAtMatrix.multiply(this.vectors[i]); - console.log("vector " + i + " (after) : " + JSON.stringify(this.vectors[i])); - } -}; - -GameLib.D3.Vector4.Points.prototype.distances = function () { - - var minX = this.vectors[0].x; - var minY = this.vectors[0].y; - var minZ = this.vectors[0].z; - - var maxX = this.vectors[0].x; - var maxY = this.vectors[0].y; - var maxZ = this.vectors[0].z; - - for (var i = 0; i < this.vectors.length; i++) { - if (this.vectors[i].x < minX) { - minX = this.vectors[i].x; - } - if (this.vectors[i].y < minY) { - minY = this.vectors[i].y; - } - if (this.vectors[i].z < minZ) { - minZ = this.vectors[i].z; - } - - if (this.vectors[i].x > maxX) { - maxX = this.vectors[i].x; - } - if (this.vectors[i].y > maxY) { - maxY = this.vectors[i].y; - } - if (this.vectors[i].z > maxZ) { - maxZ = this.vectors[i].z; - } - } - - return new GameLib.D3.Vector3( - Math.abs(maxX - minX), - Math.abs(maxY - minY), - Math.abs(maxY - minZ) - ) -}; - -GameLib.D3.Vector4.Points.prototype.average = function () { - var averageX = 0; - var averageY = 0; - var averageZ = 0; - - for (var i = 0; i < this.vectors.length; i++) { - averageX += this.vectors[i].x; - averageY += this.vectors[i].y; - averageZ += this.vectors[i].z; - } - - return new GameLib.D3.Vector3( - averageX / this.vectors.length, - averageY / this.vectors.length, - averageZ / this.vectors.length - ); -}; - -GameLib.D3.Vector4.Points.prototype.negative = function () { - - for (var i = 0; i < this.vectors.length; i++) { - this.vectors[i].x *= -1; - this.vectors[i].y *= -1; - this.vectors[i].z *= -1; - } - - return this; -}; - - -GameLib.D3.Vector4.Points.prototype.toOrigin = function () { - - var distanceFromOrigin = this.average().negative(); - - for (var i = 0; i < this.vectors.length; i++) { - this.vectors[i].translate(distanceFromOrigin); - } -}; \ No newline at end of file diff --git a/src/game-lib-vector-4.js b/src/game-lib-vector-4.js index 18ee60f..b19eaaf 100644 --- a/src/game-lib-vector-4.js +++ b/src/game-lib-vector-4.js @@ -97,3 +97,227 @@ GameLib.D3.Vector4.prototype.subtract = function (v) { return this; }; +GameLib.D3.Vector4.Points = function () { + this.vectors = []; +}; + +GameLib.D3.Vector4.Points.prototype.add = function (vector) { + + if (vector instanceof GameLib.D3.Vector3) { + vector = new GameLib.D3.Vector4( + vector.x, + vector.y, + vector.z, + 1 + ) + } + + if (!vector instanceof GameLib.D3.Vector4) { + console.warn("Vector needs to be of type Vector4"); + throw new Error("Vector needs to be of type Vector4"); + } + + this.vectors.push(vector); + + return this; +}; + +GameLib.D3.Vector4.Points.prototype.copy = function () { + + var vectors = []; + + for (var i = 0; i < this.vectors.length; i++) { + vectors.push(this.vectors[i].copy()); + } + + return vectors; +}; + +GameLib.D3.Vector4.Points.prototype.maximizeXDistance = function (grain) { + +// console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); + + var multiplier = 0; + + var rotationMatrixY = new GameLib.D3.Matrix4().rotationMatrixY(grain); + + var totalRadians = 0; + + var backupVectors = this.copy(); + + var maxXDistance = 0; + + for (var i = 0; i < Math.PI * 2; i += grain) { + + multiplier++; + + for (var j = 0; j < this.vectors.length; j++) { + this.vectors[j] = rotationMatrixY.multiply(this.vectors[j]); + } + + var distances = this.distances(); + + if (distances.x > maxXDistance) { + + maxXDistance = distances.x; + totalRadians = multiplier * grain; + } + } + + this.vectors = backupVectors; + +// console.log("distance: " + maxXDistance + " radians : " + totalRadians); + + var maxRotationMatrix = new GameLib.D3.Matrix4().rotationMatrixY(totalRadians); + + for (var k = 0; k < this.vectors.length; k++) { + this.vectors[k] = maxRotationMatrix.multiply(this.vectors[k]); + } + +// console.log("vectors (after): " + JSON.stringify(this.vectors, null, 2)); + +}; + +GameLib.D3.Vector4.Points.prototype.maximizeYDistance = function (grain) { + +// console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); + + var multiplier = 0; + + var rotationMatrixX = new GameLib.D3.Matrix4().rotationMatrixX(grain); + + var totalRadians = 0; + + var backupVectors = this.copy(); + + var maxYDistance = 0; + + for (var i = 0; i < Math.PI * 2; i += grain) { + + multiplier++; + + for (var j = 0; j < this.vectors.length; j++) { + this.vectors[j] = rotationMatrixX.multiply(this.vectors[j]); + } + + var distances = this.distances(); + + if (distances.y > maxYDistance) { + maxYDistance = distances.y; + totalRadians = multiplier * grain; + } + } + + this.vectors = backupVectors; + +// console.log("distance: " + maxYDistance + " radians : " + totalRadians); + + var maxRotationMatrix = new GameLib.D3.Matrix4().rotationMatrixX(totalRadians); + + for (var k = 0; k < this.vectors.length; k++) { + this.vectors[k] = maxRotationMatrix.multiply(this.vectors[k]); + } + +// console.log("vectors (after): " + JSON.stringify(this.vectors, null, 2)); + +}; + + +GameLib.D3.Vector4.Points.prototype.lookAt = function (at, up) { + + var polyCenter = this.average(); + + console.log("poly center : " + JSON.stringify(polyCenter)); + + var lookAtMatrix = new GameLib.D3.Matrix4().lookAt(polyCenter, at, up); + + lookAtMatrix.rows[0] = new GameLib.D3.Vector4(1, 0, 0, 0); + lookAtMatrix.rows[1] = new GameLib.D3.Vector4(0, 0, 1, 0); + lookAtMatrix.rows[2] = new GameLib.D3.Vector4(0, 1, 0, 0); + + console.log("look at matrix : " + JSON.stringify(lookAtMatrix, null, 2)); + + for (var i = 0; i < this.vectors.length; i++) { + console.log("vector " + i + " (before): " + JSON.stringify(this.vectors[i])); + this.vectors[i] = lookAtMatrix.multiply(this.vectors[i]); + console.log("vector " + i + " (after) : " + JSON.stringify(this.vectors[i])); + } +}; + +GameLib.D3.Vector4.Points.prototype.distances = function () { + + var minX = this.vectors[0].x; + var minY = this.vectors[0].y; + var minZ = this.vectors[0].z; + + var maxX = this.vectors[0].x; + var maxY = this.vectors[0].y; + var maxZ = this.vectors[0].z; + + for (var i = 0; i < this.vectors.length; i++) { + if (this.vectors[i].x < minX) { + minX = this.vectors[i].x; + } + if (this.vectors[i].y < minY) { + minY = this.vectors[i].y; + } + if (this.vectors[i].z < minZ) { + minZ = this.vectors[i].z; + } + + if (this.vectors[i].x > maxX) { + maxX = this.vectors[i].x; + } + if (this.vectors[i].y > maxY) { + maxY = this.vectors[i].y; + } + if (this.vectors[i].z > maxZ) { + maxZ = this.vectors[i].z; + } + } + + return new GameLib.D3.Vector3( + Math.abs(maxX - minX), + Math.abs(maxY - minY), + Math.abs(maxY - minZ) + ) +}; + +GameLib.D3.Vector4.Points.prototype.average = function () { + var averageX = 0; + var averageY = 0; + var averageZ = 0; + + for (var i = 0; i < this.vectors.length; i++) { + averageX += this.vectors[i].x; + averageY += this.vectors[i].y; + averageZ += this.vectors[i].z; + } + + return new GameLib.D3.Vector3( + averageX / this.vectors.length, + averageY / this.vectors.length, + averageZ / this.vectors.length + ); +}; + +GameLib.D3.Vector4.Points.prototype.negative = function () { + + for (var i = 0; i < this.vectors.length; i++) { + this.vectors[i].x *= -1; + this.vectors[i].y *= -1; + this.vectors[i].z *= -1; + } + + return this; +}; + + +GameLib.D3.Vector4.Points.prototype.toOrigin = function () { + + var distanceFromOrigin = this.average().negative(); + + for (var i = 0; i < this.vectors.length; i++) { + this.vectors[i].translate(distanceFromOrigin); + } +}; diff --git a/src/game-lib-world.js b/src/game-lib-world.js index 1c008a7..a0a387f 100644 --- a/src/game-lib-world.js +++ b/src/game-lib-world.js @@ -105,7 +105,7 @@ GameLib.D3.World.prototype.createWorldInstance = function() { return customWorld; }; -GameLib.D3.Physics.World.prototype.AddShape = function( +GameLib.D3.World.prototype.AddShape = function( shape, // d3.physics.shape rigidBody, offset, // vec3 @@ -133,11 +133,11 @@ GameLib.D3.Physics.World.prototype.AddShape = function( } }; -GameLib.D3.Physics.World.prototype.Wheel = function() { +GameLib.D3.World.prototype.Wheel = function() { }; -GameLib.D3.Physics.World.prototype.CreateRigidVehicle = function( +GameLib.D3.World.prototype.CreateRigidVehicle = function( chassisBody // Physics.RigidBody ) { var rigidVehicle = new GameLib.D3.Physics.RigidVehicle(); @@ -151,7 +151,7 @@ GameLib.D3.Physics.World.prototype.CreateRigidVehicle = function( } }; -GameLib.D3.Physics.World.prototype.CreateRaycastVehicle = function( +GameLib.D3.World.prototype.CreateRaycastVehicle = function( chassisBody // Physics.RigidBody ) { var raycastVehicle = new GameLib.D3.Physics.RaycastVehicle(); @@ -165,7 +165,7 @@ GameLib.D3.Physics.World.prototype.CreateRaycastVehicle = function( } }; -GameLib.D3.Physics.World.prototype.AddWheelToRigidVehicle = function( +GameLib.D3.World.prototype.AddWheelToRigidVehicle = function( vehicle, rigidBody, position, @@ -182,7 +182,7 @@ GameLib.D3.Physics.World.prototype.AddWheelToRigidVehicle = function( } }; -GameLib.D3.Physics.World.prototype.AddWheelToRaycastVehicle = function ( +GameLib.D3.World.prototype.AddWheelToRaycastVehicle = function ( vehicle, // physics.raycastvehicle options // cannon options ) { @@ -193,22 +193,9 @@ GameLib.D3.Physics.World.prototype.AddWheelToRaycastVehicle = function ( } }; -GameLib.D3.Physics.World.prototype.RigidVehicle.prototype.GetWheelInfo = function( - -) { - // note: need a way to determine which engine we are currently using - return this.vehicleObject.wheelBodies; -}; - -GameLib.D3.Physics.World.prototype.RaycastVehicle.prototype.GetWheelInfo = function( - -) { - // note: need a way to determine which engine we are currently using - return this.vehicleObject.wheelInfos; -}; -GameLib.D3.Physics.World.prototype.CreateTriMeshShape = function( +GameLib.D3.World.prototype.CreateTriMeshShape = function( vertices, // flat list of floats indices // flat list of floats ) { @@ -217,7 +204,7 @@ GameLib.D3.Physics.World.prototype.CreateTriMeshShape = function( } }; -GameLib.D3.Physics.World.prototype.CreateSphereShape = function ( +GameLib.D3.World.prototype.CreateSphereShape = function ( radius ) { if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) { @@ -225,7 +212,7 @@ GameLib.D3.Physics.World.prototype.CreateSphereShape = function ( } }; -GameLib.D3.Physics.World.prototype.CreateBoxShape = function( +GameLib.D3.World.prototype.CreateBoxShape = function( halfExtensions // vec3 ) { if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) { @@ -233,7 +220,7 @@ GameLib.D3.Physics.World.prototype.CreateBoxShape = function( } }; -GameLib.D3.Physics.World.prototype.CreateCylinderShape = function( +GameLib.D3.World.prototype.CreateCylinderShape = function( radiusTop, radiusBottom, height, @@ -244,7 +231,7 @@ GameLib.D3.Physics.World.prototype.CreateCylinderShape = function( } }; -GameLib.D3.Physics.World.prototype.AddRigidBody = function( +GameLib.D3.World.prototype.AddRigidBody = function( rigidBody // Physics.RigidBody ) { if(this.physics.engineType === GameLib.D3.Physics.TYPE_CANNON) { @@ -252,7 +239,7 @@ GameLib.D3.Physics.World.prototype.AddRigidBody = function( } }; -GameLib.D3.Physics.World.prototype.AddVehicle = function( +GameLib.D3.World.prototype.AddVehicle = function( vehicle // note: physics.vehicle ) { if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) { @@ -260,7 +247,7 @@ GameLib.D3.Physics.World.prototype.AddVehicle = function( } }; -GameLib.D3.Physics.World.prototype.Step = function( +GameLib.D3.World.prototype.Step = function( timeStep ) { if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) { @@ -286,7 +273,7 @@ GameLib.D3.Physics.World.prototype.Step = function( } }; -GameLib.D3.Physics.World.prototype.GetIndexedVertices = function( +GameLib.D3.World.prototype.GetIndexedVertices = function( triangleMeshShape ) { @@ -304,7 +291,7 @@ GameLib.D3.Physics.World.prototype.GetIndexedVertices = function( }; -GameLib.D3.Physics.World.prototype.GenerateWireframeViewMesh = function( +GameLib.D3.World.prototype.GenerateWireframeViewMesh = function( triangleMeshShape, normalLength, scale, @@ -363,7 +350,7 @@ GameLib.D3.Physics.World.prototype.GenerateWireframeViewMesh = function( return wireframeTHREEMesh; }; -GameLib.D3.Physics.World.prototype.GenerateTriangleCollisionMesh = function( +GameLib.D3.World.prototype.GenerateTriangleCollisionMesh = function( threeMesh, mass, // default = 0 friction, // default = 10 diff --git a/test/test.GameLib.js b/test/test.GameLib.js new file mode 100644 index 0000000..af1d51d --- /dev/null +++ b/test/test.GameLib.js @@ -0,0 +1,49 @@ +var chai = require('chai'), + sinon = require("sinon"), + sinonChai = require("sinon-chai"), + config = require('../config.js'), + assert = chai.assert, + GameLib = require('../build/game-lib'); + +chai.use(sinonChai); + +describe('Bone', function(){ + + this.timeout(0); + + before(function(){ + + }); + + after(function(){ + + }); + + beforeEach(function(done) { + done(); + }); + + afterEach(function(done){ + done(); + }); + + it('Should create a Bone object', function (done) { + + var bone = new GameLib.D3.Bone( + null, + 1, + 'test bone 1', + [2, 3, 4] + ); + + assert(bone.position instanceof GameLib.D3.Vector3); + assert(bone.rotation instanceof GameLib.D3.Vector3); + assert(bone.scale instanceof GameLib.D3.Vector3); + assert(bone.up instanceof GameLib.D3.Vector3); + assert(bone.quaternion instanceof GameLib.D3.Vector4); + assert(bone.parentBoneId == null); + assert.deepEqual(bone.childBoneIds, [2,3,4]); + + done(); + }); +}); \ No newline at end of file