27: Moving with gaze ================================================================================ Today’s hack is more an aside while I’m focussing on yet another Tevris rewrite. I thought I’d play with an idea for controlling the left/right movement of the active piece based on where you’re looking. Now, knowing where you’re looking in 3D space is both a hard thing to Google, and a hard thing to explain. So take this solution with a grain of salt: var getpos = function (obj) { camera.matrixWorldInverse.getInverse( camera.matrixWorld ); var mat = new THREE.Matrix4().multiplyMatrices( camera.matrixWorldInverse, obj.matrixWorld ); var pos = obj.position.clone().applyProjection(mat); return pos; }; var getLooking = function () { var pos = getpos(board[0][Math.floor(cols/2)]); // MAGIC 💩 if (pos.x - pos.z > 180) { return 'left'; } if (pos.x - pos.z < 120) { return 'right'; } }; There’s a lot going on there, and some MAGIC, and it’s not going to scale, but it sort of works. [IMAGE: https://blog.omgmog.net/images/2016-10/27/giphy.gif] I’m not completely happy with this, so you may have noticed the poop emoji (💩) all over the place. I’ll definitely throw this away with the re-write I’m working on, but it’s nice to explore the idea. ================================================================================ Published October 27, 2016 Generated from the original post: https://blog.omgmog.net/cardboctober/cardboctober-27/ Max Glenister is an interface designer and senior full-stack developer from Oxfordshire. He writes mostly about front-end development and technology. - Mastodon: https://indieweb.social/@omgmog - Github: https://github.com/omgmog - Reddit: https://reddit.com/u/omgmog - Discord: https://discordapp.com/users/omgmog#6206