Reaperxxxx commited on
Commit
c3703f6
·
verified ·
1 Parent(s): a5dfcb3

Update index.html

Browse files
Files changed (1) hide show
  1. 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 checkCameraWallCollision(camPos, playerPos) {
319
- const wallDistance = 1.5; // How close to wall before camera pulls back
320
- const minCamDistance = 2; // Minimum distance from player
321
-
322
- // Define garage walls
323
- const walls = [
324
- { side: 'left', x: -2.2, minZ: -4, maxZ: 3.5 },
325
- { side: 'right', x: 4, minZ: -4, maxZ: 3.5 },
326
- { side: 'front', z: -4, minX: -2.2, maxX: 4 },
327
- { side: 'back', z: 3.5, minX: -2.2, maxX: 4 }
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
- } else {
342
- const wallZ = wall.z;
343
- const distToWall = Math.abs(camPos.z - wallZ);
344
- if (distToWall < wallDistance && camPos.x >= wall.minX && camPos.x <= wall.maxX) {
345
- // Push camera closer to player
346
- newCamPos.z = playerPos.z + (wall.side === 'front' ? minCamDistance : -minCamDistance);
 
 
 
 
 
 
 
 
 
 
 
 
347
  }
348
  }
349
  });
350
-
351
- return newCamPos;
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
- // Update camera (third person) - Fixed: camera stays behind, doesn't move with movement
368
- const cameraX = playerPos.x + Math.sin(cameraRotation.y) * cameraDistance * Math.cos(cameraRotation.x);
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.lookAt(playerPos.x, playerPos.y + 0.5, playerPos.z);
 
 
 
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);