1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288import * as THREE from "three"
import { PointerLockControls } from "three/examples/jsm/controls/PointerLockControls"
// Setup desktop controls (keyboard + mouse)
export function setupDesktopControls(camera: THREE.Camera, domElement: HTMLElement) {
// Create pointer lock controls
const controls = new PointerLockControls(camera, domElement)
// Click to start controls
domElement.addEventListener("click", () => {
controls.lock()
})
// Movement variables
const velocity = new THREE.Vector3()
const direction = new THREE.Vector3()
const moveSpeed = 5.0
// Track key states
const keyState = {
forward: false,
backward: false,
left: false,
right: false,
jump: false,
}
// Key down event
document.addEventListener("keydown", (event) => {
switch (event.code) {
case "ArrowUp":
case "KeyW":
keyState.forward = true
break
case "ArrowDown":
case "KeyS":
keyState.backward = true
break
case "ArrowLeft":
case "KeyA":
keyState.left = true
break
case "ArrowRight":
case "KeyD":
keyState.right = true
break
case "Space":
keyState.jump = true
break
}
})
// Key up event
document.addEventListener("keyup", (event) => {
switch (event.code) {
case "ArrowUp":
case "KeyW":
keyState.forward = false
break
case "ArrowDown":
case "KeyS":
keyState.backward = false
break
case "ArrowLeft":
case "KeyA":
keyState.left = false
break
case "ArrowRight":
case "KeyD":
keyState.right = false
break
case "Space":
keyState.jump = false
break
}
})
// Update function for animation loop
const update = (delta: number) => {
if (controls.isLocked) {
// Calculate velocity based on key states
velocity.x = 0
velocity.z = 0
direction.z = Number(keyState.forward) - Number(keyState.backward)
direction.x = Number(keyState.right) - Number(keyState.left)
direction.normalize()
// Apply movement in the direction the camera is facing
if (keyState.forward || keyState.backward) {
velocity.z -= direction.z * moveSpeed * delta
}
if (keyState.left || keyState.right) {
velocity.x += direction.x * moveSpeed * delta
}
// Apply movement to controls
controls.moveRight(velocity.x)
controls.moveForward(-velocity.z)
// Simple jump (just move up for now)
if (keyState.jump) {
camera.position.y += 0.1
}
}
}
// Add update method to controls
controls.update = update
return controls
}
// Setup mobile controls (touch)
export function setupMobileControls(camera: THREE.Camera, domElement: HTMLElement) {
// Create a container for the joystick
const joystickContainer = document.createElement("div")
joystickContainer.style.position = "absolute"
joystickContainer.style.bottom = "20px"
joystickContainer.style.left = "20px"
joystickContainer.style.width = "100px"
joystickContainer.style.height = "100px"
joystickContainer.style.borderRadius = "50%"
joystickContainer.style.backgroundColor = "rgba(255, 255, 255, 0.2)"
document.body.appendChild(joystickContainer)
// Create the joystick
const joystick = document.createElement("div")
joystick.style.position = "absolute"
joystick.style.top = "25px"
joystick.style.left = "25px"
joystick.style.width = "50px"
joystick.style.height = "50px"
joystick.style.borderRadius = "50%"
joystick.style.backgroundColor = "rgba(255, 255, 255, 0.5)"
joystickContainer.appendChild(joystick)
// Joystick variables
let joystickActive = false
let joystickOrigin = { x: 0, y: 0 }
let joystickPosition = { x: 0, y: 0 }
const maxJoystickDistance = 50
// Camera rotation variables
let touchRotateActive = false
let previousTouchPosition = { x: 0, y: 0 }
const rotationSpeed = 0.005
// Joystick touch start
joystickContainer.addEventListener("touchstart", (event) => {
event.preventDefault()
joystickActive = true
const touch = event.touches[0]
const rect = joystickContainer.getBoundingClientRect()
joystickOrigin = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2,
}
joystickPosition = {
x: touch.clientX - joystickOrigin.x,
y: touch.clientY - joystickOrigin.y,
}
// Limit joystick position
const distance = Math.sqrt(joystickPosition.x ** 2 + joystickPosition.y ** 2)
if (distance > maxJoystickDistance) {
joystickPosition.x = (joystickPosition.x / distance) * maxJoystickDistance
joystickPosition.y = (joystickPosition.y / distance) * maxJoystickDistance
}
// Update joystick position
joystick.style.transform = `translate(${joystickPosition.x}px, ${joystickPosition.y}px)`
})
// Joystick touch move
joystickContainer.addEventListener("touchmove", (event) => {
event.preventDefault()
if (joystickActive) {
const touch = event.touches[0]
joystickPosition = {
x: touch.clientX - joystickOrigin.x,
y: touch.clientY - joystickOrigin.y,
}
// Limit joystick position
const distance = Math.sqrt(joystickPosition.x ** 2 + joystickPosition.y ** 2)
if (distance > maxJoystickDistance) {
joystickPosition.x = (joystickPosition.x / distance) * maxJoystickDistance
joystickPosition.y = (joystickPosition.y / distance) * maxJoystickDistance
}
// Update joystick position
joystick.style.transform = `translate(${joystickPosition.x}px, ${joystickPosition.y}px)`
}
})
// Joystick touch end
joystickContainer.addEventListener("touchend", (event) => {
event.preventDefault()
joystickActive = false
joystickPosition = { x: 0, y: 0 }
joystick.style.transform = "translate(0px, 0px)"
})
// Camera rotation touch start
domElement.addEventListener("touchstart", (event) => {
// Only handle touches outside the joystick container
if (event.target !== joystickContainer && event.target !== joystick) {
touchRotateActive = true
previousTouchPosition = {
x: event.touches[0].clientX,
y: event.touches[0].clientY,
}
}
})
// Camera rotation touch move
domElement.addEventListener("touchmove", (event) => {
if (touchRotateActive) {
const touch = event.touches[0]
const deltaX = touch.clientX - previousTouchPosition.x
const deltaY = touch.clientY - previousTouchPosition.y
// Rotate camera based on touch movement
camera.rotation.y -= deltaX * rotationSpeed
camera.rotation.x -= deltaY * rotationSpeed
// Limit vertical rotation
camera.rotation.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, camera.rotation.x))
previousTouchPosition = {
x: touch.clientX,
y: touch.clientY,
}
}
})
// Camera rotation touch end
domElement.addEventListener("touchend", () => {
touchRotateActive = false
})
// Create a simple object to mimic PointerLockControls interface
const controls = {
isLocked: true, // Always active for mobile
// Update function for animation loop
update: (delta: number) => {
if (joystickActive) {
// Calculate movement direction and distance
const moveDirection = new THREE.Vector3(
joystickPosition.x / maxJoystickDistance,
0,
joystickPosition.y / maxJoystickDistance,
)
// Apply movement in the direction the camera is facing
const cameraDirection = new THREE.Vector3(0, 0, -1)
cameraDirection.applyQuaternion(camera.quaternion)
cameraDirection.y = 0
cameraDirection.normalize()
const cameraSideDirection = new THREE.Vector3(1, 0, 0)
cameraSideDirection.applyQuaternion(camera.quaternion)
cameraSideDirection.y = 0
cameraSideDirection.normalize()
// Calculate movement vector
const moveVector = new THREE.Vector3()
moveVector.addScaledVector(cameraDirection, -moveDirection.z)
moveVector.addScaledVector(cameraSideDirection, moveDirection.x)
moveVector.normalize()
// Apply movement
const moveSpeed = 3.0 * delta
camera.position.addScaledVector(moveVector, moveSpeed)
}
},
}
return controls
}