Last active
November 6, 2016 22:41
-
-
Save vthibault/9d5c08c111db2eabfc37 to your computer and use it in GitHub Desktop.
roBrowser - Keyboard support to move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependencies | |
var glMatrix = require('Vendors/gl-matrix'); | |
var Camera = require('Renderer/Camera'); | |
var vec2 = glMatrix.vec2; | |
var mat2 = glMatrix.mat2; | |
// Object to initialize | |
var direction = vec2.create(); | |
var rotate = mat2.create(); | |
//---- Now the job ---- | |
function processKeyEvent( event ) { | |
// Get direction from keyboard | |
direction[0] = (event.which === KEYS.RIGHT ? -1 : | |
event.which === KEYS.LEFT ? +1 : | |
0); | |
direction[1] = (event.which === KEYS.UP ? +1 : | |
event.which === KEYS.DOWN ? -1 : | |
0); | |
// Initialize matrix, based on Camera direction | |
mat2.identity(rotate); | |
mat2.rotate(rotate, rotate, -Camera.direction * 45 / 180 * Math.PI); | |
// Apply matrix to vector | |
vec2.transformMat2( direction, direction, rotate); | |
// You now have "direction" containing the direction based on the camera. | |
// (value from -1 to 1). | |
direction; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please accept me forum 'robrowser' email [email protected]