From 4ace1890a6044396ff96990ba6586e50a32cfe4f Mon Sep 17 00:00:00 2001 From: Dilip Ramirez Date: Sun, 8 Mar 2020 19:34:52 -0300 Subject: [PATCH] Constraint WIP + Example update --- examples/spark/package-lock.json | 4 +--- examples/spark/package.json | 2 +- examples/spark/physar_examples/scripts/script.js | 2 +- examples/spark/physar_examples/scripts/tsconfig.json | 1 + examples/spark/src/index.js | 2 +- src/index.js | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) create mode 100644 examples/spark/physar_examples/scripts/tsconfig.json diff --git a/examples/spark/package-lock.json b/examples/spark/package-lock.json index b919e58..bed047b 100644 --- a/examples/spark/package-lock.json +++ b/examples/spark/package-lock.json @@ -4118,9 +4118,7 @@ } }, "physar": { - "version": "0.1.9-0", - "resolved": "https://registry.npmjs.org/physar/-/physar-0.1.9-0.tgz", - "integrity": "sha512-hTLsN6Gpa17AebioHsfaPuduAhnA62W0idOtUYJfNL1jcFT9Lvptwo20Ra4W/ZHpyiqP+5EIRt9r8OMQF7YpVA==" + "version": "file:../.." }, "pify": { "version": "4.0.1", diff --git a/examples/spark/package.json b/examples/spark/package.json index 19029ec..c52d777 100644 --- a/examples/spark/package.json +++ b/examples/spark/package.json @@ -10,7 +10,7 @@ "start": "npm run dev" }, "dependencies": { - "physar": "^0.1.9-0", + "physar": "file:../..", "short-uuid": "^3.1.1", "webpack": "^4.41.6" }, diff --git a/examples/spark/physar_examples/scripts/script.js b/examples/spark/physar_examples/scripts/script.js index fed6080..42c1a65 100644 --- a/examples/spark/physar_examples/scripts/script.js +++ b/examples/spark/physar_examples/scripts/script.js @@ -1 +1 @@ -!function(t){var e={};function r(s){if(e[s])return e[s].exports;var o=e[s]={i:s,l:!1,exports:{}};return t[s].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=t,r.c=e,r.d=function(t,e,s){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(s,o,function(e){return t[e]}.bind(null,o));return s},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=4)}([function(t,e){t.exports=require("Time")},function(t,e){t.exports=require("cannon")},function(t,e){t.exports=require("Diagnostics")},function(t,e){t.exports=require("Scene")},function(t,e,r){"use strict";r.r(e);const s=r(3),o=s.root.find("SphereObject"),i=s.root.find("Cube01"),n=s.root.find("plane0"),a=new class{constructor(t){this.Diagnostics=void 0,this.Time=void 0,this.currentInterval=void 0,this.lastInterval=void 0,this.lastTime=void 0,this.fixedTimeStep=1/60,this.maxSubSteps=3,this.timeInterval=5,this.CANNON=void 0,this.worldObjects=[],this.defaults={body:{mass:0,radius:1,transform:{rotation:{x:0,y:0,z:0,w:.5},position:{x:0,y:0,z:0},scale:{x:1,y:1,z:1}}},sync:{properties:["position","rotation"],axes:["x","y","z"],rotation:{enabled:!0,allAxis:!0,x:!0,y:!0,z:!0},position:{enabled:!0,allAxis:!0,x:!0,y:!0,z:!0}}},this.initRequiredSparkModules(),this.createCannonWorld(t)}initRequiredSparkModules(){this.Time=r(0),this.CANNON=r(1),this.Diagnostics=r(2)}createCannonWorld(t){this.world=new this.CANNON.World,this.world.broadphase=new this.CANNON.NaiveBroadphase,this.world.gravity.set(t.x,t.y,t.z)}beginSync(){this.initialWorldState=this.worldObjects,this.currentInterval=this.Time.setInterval(t=>{if(void 0!==this.lastTime){let e=(t-this.lastTime)/1e3;this.world.step(this.fixedTimeStep,e,this.maxSubSteps),this.objectSync()}this.lastTime=t},this.timeInterval)}objectSync(){this.worldObjects.forEach(t=>{if(t.spark){const e=t.sync,r=e.properties,s=e.axes,o={};t.body.quaternion.toEuler(o),r.forEach(e=>{t.sync[e].enabled&&s.forEach(r=>{if(t.sync[e].allAxis||t.sync[e][r]){const s=`rotation${r.toUpperCase()}`;"position"==e&&(t.spark.transform[r]=t.body[e][r]),"rotation"==e&&(t.spark.transform[s]=o[r])}})})}})}stopSync(){this.currentInterval&&(this.Time.clearInterval(this.currentInterval),this.lastInterval=currentInterval,this.currentInterval=void 0),this.resetWorldState()}getDefaults(t){return t?this.defaults[t]:this.defaults}fillWithDefaults(t){const e=this.getDefaults();return Object.keys(e).forEach(r=>{t.hasOwnProperty(r)||(t[r]=e[r]),Object.keys(e[r]).forEach(s=>{Array.isArray(t[r][s])?e[r][s].forEach(o=>{t[r][s].indexOf(o)<0&&t[r][s].push(e[r][s][o])}):(t[r].hasOwnProperty(s)||(t[r][s]=e[r][s]),"object"!=typeof e[r][s]||Array.isArray(e[r][s])||Object.keys(e[r][s]).forEach(o=>{t[r][s].hasOwnProperty(o)||(t[r][s][o]=e[r][s][o])}))})}),t}resetWorldState(){this.initialWorldState&&this.worldObjects.forEach((t,e)=>{const r=this.initialWorldState[e],s=r.body,o=t.body;if(o.velocity.setZero(),o.initVelocity.setZero(),o.angularVelocity.setZero(),o.initAngularVelocity.setZero(),o.force.setZero(),o.torque.setZero(),o.quaternion=s.quaternion,o.position=s.position,t.spark){const e=t.sync,s=e.properties,o=e.axes,i={};t.body.quaternion.toEuler(i),s.forEach(e=>{t.sync[e].enabled&&o.forEach(s=>{if(t.sync[e].allAxis||t.sync[e][s]){const o=`rotation${s.toUpperCase()}`;"position"==e&&(t.spark.transform[s]=r.spark.transform[s]),"rotation"==e&&(t.spark.transform[o]=r.spark.transform[o])}})})}})}createObject(t,e,r){const{sync:s,body:o}=this.fillWithDefaults(r),{mass:i,radius:n,transform:a}=o,c=a.rotation,l=a.scale,u=a.position,d=new this.CANNON.Vec3(u.x,u.y,u.z),h=new this.CANNON.Quaternion(c.x,c.y,c.z,c.w),f=t=>{switch(t){case"box":return new this.CANNON.Box(new this.CANNON.Vec3(l.x,l.y,l.z));case"sphere":return new this.CANNON.Sphere(n);case"ground":return new this.CANNON.Plane}},y=t=>{const r={mass:i,shape:t,position:d,quaternion:h,angularDamping:.8};return"sphere"==e&&(r.radius=n),"ground"==e&&(r.mass=0),r},p=(t=>{const e=new this.CANNON.Body(y(f(t)));if("ground"==t){const t=-Math.PI/2,r=new this.CANNON.Vec3(1,0,0);e.quaternion.setFromAxisAngle(r,t)}return e})(e);this.addObjectToPhysicsWorld(p,t,s,"ground"==e)}log(t){this.Diagnostics.log(t)}addObjectToPhysicsWorld(t,e,r,s){this.world.addBody(t);const o={body:t,spark:e,sync:{...this.getDefaults("sync"),...r},isGround:s,id:((t,e)=>{for(e=t="";t++<36;e+=51*t&52?(15^t?8^Math.random()*(20^t?16:4):4).toString(16):"-");return e})()};return this.worldObjects.push(o),o}createCannonMaterial(t){let e=void 0;return null==t&&(e=new this.CANNON.Material),e}start(){this.beginSync()}}({x:0,y:-9.82,z:0});a.createObject(n,"ground",{body:{mass:0,transform:{position:{x:0,y:0,z:0}}}}),a.createObject(o,"sphere",{body:{mass:1,radius:.01,transform:{position:{x:0,y:2,z:0}}}}),a.createObject(i,"box",{body:{mass:5,transform:{scale:{x:.01,y:.01,z:.01},position:{x:0,y:10,z:0},rotation:{x:.2,y:.8,z:.3,w:0}}}}),a.start()}]); \ No newline at end of file +!function(t){var e={};function s(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,s),i.l=!0,i.exports}s.m=t,s.c=e,s.d=function(t,e,r){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(s.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)s.d(r,i,function(e){return t[e]}.bind(null,i));return r},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=4)}([function(t,e){t.exports=require("Time")},function(t,e){t.exports=require("cannon")},function(t,e){t.exports=require("Diagnostics")},function(t,e){t.exports=require("Scene")},function(t,e,s){"use strict";s.r(e);const r=s(3),i=r.root.find("SphereObject"),n=r.root.find("Cube01"),o=r.root.find("plane0"),a=new class{constructor(t){this.Diagnostics=void 0,this.Time=void 0,this.currentInterval=void 0,this.lastInterval=void 0,this.lastTime=void 0,this.fixedTimeStep=1/60,this.maxSubSteps=3,this.timeInterval=5,this.C=void 0,this.worldObjects=[],this.worldConstraints=[],this.defaults={body:{mass:0,radius:1,transform:{rotation:{x:0,y:0,z:0,w:.5},position:{x:0,y:0,z:0},scale:{x:1,y:1,z:1}}},sync:{properties:["position","rotation"],axes:["x","y","z"],rotation:{enabled:!0,allAxis:!0,x:!0,y:!0,z:!0},position:{enabled:!0,allAxis:!0,x:!0,y:!0,z:!0}}},this.initRequiredSparkModules(),this.createCWorld(t)}initRequiredSparkModules(){this.Time=s(0),this.C=s(1),this.Diagnostics=s(2)}createCWorld(t){this.world=new this.C.World,this.world.broadphase=new this.C.NaiveBroadphase,this.world.gravity.set(t.x,t.y,t.z)}beginSync(){this.initialWorldState=this.worldObjects,this.currentInterval=this.Time.setInterval(t=>{if(void 0!==this.lastTime){let e=(t-this.lastTime)/1e3;this.world.step(this.fixedTimeStep,e,this.maxSubSteps),this.objectSync()}this.lastTime=t},this.timeInterval)}objectSync(){this.worldObjects.forEach(t=>{if(t.spark){const e=t.sync,s=e.properties,r=e.axes,i={};t.body.quaternion.toEuler(i),s.forEach(e=>{t.sync[e].enabled&&r.forEach(s=>{if(t.sync[e].allAxis||t.sync[e][s]){const r=`rotation${s.toUpperCase()}`;"position"==e&&(t.spark.transform[s]=t.body[e][s]),"rotation"==e&&(t.spark.transform[r]=i[s])}})})}})}stopSync(){this.currentInterval&&(this.Time.clearInterval(this.currentInterval),this.lastInterval=currentInterval,this.currentInterval=void 0),this.resetWorldState()}getDefaults(t){return t?this.defaults[t]:this.defaults}fillWithDefaults(t){const e=this.getDefaults();return Object.keys(e).forEach(s=>{t.hasOwnProperty(s)||(t[s]=e[s]),Object.keys(e[s]).forEach(r=>{Array.isArray(t[s][r])?e[s][r].forEach(i=>{t[s][r].indexOf(i)<0&&t[s][r].push(e[s][r][i])}):(t[s].hasOwnProperty(r)||(t[s][r]=e[s][r]),"object"!=typeof e[s][r]||Array.isArray(e[s][r])||Object.keys(e[s][r]).forEach(i=>{t[s][r].hasOwnProperty(i)||(t[s][r][i]=e[s][r][i])}))})}),t}resetWorldState(){this.initialWorldState&&this.worldObjects.forEach((t,e)=>{const s=this.initialWorldState[e],r=s.body,i=t.body;if(i.velocity.setZero(),i.initVelocity.setZero(),i.angularVelocity.setZero(),i.initAngularVelocity.setZero(),i.force.setZero(),i.torque.setZero(),i.quaternion=r.quaternion,i.position=r.position,t.spark){const e=t.sync,r=e.properties,i=e.axes,n={};t.body.quaternion.toEuler(n),r.forEach(e=>{t.sync[e].enabled&&i.forEach(r=>{if(t.sync[e].allAxis||t.sync[e][r]){const i=`rotation${r.toUpperCase()}`;"position"==e&&(t.spark.transform[r]=s.spark.transform[r]),"rotation"==e&&(t.spark.transform[i]=s.spark.transform[i])}})})}})}createObject(t,e,s){const{sync:r,body:i}=this.fillWithDefaults(s),{mass:n,radius:o,transform:a}=i,c=a.rotation,d=a.scale,l=a.position,h=new this.C.Vec3(l.x,l.y,l.z),u=new this.C.Quaternion(c.x,c.y,c.z,c.w),f=t=>{switch(t){case"box":return new this.C.Box(new this.C.Vec3(d.x,d.y,d.z));case"sphere":return new this.C.Sphere(o);case"ground":return new this.C.Plane}},p=t=>{const s={mass:n,shape:t,position:h,quaternion:u,angularDamping:.8};return"sphere"==e&&(s.radius=o),"ground"==e&&(s.mass=0),s},y=(t=>{const e=new this.C.Body(p(f(t)));if("ground"==t){const t=-Math.PI/2,s=new this.C.Vec3(1,0,0);e.quaternion.setFromAxisAngle(s,t)}return e})(e);return this.addObjectToPhysicsWorld(y,t,r,"ground"==e)}find(t=0,e){return"object"==t?this.worldObjects.find(t=>t.id==e):"constraint"==t?this.worldConstraints.find(t=>t.id==e):void 0}createConstraint(t,e={}){if(!e.bodyA||!e.bodyB)return;let[s,r,i,n,o,a]=e;const c={pivotA:i,pivotB:n,axisA:o,axisB:a},d=this.find("object",s),l=this.find("object",r);if(!d||!l)return;let h;switch(t){case"point":h=new this.C.PointToPointConstraint(s,i,r,n);break;case"hinge":h=new this.C.HingeConstraint(s,r,c);break;case"conetwist":h=new this.C.ConeTwistConstraint(s,r,c);break;case"lock":h=new this.C.LockConstraint(i,n,{});break;default:return}return this.addConstraint(h,d,l)}addConstraint(t,e,s){if(!e.body||!s.body)return;const r=e.body,i=s.body;this.world.addConstraint(t);const n=btoa(`${r.id}.${i.id}`);return this.worldConstraints.push({id:n,constraint:t,isActive:!0}),n}removeConstraint(t,e=!1){const s=this.find("constraint",t);if(s&&this.world.removeConstraint(s.constraint),e){const t=this.worldConstraints.indexOf(s);this.worldConstraints.splice(t,1)}}pauseConstraint(t){const e=this.find("constraint",t);e&&this.removeConstraint(t,!1);const s=this.worldConstraints.indexOf(e);this.worldConstraints[s].isActive=!1}resumeConstraint(t){const e=this.find("constraint",t),s=e.constraint;if(e&&0==e.isActive){this.world.addConstraint(s);const t=this.worldConstraints.indexOf(e);this.worldConstraints[t].isActive=!0}}log(t){this.Diagnostics.log(t)}addObjectToPhysicsWorld(t,e,s,r){this.world.addBody(t);const i={...this.getDefaults("sync"),...s},n=((t,e)=>{for(e=t="";t++<36;e+=51*t&52?(15^t?8^Math.random()*(20^t?16:4):4).toString(16):"-");return e})(),o={id:n,body:t,spark:e,sync:i,isGround:r};return this.worldObjects.push(o),n}createCMaterial(t){let e=void 0;return null==t&&(e=new this.C.Material),e}start(){this.beginSync()}}({x:0,y:-9.82,z:0});a.createObject(o,"ground",{body:{mass:0,transform:{position:{x:0,y:0,z:0}}}});const c=a.createObject(i,"sphere",{body:{mass:1,radius:.01,transform:{position:{x:0,y:2,z:0}}}}),d=a.createObject(n,"box",{body:{mass:5,transform:{scale:{x:.01,y:.01,z:.01},position:{x:0,y:10,z:0},rotation:{x:.2,y:.8,z:.3,w:0}}}});a.addConstraint("point",c,d);a.start()}]); \ No newline at end of file diff --git a/examples/spark/physar_examples/scripts/tsconfig.json b/examples/spark/physar_examples/scripts/tsconfig.json new file mode 100644 index 0000000..6adaedd --- /dev/null +++ b/examples/spark/physar_examples/scripts/tsconfig.json @@ -0,0 +1 @@ +{"compilerOptions":{"baseUrl":"C:/Users/Dilip/AppData/Local/Temp/305780ed8f2897942b891e144bb4e736657/skylight-typedefs","allowJs":true,"checkJs":true,"outDir":"C:/Users/Dilip/AppData/Local/Temp/305780ed8f2897942b891e144bb4e736657/skylight-typedefs/out-dir"},"include":["*.js"]} \ No newline at end of file diff --git a/examples/spark/src/index.js b/examples/spark/src/index.js index cf7fd93..abaf55d 100644 --- a/examples/spark/src/index.js +++ b/examples/spark/src/index.js @@ -62,5 +62,5 @@ physar.createObject(plane, 'ground', groundProps) const sphereID = physar.createObject(sphere, 'sphere', sphereProps) const boxID = physar.createObject(cube, 'box', cubeProps) -const constraint = physar.addConstrait(sphereID, boxID) +const constraint = physar.addConstraint('point', sphereID, boxID) physar.start() diff --git a/src/index.js b/src/index.js index 10b47c1..f6e8038 100644 --- a/src/index.js +++ b/src/index.js @@ -404,7 +404,7 @@ export default class Physar { resumeConstraint(id) { const c = this.find('constraint', id) - const [id, constraint] = c + const constraint = c.constraint if(c && c.isActive == false) {