Example #1
0
  update(dt) {
    super.update(dt);

    if (this.currentDevice) {
      var pose = this.currentPose || this.lastPose || DEFAULT_POSE;
      this.lastPose = pose;
      this.inPhysicalUse = this.hasOrientation || this.inPhysicalUse;
      var orient = this.currentPose && this.currentPose.orientation,
        pos = this.currentPose && this.currentPose.position;
      if (orient) {
        this.poseQuaternion.fromArray(orient);
        if(isMobile && isIE){
          this.poseQuaternion.multiply(IE_CORRECTION);
        }
      }
      else {
        this.poseQuaternion.set(0, 0, 0, 1);
      }
      if (pos) {
        this.posePosition.fromArray(pos);
      }
      else {
        this.posePosition.set(0, 0, 0);
      }
    }
  }
Example #2
0
  update( dt ) {
    this.velocity.addScaledVector( this.acceleration, dt );
    this.position.addScaledVector( this.velocity, dt );

    vector.copy( this.angularVelocity ).multiplyScalar( dt / 2 );
    quaternion.set( vector.x, vector.y, vector.z, 1 ).normalize();
    this.quaternion.multiply( quaternion );
  }
Example #3
0
	this.getYawPitchRollFromDeviceQuaternion = function (quaternion) {

		var deviceQuaternion = new THREE.Quaternion();
		var screenTransform = new THREE.Quaternion();
		var worldTransform = new THREE.Quaternion(0, Math.sqrt(0.5), 0, Math.sqrt(0.5));

		deviceQuaternion.set(quaternion.x, quaternion.y, quaternion.z, quaternion.w);

		var minusHalfAngle = -45; //-90/2

		screenTransform.set(0, Math.sin(minusHalfAngle), 0, Math.cos(minusHalfAngle));

		//deviceQuaternion.multiply( screenTransform );

		deviceQuaternion.multiply(worldTransform);

		return this.getYawPitchRollFromQuaternion(deviceQuaternion);
	};
Example #4
0
  orbitYawPitch(x, y) {
    // the most you can drag is 33% of the total rotation allowed
    const radiansPerPixel = 1500; // 1208
    const yawValue = -(this.dragStart.x - x) / radiansPerPixel;
    const pitchValue = -(this.dragStart.y - y) / radiansPerPixel;

    this.dragStart.set(x, y);

    // Apply pitch value
    this.rotationVector.x = pitchValue;
    this.rotationVector.y = yawValue;

    this.tmpQuaternion.set(this.rotationVector.x, 0, 0, 1).normalize();
    const camera = this.object.children[0];
    camera.quaternion.multiply(this.tmpQuaternion);
    camera.quaternion.x = THREE.Math.clamp(camera.quaternion.x, -0.7, 2.6);
    camera.quaternion.w = 1;

    this.tmpQuaternion.set(0, this.rotationVector.y, 0, 1).normalize();
    this.object.quaternion.multiply(this.tmpQuaternion);
  }
Example #5
0
	return function( message, data ) {

		// get correct teammate
		var teammate = self._getTeammate( data.clientId );

		// process position and orientation
		position.set( data.player.position.x, data.player.position.y, data.player.position.z );
		quaternion.set( data.player.quaternion._x, data.player.quaternion._y, data.player.quaternion._z, data.player.quaternion._w );

		// update teammate
		teammate.update( position, quaternion );
	};
