import streamlit as st import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import io # Use Agg backend for headless rendering import matplotlib matplotlib.use("Agg") # --------------------------------------------- # โš™๏ธ PAGE SETUP # --------------------------------------------- st.set_page_config( page_title="Missile Projectile Motion Simulator", page_icon="๐Ÿš€", layout="wide" ) st.title("๐Ÿš€ Missile Projectile Motion Simulator") st.markdown( """ This interactive simulation shows the **2D motion of a missile** under the influence of gravity and air resistance. Adjust the parameters on the sidebar and watch the missile trajectory update in real time! """ ) # --------------------------------------------- # ๐ŸŽ›๏ธ SIDEBAR INPUTS # --------------------------------------------- st.sidebar.header("Simulation Controls") v0 = st.sidebar.slider("Initial Velocity (m/s)", 10, 300, 100) angle_deg = st.sidebar.slider("Launch Angle (degrees)", 5, 85, 45) mass = st.sidebar.slider("Missile Mass (kg)", 1.0, 200.0, 50.0) drag_coeff = st.sidebar.slider("Air Drag Coefficient (0 = none, 1 = strong)", 0.0, 1.0, 0.1) area = st.sidebar.slider("Cross-sectional Area (mยฒ)", 0.01, 1.0, 0.1) time_step = st.sidebar.slider("Time Step (s)", 0.001, 0.05, 0.01) duration = st.sidebar.slider("Simulation Duration (s)", 1.0, 30.0, 10.0) g = 9.81 # gravity (m/sยฒ) rho = 1.225 # air density (kg/mยณ) # --------------------------------------------- # ๐Ÿง  SIMULATION # --------------------------------------------- theta = np.radians(angle_deg) vx, vy = v0 * np.cos(theta), v0 * np.sin(theta) x, y = 0.0, 0.0 positions = [(x, y)] t = 0 while y >= 0 and t < duration: v = np.sqrt(vx**2 + vy**2) F_drag = 0.5 * drag_coeff * rho * area * v**2 # Components of drag Fx_drag = -F_drag * (vx / v) Fy_drag = -F_drag * (vy / v) # Acceleration ax = Fx_drag / mass ay = (Fy_drag / mass) - g # Update velocity vx += ax * time_step vy += ay * time_step # Update position x += vx * time_step y += vy * time_step positions.append((x, y)) t += time_step positions = np.array(positions) range_m = positions[-1, 0] max_height = np.max(positions[:, 1]) # --------------------------------------------- # ๐Ÿ“ˆ STATIC PLOT # --------------------------------------------- fig, ax = plt.subplots(figsize=(8, 4)) ax.plot(positions[:, 0], positions[:, 1], label="Trajectory", color="crimson") ax.set_xlabel("Horizontal Distance (m)") ax.set_ylabel("Height (m)") ax.set_title("Missile Trajectory with Air Resistance") ax.grid(True) ax.legend() ax.set_xlim(0, positions[-1, 0] * 1.1) ax.set_ylim(0, max_height * 1.2) st.pyplot(fig) # --------------------------------------------- # ๐ŸŽž๏ธ ANIMATED PLOT # --------------------------------------------- st.subheader("๐ŸŽฅ Real-Time Simulation") fig2, ax2 = plt.subplots(figsize=(8, 4)) ax2.set_xlim(0, positions[-1, 0] * 1.1) ax2.set_ylim(0, max_height * 1.2) ax2.set_xlabel("Horizontal Distance (m)") ax2.set_ylabel("Height (m)") ax2.set_title("Missile Flight Animation") line, = ax2.plot([], [], color="orange", lw=2) point, = ax2.plot([], [], "ro", markersize=8) def init(): line.set_data([], []) point.set_data([], []) return line, point def update(frame): line.set_data(positions[:frame, 0], positions[:frame, 1]) point.set_data(positions[frame, 0], positions[frame, 1]) return line, point frames = len(positions) ani = FuncAnimation(fig2, update, frames=frames, init_func=init, interval=30, blit=True) buf = io.BytesIO() ani.save(buf, format="gif", fps=30) st.image(buf.getvalue(), caption="Missile Trajectory Animation") # --------------------------------------------- # ๐Ÿงพ RESULTS # --------------------------------------------- st.markdown("### ๐Ÿ“Š Flight Summary") col1, col2 = st.columns(2) col1.metric("Range (m)", f"{range_m:.2f}") col2.metric("Maximum Height (m)", f"{max_height:.2f}")