From 44736e8061cf61107ba4de42614a3487bdd4563c Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Mon, 25 Aug 2025 10:30:11 +0200 Subject: [PATCH] u --- src/index.html | 6 +++++ src/index.js | 68 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/index.html b/src/index.html index a26da8d..d745e19 100644 --- a/src/index.html +++ b/src/index.html @@ -26,6 +26,12 @@

In FPV: WASD to move, mouse to look, SPACE to jump, SHIFT to crouch, F or ESC to exit

+ + + diff --git a/src/index.js b/src/index.js index df57e26..dabd952 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,9 @@ let fpvControls = { rotation: { x: 0, y: 0 }, verticalVelocity: 0, isJumping: false, - isCrouching: false + isCrouching: false, + currentHeight: 0, + targetHeight: 0 }; let modelBounds = null; let groundLevel = 0; @@ -952,19 +954,29 @@ function enterFPVMode() { // Disable orbit controls controls.enabled = false; - // Position camera at eye height on the ground level (center of model in X/Z) - const centerX = modelBounds.center.x; - const centerZ = modelBounds.center.z; - camera.position.set(centerX, eyeHeight, centerZ); + // Keep current camera position instead of forcing to model center + // Just ensure the height is reasonable for walking + fpvControls.currentHeight = Math.max(camera.position.y, eyeHeight); + fpvControls.targetHeight = fpvControls.currentHeight; + camera.position.y = fpvControls.currentHeight; - // Reset rotation and movement - fpvControls.rotation.x = 0; - fpvControls.rotation.y = 0; + // Calculate rotation based on current camera orientation + const euler = new THREE.Euler().setFromQuaternion(camera.quaternion, 'YXZ'); + fpvControls.rotation.x = euler.x; + fpvControls.rotation.y = euler.y; + + // Reset movement state fpvControls.velocity.set(0, 0, 0); fpvControls.verticalVelocity = 0; fpvControls.isJumping = false; fpvControls.isCrouching = false; + // Show crosshair + const crosshair = document.getElementById('fpv-crosshair'); + if (crosshair) { + crosshair.style.display = 'block'; + } + // Update button text const fpvBtn = document.getElementById('fpv-btn'); if (fpvBtn) { @@ -993,6 +1005,12 @@ function exitFPVMode() { // Re-enable orbit controls controls.enabled = true; + // Hide crosshair + const crosshair = document.getElementById('fpv-crosshair'); + if (crosshair) { + crosshair.style.display = 'none'; + } + // Update button text const fpvBtn = document.getElementById('fpv-btn'); if (fpvBtn) { @@ -1010,6 +1028,8 @@ function exitFPVMode() { fpvControls.verticalVelocity = 0; fpvControls.isJumping = false; fpvControls.isCrouching = false; + fpvControls.currentHeight = eyeHeight; + fpvControls.targetHeight = eyeHeight; console.log('Orbit controls restored'); }, 50); @@ -1020,9 +1040,10 @@ function updateFPVMovement() { const delta = 0.016; // Approximate 60fps const moveSpeed = 100.0; // Units per second (1000x faster) - const jumpSpeed = 120.0; // Jump initial velocity (4x higher) - const gravity = 120.0; // Gravity strength (4x faster) - const crouchHeight = eyeHeight * 0.6; // Crouch height (60% of normal eye height) + const jumpSpeed = 220.0; // Jump initial velocity (4x higher) + const gravity = 420.0; // Gravity strength (4x faster) + const crouchHeight = eyeHeight * 0.5; // Crouch height (60% of normal eye height) + const crouchSpeed = 100.0; // Speed of crouching transition // Reset direction fpvControls.direction.set(0, 0, 0); @@ -1057,23 +1078,36 @@ function updateFPVMovement() { fpvControls.canJump = false; } + // Update target height based on crouching state + fpvControls.targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight; + // Apply gravity and vertical movement if (fpvControls.isJumping) { fpvControls.verticalVelocity -= gravity * delta; camera.position.y += fpvControls.verticalVelocity * delta; // Check if landed back on ground - const targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight; - if (camera.position.y <= targetHeight) { - camera.position.y = targetHeight; + if (camera.position.y <= fpvControls.targetHeight) { + camera.position.y = fpvControls.targetHeight; + fpvControls.currentHeight = fpvControls.targetHeight; fpvControls.verticalVelocity = 0; fpvControls.isJumping = false; fpvControls.canJump = true; } } else { - // Handle crouching when not jumping - const targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight; - camera.position.y = targetHeight; + // Smooth crouching transition when not jumping + if (Math.abs(fpvControls.currentHeight - fpvControls.targetHeight) > 0.01) { + const direction = fpvControls.targetHeight > fpvControls.currentHeight ? 1 : -1; + fpvControls.currentHeight += direction * crouchSpeed * delta; + + // Clamp to target + if (direction > 0 && fpvControls.currentHeight >= fpvControls.targetHeight) { + fpvControls.currentHeight = fpvControls.targetHeight; + } else if (direction < 0 && fpvControls.currentHeight <= fpvControls.targetHeight) { + fpvControls.currentHeight = fpvControls.targetHeight; + } + } + camera.position.y = fpvControls.currentHeight; } // Apply rotation to camera