From 63c2ced82d62eeb5acaaf0af1a793d82d29709d5 Mon Sep 17 00:00:00 2001 From: Ridwan Sharkar Date: Tue, 19 Nov 2024 10:58:20 -0500 Subject: [PATCH] physics bro wanted hyperbolic trajectory shnarve --- warpgate/Exoplanet.tsx | 68 +++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/warpgate/Exoplanet.tsx b/warpgate/Exoplanet.tsx index 06c6e15..949086f 100644 --- a/warpgate/Exoplanet.tsx +++ b/warpgate/Exoplanet.tsx @@ -2,6 +2,7 @@ import React, { useRef, useEffect, useMemo, useState } from 'react'; import { useFrame } from '@react-three/fiber'; import { Mesh, Vector3, MathUtils, Color } from 'three'; import PlanetTrail from './PlanetTrail'; +import Explosion from './Explosion'; interface ExoplanetProps { onRemove: () => void; @@ -16,7 +17,12 @@ const Exoplanet: React.FC = ({ onRemove }) => { const colors = useMemo(() => ['#00ffff', '#ff00ff', '#FF7F11', '#ff3366', '#66ff33', '#B8B3E9', '#B8B3E9', '#F87666', '6EFAFB', 'BEEE62', 'CD9FCC', '93FF96', 'B2FFA8' ], []); const color = useMemo(() => colors[Math.floor(Math.random() * colors.length)], [colors]); - const SPAWN_RADIUS = 50; // SPAWN POINT + // Constants for simulation + const SPAWN_RADIUS = 50; + const HYPERBOLIC_THRESHOLD = 40; // GRAVITY INFLUENCE + const MIN_DISTANCE = 2; + const BASE_GRAVITY_STRENGTH = 0.15; // GRAVITY STRENGTH + const CLOSE_APPROACH_THRESHOLD = 8; // APPROACH THRESHOLD const targetPoint = useMemo(() => { const radius = Math.random() * 8; // COLLISION ZONE @@ -29,7 +35,7 @@ const Exoplanet: React.FC = ({ onRemove }) => { ); }, []); - // Generates random position on surface with spawn radius + // SPAWNER const initialPosition = useMemo(() => { const theta = Math.random() * 2 * Math.PI; const phi = Math.acos(2 * Math.random() - 1); @@ -45,17 +51,17 @@ const Exoplanet: React.FC = ({ onRemove }) => { } }, [initialPosition]); - // Velocity vector towards the solar system with variration + // Velocity vector towards the solar system with variation useEffect(() => { const speed = Math.random() * 0.4 + 0.04; const direction = targetPoint.clone().sub(initialPosition).normalize(); - // inclination variation ±5° to ±3° + // Inclination variation ±5° to ±3° const inclinationVariation = MathUtils.degToRad(Math.random() * 6 - 3); const axis = new Vector3( 0.9, // x component - Math.random() * 0.1 - 0.05, // y variation + Math.random() * 0.1 - 0.05, // y variation Math.random() * 0.2 - 0.1 // z variation ).normalize(); @@ -65,45 +71,85 @@ const Exoplanet: React.FC = ({ onRemove }) => { // OPACITY const [opacity, setOpacity] = useState(1); + const [showExplosion, setShowExplosion] = useState(false); + const [collisionPoint, setCollisionPoint] = useState(null); // FADE OUT TIMER useEffect(() => { const fadeOutTimer = setTimeout(() => { setOpacity(0); - // Remove after fade-out duration + // Remove after fade-out duration const removeTimer = setTimeout(() => { onRemove(); }, 1000); // 1000 ms = 1 second return () => clearTimeout(removeTimer); - }, 15000); // 20000 ms = 20 seconds + }, 15000); // 15000 ms = 15 seconds return () => clearTimeout(fadeOutTimer); }, [onRemove]); useFrame(() => { if (meshRef.current && velocityRef.current) { - meshRef.current.position.add(velocityRef.current); // Update position + meshRef.current.position.add(velocityRef.current); + + // Proximity detection to the Sun + const distanceToSun = meshRef.current.position.length(); + + // Check for collision with sun + if (distanceToSun < MIN_DISTANCE) { + setCollisionPoint(meshRef.current.position.clone()); + setShowExplosion(true); + setTimeout(() => { + onRemove(); + }, 1500); // 1.5 seconds matching explosion duration + return; + } + + // Apply continuous gravitational force when within threshold + if (distanceToSun < HYPERBOLIC_THRESHOLD) { + // Enhanced gravity when very close to sun + let gravityStrength = BASE_GRAVITY_STRENGTH; + if (distanceToSun < CLOSE_APPROACH_THRESHOLD) { + // Exponentially increase gravity strength for close approaches + gravityStrength *= (CLOSE_APPROACH_THRESHOLD / distanceToSun) ** 1.5; + } + + const gravityMultiplier = gravityStrength / (distanceToSun * distanceToSun); + const gravity = meshRef.current.position.clone() + .normalize() + .multiplyScalar(-gravityMultiplier); + + velocityRef.current.add(gravity); + } } }); return ( - {/* Exoplanet Mesh */} - {/* Trail Effect */} + + {showExplosion && collisionPoint && ( + + )} ); };