Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Create SmoothControls.js

  • Loading branch information...
commit 47bab3d5e4a54c626450fa3e9e8f116b56f5b3c5 1 parent 1267de5
Clark A authored

Showing 1 changed file with 126 additions and 0 deletions. Show diff stats Hide diff stats

  1. +126 0 SmoothControls.js
126 SmoothControls.js
... ... @@ -0,0 +1,126 @@
  1 +THREE.SmoothControls = function ( object ) {
  2 +
  3 + //public
  4 + this.enabled = true;
  5 + this.movementSpeed = 1.0;
  6 + this.lookSpeed = 0.005;
  7 +
  8 + //private
  9 + this.moveForward = false;
  10 + this.moveBackward = false;
  11 + this.moveLeft = false;
  12 + this.moveRight = false;
  13 + this.object = object;
  14 + this.target = new THREE.Vector3( 0, 0, 0 );
  15 + this.movementX = 0;
  16 + this.movementY = 0;
  17 + this.lat = 0;
  18 + this.lon = 0;
  19 + this.phi = 0;
  20 + this.theta = 0;
  21 +
  22 + this.update = function( delta ) {
  23 +
  24 + if ( !this.enabled ) {
  25 + return;
  26 + }
  27 +
  28 + var actualMoveSpeed = delta * this.movementSpeed;
  29 + var actualLookSpeed = delta * this.lookSpeed;
  30 +
  31 + if ( this.moveForward ) this.object.translateZ( - ( actualMoveSpeed ) );
  32 + if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
  33 +
  34 + if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
  35 + if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
  36 +
  37 + if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
  38 + if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
  39 +
  40 + this.lon += (20* this.movementX) * actualLookSpeed;
  41 + this.lat -= (20* this.movementY) * actualLookSpeed;// * verticalLookRatio;
  42 + this.movementX = 0;
  43 + this.movementY = 0;
  44 +
  45 + this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  46 + this.phi = ( 90 - this.lat ) * Math.PI / 180;
  47 + this.theta = this.lon * Math.PI / 180;
  48 +
  49 + this.target.x = this.object.position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  50 + this.target.y = this.object.position.y + 100 * Math.cos( this.phi );
  51 + this.target.z = this.object.position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  52 +
  53 + this.object.lookAt( this.target );
  54 + };
  55 +
  56 + this.onMouseMove = function ( event ) {
  57 + this.movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  58 + this.movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  59 + };
  60 +
  61 + this.onMouseDown = function ( event ) {
  62 + event.preventDefault();
  63 + event.stopPropagation();
  64 + switch ( event.button ) {
  65 + case 0: this.moveForward = true; break;
  66 + case 2: this.moveBackward = true; break;
  67 + }
  68 + };
  69 +
  70 + this.onMouseUp = function ( event ) {
  71 + event.preventDefault();
  72 + event.stopPropagation();
  73 + switch ( event.button ) {
  74 + case 0: this.moveForward = false; break;
  75 + case 2: this.moveBackward = false; break;
  76 + }
  77 + };
  78 +
  79 + this.onKeyDown = function ( event ) {
  80 + switch ( event.keyCode ) {
  81 + case 38: /*up*/
  82 + case 87: /*W*/ this.moveForward = true; break;
  83 +
  84 + case 37: /*left*/
  85 + case 65: /*A*/ this.moveLeft = true; break;
  86 +
  87 + case 40: /*down*/
  88 + case 83: /*S*/ this.moveBackward = true; break;
  89 +
  90 + case 39: /*right*/
  91 + case 68: /*D*/ this.moveRight = true; break;
  92 +
  93 + case 82: /*R*/ this.moveUp = true; break;
  94 + case 70: /*F*/ this.moveDown = true; break;
  95 +
  96 + case 81: /*Q*/ this.freeze = !this.freeze; break;
  97 + }
  98 + };
  99 +
  100 + this.onKeyUp = function ( event ) {
  101 + switch( event.keyCode ) {
  102 + case 38: /*up*/
  103 + case 87: /*W*/ this.moveForward = false; break;
  104 +
  105 + case 37: /*left*/
  106 + case 65: /*A*/ this.moveLeft = false; break;
  107 +
  108 + case 40: /*down*/
  109 + case 83: /*S*/ this.moveBackward = false; break;
  110 +
  111 + case 39: /*right*/
  112 + case 68: /*D*/ this.moveRight = false; break;
  113 +
  114 + case 82: /*R*/ this.moveUp = false; break;
  115 + case 70: /*F*/ this.moveDown = false; break;
  116 + }
  117 + };
  118 +
  119 + document.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  120 + document.addEventListener( 'mousemove', this.onMouseMove.bind(this), false );
  121 + document.addEventListener( 'mousedown', this.onMouseDown.bind(this), false );
  122 + document.addEventListener( 'mouseup', this.onMouseUp.bind(this), false );
  123 + document.addEventListener( 'keydown', this.onKeyDown.bind(this), false );
  124 + document.addEventListener( 'keyup', this.onKeyUp.bind(this), false );
  125 +
  126 +};

0 comments on commit 47bab3d

Please sign in to comment.
Something went wrong with that request. Please try again.