const intersects = raycaster.intersectObject(globe); if(intersects.length > 0) { const point = intersects[0].point; const latLng = pointToLatLng(point); console.log("Clicked at: ", latLng); // If user's location is known, check if clicked location is close if(userLocation) { const distance = computeDistance(latLng.lat, latLng.lng, userLocation.lat, userLocation.lng); console.log("Distance to user: ", distance, "km"); // Threshold set to 500 km for demo (adjust as needed) if(distance < 500) { triggerBlastEffect(); } } else { alert("User location not available."); } } } // Convert intersection point to latitude and longitude function pointToLatLng(point) { // Normalize the point (assuming sphere centered at origin) const r = point.length(); const lat = 90 - (Math.acos(point.y / r)) * 180 / Math.PI; const lng = ((Math.atan2(point.z, point.x)) * 180 / Math.PI); return { lat, lng }; } // Haversine formula to compute distance (in km) between two lat/lng points function computeDistance(lat1, lng1, lat2, lng2) { function toRad(deg) { return deg * Math.PI / 180; } const R = 6371; // Radius of the earth in km const dLat = toRad(lat2 - lat1); const dLng = toRad(lng2 - lng1); const a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLng/2) * Math.sin(dLng/2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); const d = R * c; return d; } // Trigger fake blast effect on the webcam feed function triggerBlastEffect() { const overlay = document.getElementById('blastOverlay'); overlay.style.display = 'flex'; // Optional: You can add screen shake effect by modifying CSS transforms setTimeout(() => { overlay.style.display = 'none'; }, 1000); } // Get user's geolocation function getUserLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { userLocation = { lat: position.coords.latitude, lng: position.coords.longitude }; console.log("User location:", userLocation); }, function(error) { console.error("Geolocation error: ", error); }); } else { alert("Geolocation is not supported by this browser."); } } // Initialize webcam stream function initWebcam() { const video = document.getElementById('webcam'); if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true }) .then(function(stream) { video.srcObject = stream; video.play(); }) .catch(function(err) { console.error("Error accessing webcam: ", err); }); } else { alert("Webcam not supported by this browser."); } } // Event listeners window.addEventListener('load', function() { initGlobe(); getUserLocation(); initWebcam(); renderer.domElement.addEventListener('click', onGlobeClick, false); });