Example #6
0
		return function (alpha, beta, gamma, screenOrientation) {

			deviceEuler.set(beta, alpha,  - gamma, 'YXZ');

			finalQuaternion.setFromEuler(deviceEuler);

			minusHalfAngle =  - screenOrientation / 2;

			screenTransform.set(0, Math.sin(minusHalfAngle), 0, Math.cos(minusHalfAngle));

			finalQuaternion.multiply(screenTransform);

			finalQuaternion.multiply(worldTransform);

			return finalQuaternion;

		}
  /**
   * Return an array containing the vector components of the controller's
   * current orientation, which is used as the direction of the ray.
   * Since the controller rotation is not relative to the camera, we multiply
   * by the inverse of the camera's quaternion.
   */
  getRayDirection(camera) {
    if (!this._active || this._gamepadIndex < 0) {
      return null;
    }
    const gamepad = this._getGamepad();
    if (gamepad && gamepad.pose && gamepad.pose.orientation) {
      // Rotate <0, 0, -1> by the controller pose quaternion
      this._vector.set(0, 0, -1);
      const orientation = gamepad.pose.orientation;
      this._controllerQuaternion.set(orientation[0], orientation[1], orientation[2], orientation[3]);
      this._cameraQuaternion.copy(camera.quaternion);
      this._cameraQuaternion.inverse();

      this._vector.applyQuaternion(this._controllerQuaternion);
      this._vector.applyQuaternion(this._cameraQuaternion);
      return [this._vector.x, this._vector.y, this._vector.z];
    }
  }
Example #8
0
  update() {
    this.position.set(0, 0, 0);

    if(this.unproject) {
      QUAT_TEMP.set(0, 1, 0, 0);
      VECTOR_TEMP.set(0, 0, 0);
      for(let i = 0; i < this.devices.length; ++i) {
        const obj = this.devices[i];
        if(obj.enabled && !obj.commands.U.disabled && !obj.commands.V.disabled) {
          VECTOR_TEMP.x += obj.getValue("U") - 1;
          VECTOR_TEMP.y += obj.getValue("V") - 1;
        }
      }
      VECTOR_TEMP.applyMatrix4(this.unproject)
        .applyQuaternion(QUAT_TEMP);
      this.lookAt(VECTOR_TEMP);
    }
    else {
      this.quaternion.set(0, 0, 0, 1);
      EULER_TEMP.set(0, 0, 0, "YXZ");
      for(let i = 0; i < this.devices.length; ++i) {
        const obj = this.devices[i];
        if(obj.enabled) {
          if(obj.quaternion) {
            this.quaternion.multiply(obj.quaternion);
          }
          if(obj.position) {
            this.position.add(obj.position);
          }
        }
      }

      QUAT_TEMP.setFromEuler(EULER_TEMP);
      this.quaternion.multiply(QUAT_TEMP);
    }
    this.updateMatrixWorld();
  }
Example #9
0
  update(delta) {
    const moveMult = delta * this.movementSpeed;
    const rotMult = delta * this.rollSpeed;

    if (typeof this.movementBounds !== 'undefined') {
      const x = this.object.position.x + (this.moveVector.x * moveMult);
      const y = this.object.position.y + (this.moveVector.y * moveMult);
      const z = this.object.position.z + (this.moveVector.z * moveMult);

      const newPosition = new THREE.Vector3(x, y, z);

      if (this.movementBounds.distanceToPoint(newPosition) === 0) {
        this.object.translateX(this.moveVector.x * moveMult);
        this.object.translateY(this.moveVector.y * moveMult);
        this.object.translateZ(this.moveVector.z * moveMult);
      }

      // if (x >= this.movementBounds.min.x && x <= this.movementBounds.max.x) {
      //   this.object.translateX(this.moveVector.x * moveMult);
      // }
      // if (y >= this.movementBounds.min.y && y <= this.movementBounds.max.y) {
      //   this.object.translateY(this.moveVector.y * moveMult);
      // }
      // if (z >= this.movementBounds.min.z && z <= this.movementBounds.max.z) {
      //   this.object.translateZ(this.moveVector.z * moveMult);
      // }
    } else {
      this.object.translateX(this.moveVector.x * moveMult);
      this.object.translateY(this.moveVector.y * moveMult);
      this.object.translateZ(this.moveVector.z * moveMult);
    }

    // modified to find a camera in the children of the object - jfox
    if (this.object.children.length > 0) {
      // the camera rotates x and z
      this.tmpQuaternion.set(
        this.rotationVector.x * rotMult,
        0,
        this.rotationVector.z * rotMult,
        1).normalize();
      const camera = this.object.children[0];
      camera.quaternion.multiply(this.tmpQuaternion);
      // rig rotates y
      this.tmpQuaternion.set(0, this.rotationVector.y * rotMult, 0, 1).normalize();
      this.object.quaternion.multiply(this.tmpQuaternion);

      // this.lastDebugTime = this.lastDebugTime || Date.now();
      // if (Date.now() - this.lastDebugTime > 2500) {
      //   console.log('rig:');
      //   console.log(this.object.position);
      //   console.log(this.object.rotation);
      //   console.log('camera:');
      //   console.log(camera.rotation);
      //   this.lastDebugTime = Date.now();

      //   let raycaster = new THREE.Raycaster();
      //   raycaster.set(this.object.position, camera.getWorldDirection())
      //   let intersects = raycaster.intersectObjects(this.scene.children, true);
      //   if (intersects.length > 0) {
      //     console.log(intersects[0]);
      //   }
      // }
    } else {
      this.tmpQuaternion.set(
        this.rotationVector.x * rotMult,
        this.rotationVector.y * rotMult,
        this.rotationVector.z * rotMult,
        1).normalize();
      this.object.quaternion.multiply(this.tmpQuaternion);
    }

    this.resetTemporaryMoveState();
  }
