From 0a0ceb8f2846c13c16839caa38a1ab849fb871f1 Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Mon, 3 Jun 2024 08:47:56 +0200 Subject: [PATCH] throw-on-drop partial examples (working but does not take time into account) --- index.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/index.html b/index.html index 3ee32c0..b2d1ef8 100644 --- a/index.html +++ b/index.html @@ -115,6 +115,41 @@ function showOnlyThisGame(name){ } //___________________________________________________________________________________________________________________________________ +AFRAME.registerComponent('throw-on-drop', { + init: function(){ + let generatorName = this.attrName + let el = this.el + + AFRAME.scenes[0].setAttribute("physics", "debug:true") + el.setAttribute("onpicked", 'window.pfp = e.detail.element.getAttribute("position").clone(); e.detail.element.removeAttribute("dynamic-body")') + // could also start an interval instead + el.setAttribute("onreleased", 'toss( e.detail.element )') + } +}) + +function toss(el, forcedvec){ + // time as force multiplier, i.e .1m/s vs .1m/10s + // need to have another variable storing the time when window.pfp changed + //AFRAME.scenes[0].setAttribute("physics", "debug:true") + el.setAttribute("dynamic-body", "") + //el.body.applyImpulse( forcedvec, new CANNON.Vec3().copy(el.getAttribute('position')) ) + //el.body.applyForce( forcedvec, new CANNON.Vec3().copy(el.getAttribute('position')) ) + // tested via toss( document.querySelector("[throw-on-drop]"), new THREE.Vector3(0,1,-1) ) + let pos = new THREE.Vector3().copy( el.getAttribute('position') ) + pos.sub( window.pfp ) + let vec = new CANNON.Vec3().copy( pos ) + console.log("sub", vec ) // probably have to be normalized + //el.body.applyForce( vec, new CANNON.Vec3().copy(el.getAttribute('position')) ) + el.body.applyImpulse( vec, new CANNON.Vec3().copy(el.getAttribute('position')) ) + // cf https://schteppe.github.io/cannon.js/docs/classes/Body.html#method_applyImpulse + // could try alternatives, e.g applyForce () + // kind of works... but no notion of power inherited from speed + // also the direction is from start from finish, which is not what others expect + // except if shown, e.g with helper arrow + // could clone and preview movement at interval + // does not move if we pick and release in place +} + /* physics https://github.com/c-frame/aframe-physics-system and setup docs https://github.com/c-frame/aframe-physics-system/blob/master/CannonDriver.md#installation should append to head script with src="https://cdn.jsdelivr.net/gh/c-frame/aframe-physics-system@v4.2.2/dist/aframe-physics-system.min.js" @@ -1043,6 +1078,15 @@ S1111 + + + +