|
| 1 | +import nipplejs from "nipplejs"; |
| 2 | +import styles from "./virtual-gamepad-controls.css"; |
| 3 | + |
| 4 | +const THREE = AFRAME.THREE; |
| 5 | +const DEGREES = Math.PI / 180; |
| 6 | +const HALF_PI = Math.PI / 2; |
| 7 | + |
| 8 | +AFRAME.registerComponent("virtual-gamepad-controls", { |
| 9 | + schema: { |
| 10 | + movementSpeed: { default: 2 }, |
| 11 | + lookSpeed: { default: 60 } |
| 12 | + }, |
| 13 | + |
| 14 | + init() { |
| 15 | + // Setup gamepad elements |
| 16 | + const leftTouchZone = document.createElement("div"); |
| 17 | + leftTouchZone.classList.add(styles.touchZone, styles.left); |
| 18 | + document.body.appendChild(leftTouchZone); |
| 19 | + |
| 20 | + const rightTouchZone = document.createElement("div"); |
| 21 | + rightTouchZone.classList.add(styles.touchZone, styles.right); |
| 22 | + document.body.appendChild(rightTouchZone); |
| 23 | + |
| 24 | + const leftStick = nipplejs.create({ |
| 25 | + zone: leftTouchZone, |
| 26 | + mode: "static", |
| 27 | + color: "white", |
| 28 | + position: { left: "50px", bottom: "50px" } |
| 29 | + }); |
| 30 | + |
| 31 | + const rightStick = nipplejs.create({ |
| 32 | + zone: rightTouchZone, |
| 33 | + mode: "static", |
| 34 | + color: "white", |
| 35 | + position: { right: "50px", bottom: "50px" } |
| 36 | + }); |
| 37 | + |
| 38 | + this.onJoystickChanged = this.onJoystickChanged.bind(this); |
| 39 | + |
| 40 | + rightStick.on("move end", this.onJoystickChanged); |
| 41 | + leftStick.on("move end", this.onJoystickChanged); |
| 42 | + |
| 43 | + this.leftTouchZone = leftTouchZone; |
| 44 | + this.rightTouchZone = rightTouchZone; |
| 45 | + this.leftStick = leftStick; |
| 46 | + this.rightStick = rightStick; |
| 47 | + |
| 48 | + // Define initial state |
| 49 | + this.velocity = new THREE.Vector3(); |
| 50 | + this.yaw = 0; |
| 51 | + |
| 52 | + // Allocate matrices and vectors |
| 53 | + this.move = new THREE.Matrix4(); |
| 54 | + this.trans = new THREE.Matrix4(); |
| 55 | + this.transInv = new THREE.Matrix4(); |
| 56 | + this.pivotPos = new THREE.Vector3(); |
| 57 | + this.rotationAxis = new THREE.Vector3(0, 1, 0); |
| 58 | + this.yawMatrix = new THREE.Matrix4(); |
| 59 | + this.rotationMatrix = new THREE.Matrix4(); |
| 60 | + this.rotationInvMatrix = new THREE.Matrix4(); |
| 61 | + this.camRotationMatrix = new THREE.Matrix4(); |
| 62 | + this.camRotationInvMatrix = new THREE.Matrix4(); |
| 63 | + |
| 64 | + this.cameraEl = document.querySelector("[camera]"); |
| 65 | + |
| 66 | + this.onEnterVr = this.onEnterVr.bind(this); |
| 67 | + this.onExitVr = this.onExitVr.bind(this); |
| 68 | + this.el.sceneEl.addEventListener("enter-vr", this.onEnterVr); |
| 69 | + this.el.sceneEl.addEventListener("exit-vr", this.onExitVr); |
| 70 | + }, |
| 71 | + |
| 72 | + onJoystickChanged(event, joystick) { |
| 73 | + if (event.target.id === this.leftStick.id) { |
| 74 | + if (event.type === "move") { |
| 75 | + // Set velocity vector on left stick move |
| 76 | + const angle = joystick.angle.radian; |
| 77 | + const force = joystick.force < 1 ? joystick.force : 1; |
| 78 | + const x = Math.cos(angle) * force; |
| 79 | + const z = Math.sin(angle) * -force; |
| 80 | + this.velocity.set(x, 0, z); |
| 81 | + } else { |
| 82 | + this.velocity.set(0, 0, 0); |
| 83 | + } |
| 84 | + } else { |
| 85 | + if (event.type === "move") { |
| 86 | + // Set yaw angle on right stick move |
| 87 | + const angle = joystick.angle.radian; |
| 88 | + const force = joystick.force < 1 ? joystick.force : 1; |
| 89 | + this.yaw = Math.cos(angle) * -force; |
| 90 | + } else { |
| 91 | + this.yaw = 0; |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + |
| 96 | + tick(t, dt) { |
| 97 | + const deltaSeconds = dt / 1000; |
| 98 | + const lookSpeed = THREE.Math.DEG2RAD * this.data.lookSpeed * deltaSeconds; |
| 99 | + const obj = this.el.object3D; |
| 100 | + const pivot = this.cameraEl.object3D; |
| 101 | + const distance = this.data.movementSpeed * deltaSeconds; |
| 102 | + |
| 103 | + this.pivotPos.copy(pivot.position); |
| 104 | + this.pivotPos.applyMatrix4(obj.matrix); |
| 105 | + this.trans.setPosition(this.pivotPos); |
| 106 | + this.transInv.makeTranslation( |
| 107 | + -this.pivotPos.x, |
| 108 | + -this.pivotPos.y, |
| 109 | + -this.pivotPos.z |
| 110 | + ); |
| 111 | + this.rotationMatrix.makeRotationAxis(this.rotationAxis, obj.rotation.y); |
| 112 | + this.rotationInvMatrix.makeRotationAxis(this.rotationAxis, -obj.rotation.y); |
| 113 | + this.camRotationMatrix.makeRotationAxis( |
| 114 | + this.rotationAxis, |
| 115 | + pivot.rotation.y |
| 116 | + ); |
| 117 | + this.camRotationInvMatrix.makeRotationAxis( |
| 118 | + this.rotationAxis, |
| 119 | + -pivot.rotation.y |
| 120 | + ); |
| 121 | + this.move.makeTranslation( |
| 122 | + this.velocity.x * distance, |
| 123 | + this.velocity.y * distance, |
| 124 | + this.velocity.z * distance |
| 125 | + ); |
| 126 | + |
| 127 | + this.yawMatrix.makeRotationAxis(this.rotationAxis, lookSpeed * this.yaw); |
| 128 | + |
| 129 | + // Translate to middle of playspace (player rig) |
| 130 | + obj.applyMatrix(this.transInv); |
| 131 | + // Zero playspace (player rig) rotation |
| 132 | + obj.applyMatrix(this.rotationInvMatrix); |
| 133 | + // Zero camera (head) rotation |
| 134 | + obj.applyMatrix(this.camRotationInvMatrix); |
| 135 | + // Apply joystick translation |
| 136 | + obj.applyMatrix(this.move); |
| 137 | + // Apply joystick yaw rotation |
| 138 | + obj.applyMatrix(this.yawMatrix); |
| 139 | + // Reapply camera (head) rotation |
| 140 | + obj.applyMatrix(this.camRotationMatrix); |
| 141 | + // Reapply playspace (player rig) rotation |
| 142 | + obj.applyMatrix(this.rotationMatrix); |
| 143 | + // Reapply playspace (player rig) translation |
| 144 | + obj.applyMatrix(this.trans); |
| 145 | + |
| 146 | + this.el.setAttribute("rotation", { |
| 147 | + x: obj.rotation.x * THREE.Math.RAD2DEG, |
| 148 | + y: obj.rotation.y * THREE.Math.RAD2DEG, |
| 149 | + z: obj.rotation.z * THREE.Math.RAD2DEG |
| 150 | + }); |
| 151 | + this.el.setAttribute("position", obj.position); |
| 152 | + }, |
| 153 | + |
| 154 | + onEnterVr() { |
| 155 | + // Hide the joystick controls |
| 156 | + this.leftTouchZone.style.display = "none"; |
| 157 | + this.rightTouchZone.style.display = "none"; |
| 158 | + }, |
| 159 | + |
| 160 | + onExitVr() { |
| 161 | + // Show the joystick controls |
| 162 | + this.leftTouchZone.style.display = "block"; |
| 163 | + this.rightTouchZone.style.display = "block"; |
| 164 | + }, |
| 165 | + |
| 166 | + remove() { |
| 167 | + this.el.sceneEl.removeEventListener("entervr", this.onEnterVr); |
| 168 | + this.el.sceneEl.removeEventListener("exitvr", this.onExitVr); |
| 169 | + document.body.removeChild(this.leftTouchZone); |
| 170 | + document.body.removeChild(this.rightTouchZone); |
| 171 | + } |
| 172 | +}); |
0 commit comments