Example #10
0
  step (delta) {
    const wormhole = Simulation.config.wormhole
    const wormholeSphere = new Sphere(wormhole.position, wormhole.radius)

    if (this.velocity.lengthSq() > 0.00001) {
      __prevPosition.copy(this.object.position)

      // 1. Compute wormhole curvature/gravity.
      __gravityVector.subVectors(wormhole.position, __prevPosition)
      const rayDistance = __gravityVector.length() - wormhole.radius * (1 - wormhole.gravityRatio)
      const amount = wormhole.gravityRatio / rayDistance
      __acceleration.copy(__gravityVector.normalize()).multiplyScalar(wormhole.radius * this.velocity.lengthSq() * amount * amount)

      // Apply curvature to velocity
      __newVelocity.copy(this.velocity).add(__acceleration.multiplyScalar(delta))

      // Adjust new velocity (keep magnitude of old velocity)
      __newVelocity.normalize().multiplyScalar(this.velocity.length())

      // Update the player's position and orientation accordingly
      this.object.position.addVectors(__prevPosition, __newVelocity.multiplyScalar(delta))
      this.object.quaternion.multiplyQuaternions(
        __rotation.setFromUnitVectors(this.velocity.normalize(), __newVelocity.normalize()),
        this.object.quaternion
      )

      this.velocity.copy(__newVelocity)

      // 2. Check if we're going through the wormhole
      __direction.copy(this.velocity).normalize()

      __ray.set(__prevPosition, __direction)

      const distanceTravelledSq = __direction.subVectors(this.object.position, __prevPosition).lengthSq()

      const at = __ray.intersectSphere(wormholeSphere, __intersection)
      if (at && at.distanceToSquared(__prevPosition) <= distanceTravelledSq) {
        // Rotate 180 degrees around __axis pointing at exit point
        __axis.subVectors(__intersection, wormhole.position).normalize()
        __rotation.setFromAxisAngle(__axis, Math.PI)
        this.object.quaternion.multiplyQuaternions(__rotation, this.object.quaternion)
        this.velocity.reflect(__axis).multiplyScalar(-1)

        // Set new position a tiny bit outside mirrored __intersection point
        this.object.position
          .copy(wormhole.position)
          .add(
            __temp.subVectors(wormhole.position, __intersection)
              .multiplyScalar(1.0001)
          )

        this.galaxy = 1 - this.galaxy
      }
    }

    __rotation.set(
      this.eyeAngularVelocity.x * delta,
      this.eyeAngularVelocity.y * delta,
      this.eyeAngularVelocity.z * delta,
      1
    ).normalize()
    this.eyes.quaternion.multiply(__rotation)
  }