A photo of Max Glenister Max Glenister

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.

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.

Cardboctober

View this Cardboctober hack 27: Moving with gaze

View the other submissions for day 27 on the Cardboctober website.

To see the latest Cardboctober hacks, follow @cardboctober on Twitter

Check out my other Cardoctober posts here: /cardboctober

About the author

A photo of Max Glenister

Max Glenister

Max Glenister is a Front-end Developer based in Oxfordshire. For work he spends his time designing, validating and implementing user interfaces. For fun he tinkers with Virtual Reality, 3D printing, embedded systems, game development and many other things.

You can keep up with Max on Github, Twitter and Reddit