Update index.html
Browse files- index.html +38 -44
index.html
CHANGED
|
@@ -152,8 +152,6 @@
|
|
| 152 |
|
| 153 |
let scene, camera, renderer, playerCube, garage;
|
| 154 |
let playerPos = { x: 0, y: 1, z: 0 };
|
| 155 |
-
let cameraDistance = 8;
|
| 156 |
-
let cameraHeight = 3;
|
| 157 |
let moveDirection = { x: 0, z: 0 };
|
| 158 |
let moveSpeed = 0.1;
|
| 159 |
let cameraRotation = { x: 0, y: 0 };
|
|
@@ -315,40 +313,42 @@
|
|
| 315 |
document.getElementById('coordZ').textContent = playerPos.z.toFixed(2);
|
| 316 |
}
|
| 317 |
|
| 318 |
-
function
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
let newCamPos = { ...camPos };
|
| 331 |
-
|
| 332 |
-
// Check distance to each wall
|
| 333 |
-
walls.forEach(wall => {
|
| 334 |
-
if (wall.side === 'left' || wall.side === 'right') {
|
| 335 |
-
const wallX = wall.x;
|
| 336 |
-
const distToWall = Math.abs(camPos.x - wallX);
|
| 337 |
-
if (distToWall < wallDistance && camPos.z >= wall.minZ && camPos.z <= wall.maxZ) {
|
| 338 |
-
// Push camera closer to player
|
| 339 |
-
newCamPos.x = playerPos.x + (wall.side === 'left' ? minCamDistance : -minCamDistance);
|
| 340 |
}
|
| 341 |
-
}
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
}
|
| 348 |
}
|
| 349 |
});
|
| 350 |
-
|
| 351 |
-
return
|
| 352 |
}
|
| 353 |
|
| 354 |
function animate() {
|
|
@@ -364,19 +364,13 @@
|
|
| 364 |
playerPos = checkCollisions(newPos);
|
| 365 |
playerCube.position.set(playerPos.x, playerPos.y, playerPos.z);
|
| 366 |
|
| 367 |
-
//
|
| 368 |
-
|
| 369 |
-
const cameraY = playerPos.y + cameraHeight + Math.sin(cameraRotation.x) * cameraDistance;
|
| 370 |
-
const cameraZ = playerPos.z + Math.cos(cameraRotation.y) * cameraDistance * Math.cos(cameraRotation.x);
|
| 371 |
-
|
| 372 |
-
// Clamp camera inside garage bounds based on actual garage dimensions
|
| 373 |
-
let clampedX = Math.max(-2.2, Math.min(4, cameraX));
|
| 374 |
-
let clampedZ = Math.max(-4, Math.min(3.5, cameraZ));
|
| 375 |
-
let clampedY = Math.max(0.3, Math.min(15, cameraY));
|
| 376 |
-
|
| 377 |
-
camera.position.set(clampedX, clampedY, clampedZ);
|
| 378 |
|
| 379 |
-
camera
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
updateCoordinates();
|
| 382 |
renderer.render(scene, camera);
|
|
|
|
| 152 |
|
| 153 |
let scene, camera, renderer, playerCube, garage;
|
| 154 |
let playerPos = { x: 0, y: 1, z: 0 };
|
|
|
|
|
|
|
| 155 |
let moveDirection = { x: 0, z: 0 };
|
| 156 |
let moveSpeed = 0.1;
|
| 157 |
let cameraRotation = { x: 0, y: 0 };
|
|
|
|
| 313 |
document.getElementById('coordZ').textContent = playerPos.z.toFixed(2);
|
| 314 |
}
|
| 315 |
|
| 316 |
+
function checkCollisions(newPos) {
|
| 317 |
+
if (!garage) return newPos;
|
| 318 |
+
|
| 319 |
+
const raycaster = new THREE.Raycaster();
|
| 320 |
+
const sphere = new THREE.Sphere(new THREE.Vector3(newPos.x, newPos.y, newPos.z), 0.6);
|
| 321 |
+
|
| 322 |
+
let hasCollision = false;
|
| 323 |
+
garage.traverse((child) => {
|
| 324 |
+
if (child.geometry && child.userData.isCollider) {
|
| 325 |
+
const box = new THREE.Box3().setFromObject(child);
|
| 326 |
+
if (box.containsPoint(sphere.center) || box.distanceToPoint(sphere.center) < sphere.radius) {
|
| 327 |
+
hasCollision = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
}
|
| 329 |
+
}
|
| 330 |
+
});
|
| 331 |
+
|
| 332 |
+
return hasCollision ? playerPos : newPos;
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
function checkCollisions(newPos) {
|
| 336 |
+
if (!garage) return newPos;
|
| 337 |
+
|
| 338 |
+
const raycaster = new THREE.Raycaster();
|
| 339 |
+
const sphere = new THREE.Sphere(new THREE.Vector3(newPos.x, newPos.y, newPos.z), 0.6);
|
| 340 |
+
|
| 341 |
+
let hasCollision = false;
|
| 342 |
+
garage.traverse((child) => {
|
| 343 |
+
if (child.geometry && child.userData.isCollider) {
|
| 344 |
+
const box = new THREE.Box3().setFromObject(child);
|
| 345 |
+
if (box.containsPoint(sphere.center) || box.distanceToPoint(sphere.center) < sphere.radius) {
|
| 346 |
+
hasCollision = true;
|
| 347 |
}
|
| 348 |
}
|
| 349 |
});
|
| 350 |
+
|
| 351 |
+
return hasCollision ? playerPos : newPos;
|
| 352 |
}
|
| 353 |
|
| 354 |
function animate() {
|
|
|
|
| 364 |
playerPos = checkCollisions(newPos);
|
| 365 |
playerCube.position.set(playerPos.x, playerPos.y, playerPos.z);
|
| 366 |
|
| 367 |
+
// First person camera - directly at player eye level
|
| 368 |
+
camera.position.set(playerPos.x, playerPos.y + 1.6, playerPos.z);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
// Apply camera rotation for looking around
|
| 371 |
+
camera.rotation.order = 'YXZ';
|
| 372 |
+
camera.rotation.y = cameraRotation.y;
|
| 373 |
+
camera.rotation.x = cameraRotation.x;
|
| 374 |
|
| 375 |
updateCoordinates();
|
| 376 |
renderer.render(scene, camera);
|