aframe-physics-extras
Version:
Cannon API interface components the A-Frame Physics System.
17 lines (11 loc) • 6.58 kB
JavaScript
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';if('undefined'==typeof AFRAME)throw new Error('Component attempted to register before AFRAME was available.');require('./src/physics-collider.js'),require('./src/physics-collision-filter.js'),require('./src/physics-sleepy.js'),require('./src/body-merger.js');
},{"./src/body-merger.js":2,"./src/physics-collider.js":3,"./src/physics-collision-filter.js":4,"./src/physics-sleepy.js":5}],2:[function(require,module,exports){
'use strict';AFRAME.registerComponent('body-merger',{schema:{default:'static-body'},init:function(){const a=(b)=>{b.target===this.el&&(this.el.removeEventListener('body-loaded',a),this.merge())};this.el.body?this.merge():this.el.addEventListener('body-loaded',a)},merge:function(){const a=this.el.body,b=new THREE.Matrix4,c=new THREE.Quaternion,d=new THREE.Vector3,e=new THREE.Vector3(1,1,1),f=new CANNON.Vec3,g=new CANNON.Quaternion;for(let h of this.el.childNodes)if(h.body&&h.getAttribute(this.data)){for(h.object3D.updateMatrix();h.body.shapes.length;)d.copy(h.body.shapeOffsets.pop()),c.copy(h.body.shapeOrientations.pop()),b.compose(d,c,e),b.multiply(h.object3D.matrix),b.decompose(d,c,e),f.copy(d),g.copy(c),a.addShape(h.body.shapes.pop(),f,g);h.removeAttribute(this.data)}}});
},{}],3:[function(require,module,exports){
'use strict';AFRAME.registerComponent('physics-collider',{schema:{ignoreSleep:{default:!0}},init:function(){this.collisions=new Set,this.currentCollisions=new Set,this.newCollisions=[],this.clearedCollisions=[],this.collisionEventDetails={els:this.newCollisions,clearedEls:this.clearedCollisions}},update:function(){this.el.body?this.updateBody():this.el.addEventListener('body-loaded',this.updateBody.bind(this),{once:!0})},tick:function(){const a=4294901760,b=65535;return function(){if(!this.el.body)return;const c=this.currentCollisions,d=this.el.body.id,e=this.el.body.world.bodyOverlapKeeper.current,f=this.el.body.world.idToBodyMap,g=this.collisions,h=this.newCollisions,j=this.clearedCollisions;let k,l=0,i=(e[l]&a)>>16;for(h.length=j.length=0,c.clear();l<e.length&&i<d;)f[i]&&(k=f[i].el,(e[l]&b)===d&&(c.add(k),!g.has(k)&&h.push(k))),i=(e[++l]&a)>>16;for(;l<e.length&&i===d;)f[e[l]&b]&&(k=f[e[l]&b].el,c.add(k),!g.has(k)&&h.push(k)),i=(e[++l]&a)>>16;for(let a of g)c.has(a)||(j.push(a),g.delete(a));for(let a of h)g.add(a);(h.length||j.length)&&this.el.emit('collisions',this.collisionEventDetails)}}(),remove:function(){this.originalSleepConfig&&AFRAME.utils.extend(this.el.body,this.originalSleepConfig)},updateBody:function(a){void 0!==a&&a.target!==this.el||(this.data.ignoreSleep?(this.el.body.allowSleep=!1,this.el.body.type=window.CANNON.Body.KINEMATIC,this.el.body.sleepSpeedLimit=0):this.originalSleepConfig===void 0?this.originalSleepConfig={allowSleep:this.el.body.allowSleep,sleepSpeedLimit:this.el.body.sleepSpeedLimit,type:this.el.body.type}:AFRAME.utils.extend(this.el.body,this.originalSleepConfig))}});
},{}],4:[function(require,module,exports){
'use strict';AFRAME.registerComponent('collision-filter',{schema:{group:{default:'default'},collidesWith:{default:['default']},collisionForces:{default:!0}},init:function(){this.updateBodyBound=this.updateBody.bind(this),this.system.registerMe(this),this.el.addEventListener('body-loaded',this.updateBodyBound)},update:function(){this.system.registerMe(this),this.el.body&&this.updateBody()},remove:function(){this.el.removeEventListener('body-loaded',this.updateBodyBound)},updateBody:function(a){void 0!==a&&a.target!==this.el||(this.el.body.collisionFilterMask=this.system.getFilterCode(this.data.collidesWith),this.el.body.collisionFilterGroup=this.system.getFilterCode(this.data.group),this.el.body.collisionResponse=this.data.collisionForces)}}),AFRAME.registerSystem('collision-filter',{schema:{collisionGroups:{default:['default']}},dependencies:['physics'],init:function(){this.maxGroups=Math.log2(Number.MAX_SAFE_INTEGER)},registerMe:function(a){const b=[a.data.group,...a.data.collidesWith].filter((a)=>-1===this.data.collisionGroups.indexOf(a));if(this.data.collisionGroups.push(...b),this.data.collisionGroups.length>this.maxGroups)throw new Error('Too many collision groups')},getFilterCode:function(a){let b=0;return Array.isArray(a)||(a=[a]),a.forEach((a)=>{b+=Math.floor(Math.pow(2,this.data.collisionGroups.indexOf(a)))}),b}});
},{}],5:[function(require,module,exports){
'use strict';AFRAME.registerComponent('sleepy',{schema:{allowSleep:{default:!0},speedLimit:{default:0.25,type:'number'},delay:{default:0.25,type:'number'},linearDamping:{default:0.99,type:'number'},angularDamping:{default:0.99,type:'number'},holdState:{default:'grabbed'}},init:function(){this.updateBodyBound=this.updateBody.bind(this),this.holdStateBound=this.holdState.bind(this),this.resumeStateBound=this.resumeState.bind(this),this.el.addEventListener('body-loaded',this.updateBodyBound)},update:function(){this.el.body&&this.updateBody()},remove:function(){this.el.removeEventListener('body-loaded',this.updateBodyBound),this.el.removeEventListener('stateadded',this.holdStateBound),this.el.removeEventListener('stateremoved',this.resumeStateBound)},updateBody:function(a){if(void 0===a||a.target===this.el){if(this.data.allowSleep)try{this.el.body.world.allowSleep=!0}catch(a){console.error('Unable to activate sleep in physics.`sleepy` requires "local" physics driver')}this.el.body.allowSleep=this.data.allowSleep,this.el.body.sleepSpeedLimit=this.data.speedLimit,this.el.body.sleepTimeLimit=this.data.delay,this.el.body.linearDamping=this.data.linearDamping,this.el.body.angularDamping=this.data.angularDamping,this.data.allowSleep?(this.el.addEventListener('stateadded',this.holdStateBound),this.el.addEventListener('stateremoved',this.resumeStateBound)):(this.el.removeEventListener('stateadded',this.holdStateBound),this.el.removeEventListener('stateremoved',this.resumeStateBound))}},holdState:function(a){let b=this.data.holdState;(a.detail===b||a.detail.state===b)&&(this.el.body.allowSleep=!1)},resumeState:function(a){let b=this.data.holdState;(a.detail===b||a.detail.state===b)&&(this.el.body.allowSleep=this.data.allowSleep)}});
},{}]},{},[1]);