Update index.html
Browse files- index.html +44 -7
index.html
CHANGED
|
@@ -162,13 +162,13 @@
|
|
| 162 |
function init() {
|
| 163 |
scene = new THREE.Scene();
|
| 164 |
scene.background = new THREE.Color(0x1a1a2e);
|
| 165 |
-
scene.fog = new THREE.Fog(0x1a1a2e,
|
| 166 |
|
| 167 |
camera = new THREE.PerspectiveCamera(
|
| 168 |
75,
|
| 169 |
window.innerWidth / window.innerHeight,
|
| 170 |
-
0.
|
| 171 |
-
|
| 172 |
);
|
| 173 |
|
| 174 |
renderer = new THREE.WebGLRenderer({ antialias: true });
|
|
@@ -200,7 +200,15 @@
|
|
| 200 |
garage.traverse((child) => {
|
| 201 |
child.castShadow = true;
|
| 202 |
child.receiveShadow = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
document.getElementById('loading').style.display = 'none';
|
| 205 |
console.log('✅ Garage loaded!');
|
| 206 |
}, undefined, (error) => {
|
|
@@ -306,20 +314,49 @@
|
|
| 306 |
document.getElementById('coordZ').textContent = playerPos.z.toFixed(2);
|
| 307 |
}
|
| 308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
function animate() {
|
| 310 |
requestAnimationFrame(animate);
|
| 311 |
|
| 312 |
// Update player position
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
playerCube.position.set(playerPos.x, playerPos.y, playerPos.z);
|
| 317 |
|
| 318 |
-
// Update camera (third person)
|
| 319 |
const cameraX = playerPos.x + Math.sin(cameraRotation.y) * cameraDistance * Math.cos(cameraRotation.x);
|
| 320 |
const cameraY = playerPos.y + cameraHeight + Math.sin(cameraRotation.x) * cameraDistance;
|
| 321 |
const cameraZ = playerPos.z + Math.cos(cameraRotation.y) * cameraDistance * Math.cos(cameraRotation.x);
|
| 322 |
camera.position.set(cameraX, cameraY, cameraZ);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
camera.lookAt(playerPos.x, playerPos.y + 0.5, playerPos.z);
|
| 324 |
|
| 325 |
updateCoordinates();
|
|
|
|
| 162 |
function init() {
|
| 163 |
scene = new THREE.Scene();
|
| 164 |
scene.background = new THREE.Color(0x1a1a2e);
|
| 165 |
+
scene.fog = new THREE.Fog(0x1a1a2e, 5, 50);
|
| 166 |
|
| 167 |
camera = new THREE.PerspectiveCamera(
|
| 168 |
75,
|
| 169 |
window.innerWidth / window.innerHeight,
|
| 170 |
+
0.01,
|
| 171 |
+
50
|
| 172 |
);
|
| 173 |
|
| 174 |
renderer = new THREE.WebGLRenderer({ antialias: true });
|
|
|
|
| 200 |
garage.traverse((child) => {
|
| 201 |
child.castShadow = true;
|
| 202 |
child.receiveShadow = true;
|
| 203 |
+
// Add physics to garage objects
|
| 204 |
+
if (child.geometry) {
|
| 205 |
+
child.userData.isCollider = true;
|
| 206 |
+
}
|
| 207 |
});
|
| 208 |
+
// Spawn at saved point
|
| 209 |
+
playerPos.x = spawnPoint.x;
|
| 210 |
+
playerPos.y = spawnPoint.y;
|
| 211 |
+
playerPos.z = spawnPoint.z;
|
| 212 |
document.getElementById('loading').style.display = 'none';
|
| 213 |
console.log('✅ Garage loaded!');
|
| 214 |
}, undefined, (error) => {
|
|
|
|
| 314 |
document.getElementById('coordZ').textContent = playerPos.z.toFixed(2);
|
| 315 |
}
|
| 316 |
|
| 317 |
+
function checkCollisions(newPos) {
|
| 318 |
+
if (!garage) return newPos;
|
| 319 |
+
|
| 320 |
+
const raycaster = new THREE.Raycaster();
|
| 321 |
+
const sphere = new THREE.Sphere(new THREE.Vector3(newPos.x, newPos.y, newPos.z), 0.6);
|
| 322 |
+
|
| 323 |
+
let hasCollision = false;
|
| 324 |
+
garage.traverse((child) => {
|
| 325 |
+
if (child.geometry && child.userData.isCollider) {
|
| 326 |
+
const box = new THREE.Box3().setFromObject(child);
|
| 327 |
+
if (box.containsPoint(sphere.center) || box.distanceToPoint(sphere.center) < sphere.radius) {
|
| 328 |
+
hasCollision = true;
|
| 329 |
+
}
|
| 330 |
+
}
|
| 331 |
+
});
|
| 332 |
+
|
| 333 |
+
return hasCollision ? playerPos : newPos;
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
function animate() {
|
| 337 |
requestAnimationFrame(animate);
|
| 338 |
|
| 339 |
// Update player position
|
| 340 |
+
const newPos = {
|
| 341 |
+
x: playerPos.x + moveDirection.x * moveSpeed,
|
| 342 |
+
y: playerPos.y,
|
| 343 |
+
z: playerPos.z + moveDirection.z * moveSpeed
|
| 344 |
+
};
|
| 345 |
+
|
| 346 |
+
playerPos = checkCollisions(newPos);
|
| 347 |
playerCube.position.set(playerPos.x, playerPos.y, playerPos.z);
|
| 348 |
|
| 349 |
+
// Update camera (third person) - Fixed: camera stays behind, doesn't move with movement
|
| 350 |
const cameraX = playerPos.x + Math.sin(cameraRotation.y) * cameraDistance * Math.cos(cameraRotation.x);
|
| 351 |
const cameraY = playerPos.y + cameraHeight + Math.sin(cameraRotation.x) * cameraDistance;
|
| 352 |
const cameraZ = playerPos.z + Math.cos(cameraRotation.y) * cameraDistance * Math.cos(cameraRotation.x);
|
| 353 |
camera.position.set(cameraX, cameraY, cameraZ);
|
| 354 |
+
|
| 355 |
+
// Clamp camera inside garage bounds
|
| 356 |
+
const camClamp = 2;
|
| 357 |
+
camera.position.x = Math.max(-40, Math.min(40, camera.position.x));
|
| 358 |
+
camera.position.z = Math.max(-40, Math.min(40, camera.position.z));
|
| 359 |
+
|
| 360 |
camera.lookAt(playerPos.x, playerPos.y + 0.5, playerPos.z);
|
| 361 |
|
| 362 |
updateCoordinates();
|