VB Tutor VB2022 VB2019 VB6 VB Sample Code About Us
Projectile Simulation

Projectile Simulation Program

Interactive physics simulation with Visual Basic and VB.NET examples


Projectile motion is a fundamental concept in physics that describes the motion of an object thrown or projected into the air, subject to only the acceleration of gravity. This simulation demonstrates the parabolic path that projectiles follow, determined by the launching angle and initial speed.

The equations governing projectile motion are:

y = (v × sin(a)) × t - ½ × g × t²

x = (v × cos(a)) × t

Where v = launching speed, a = launching angle, g = gravity (9.8 m/s²), t = time

Below you'll find an interactive simulation, Visual Basic code examples for both VB6 and VB.NET, and practical exercises to deepen your understanding of physics programming.

Interactive Projectile Simulation

Adjust the parameters below to see how they affect the projectile's trajectory:

Maximum Height: 0 m | Range: 0 m | Flight Time: 0 s

Visual Basic Implementation

Private Sub cmd_Draw_Click()
    Dim x As Single, y As Single
    Dim v As Single, t As Single, a As Single
    Dim g As Single
    
    ' Get input values
    v = Val(Txt_Speed.Text)
    a = Val(Txt_Angle.Text)
    g = 9.8 ' Acceleration due to gravity
    
    ' Clear previous drawing
    Pic_Curve.Cls
    t = 0
    
    ' Draw the ground
    Pic_Curve.Line (0, 120)-(Pic_Curve.Width, 120), vbGreen
    
    ' Calculate and plot trajectory
    Do
        t = t + 0.01
        y = v * Sin(a * 3.141592654 / 180) * t - 0.5 * g * (t ^ 2)
        x = v * Cos(a * 3.141592654 / 180) * t
        
        ' Plot point (adjusting for coordinate system)
        Pic_Curve.PSet (x, 120 - y), vbRed
        
        ' Exit loop when projectile hits ground
        If y < 0 Or x > Pic_Curve.Width Then
            Exit Do
        End If
    Loop
    
    ' Display results
    lblMaxHeight.Caption = "Max Height: " & Format(CalculateMaxHeight(v, a, g), "0.00") & " m"
    lblRange.Caption = "Range: " & Format(x, "0.00") & " m"
    lblFlightTime.Caption = "Flight Time: " & Format(t, "0.00") & " s"
End Sub

Private Function CalculateMaxHeight(v As Single, a As Single, g As Single) As Single
    ' Max height = (v² * sin²(a)) / (2 * g)
    CalculateMaxHeight = (v ^ 2) * (Sin(a * 3.141592654 / 180) ^ 2) / (2 * g)
End Function
Private Sub BtnSimulate_Click(sender As Object, e As EventArgs) Handles BtnSimulate.Click
    Dim v As Double = Double.Parse(TxtSpeed.Text)
    Dim a As Double = Double.Parse(TxtAngle.Text)
    Dim g As Double = 9.8
    Dim t As Double = 0
    Dim x, y As Double
    
    ' Clear previous drawing
    PicCanvas.Image = Nothing
    Using gfx As Graphics = Graphics.FromImage(PicCanvas.Image)
        ' Set up drawing surface
        gfx.SmoothingMode = SmoothingMode.AntiAlias
        gfx.Clear(Color.White)
        
        ' Draw ground
        gfx.DrawLine(Pens.Green, 0, PicCanvas.Height - 30, PicCanvas.Width, PicCanvas.Height - 30)
        
        ' Create pen for trajectory
        Using trajectoryPen As New Pen(Color.Blue, 2)
            Dim points As New List(Of PointF)
            
            ' Calculate trajectory points
            Do
                t += 0.01
                y = v * Math.Sin(a * Math.PI / 180) * t - 0.5 * g * Math.Pow(t, 2)
                x = v * Math.Cos(a * Math.PI / 180) * t
                
                ' Convert to canvas coordinates
                Dim canvasX As Single = x * 10 ' Scale for visualization
                Dim canvasY As Single = PicCanvas.Height - 30 - (y * 10) ' Flip y-axis
                
                points.Add(New PointF(canvasX, canvasY))
                
                ' Exit when projectile hits ground
                If y < 0 Or canvasX > PicCanvas.Width Then Exit Do
            Loop
            
            ' Draw the trajectory
            If points.Count > 1 Then
                gfx.DrawCurve(trajectoryPen, points.ToArray())
            End If
            
            ' Draw launch point
            gfx.FillEllipse(Brushes.Red, 0, PicCanvas.Height - 30, 8, 8)
        End Using
    End Using
    
    ' Display results
    LblMaxHeight.Text = $"Max Height: {CalculateMaxHeight(v, a, g):F2} m"
    LblRange.Text = $"Range: {x:F2} m"
    LblFlightTime.Text = $"Flight Time: {t:F2} s"
    
    ' Refresh canvas
    PicCanvas.Invalidate()
End Sub

Private Function CalculateMaxHeight(v As Double, a As Double, g As Double) As Double
    Return (Math.Pow(v, 2) * Math.Pow(Math.Sin(a * Math.PI / 180), 2) / (2 * g)
End Function

Understanding the Projectile Simulation

The projectile simulation uses fundamental physics equations to calculate the path of an object launched at an angle:

Key Equations

x = v × cos(θ) × t

y = v × sin(θ) × t - ½ × g × t²

Where:

  • x = Horizontal position (meters)
  • y = Vertical position (meters)
  • v = Initial velocity (m/s)
  • θ = Launch angle (degrees)
  • t = Time (seconds)
  • g = Acceleration due to gravity (9.8 m/s²)

The implementation involves:

  1. Converting the angle from degrees to radians
  2. Using a loop to calculate position at small time intervals
  3. Plotting each position point to form the trajectory
  4. Stopping when the projectile hits the ground (y ≤ 0)
  5. Calculating maximum height and range using physics formulas

The VB.NET version improves upon VB6 by:

  • Using double-precision floating point for better accuracy
  • Utilizing the Graphics class for smoother drawing
  • Implementing anti-aliasing for better visual quality
  • Using a curve instead of individual points for the trajectory

Practice Exercises

Test your understanding with these programming exercises:

Exercise 1: Air Resistance

Modify the simulation to account for air resistance. Add a drag coefficient input and adjust the equations to include drag force:

  • Drag force = ½ × ρ × v² × Cd × A
  • Where ρ = air density (1.2 kg/m³), Cd = drag coefficient, A = cross-sectional area
  • Implement Euler integration to update velocity and position

Exercise 2: Multiple Projectiles

Enhance the program to simulate multiple projectiles simultaneously:

  • Allow users to add multiple projectiles with different speeds and angles
  • Display each trajectory in a different color
  • Add collision detection between projectiles
  • Implement a "compare" mode to show differences in trajectories

Exercise 3: Target Practice Game

Create a game where players try to hit targets with projectiles:

  • Place targets at random positions on the screen
  • Let players adjust angle and power (speed)
  • Add wind effects that vary with each shot
  • Keep score based on target distance and number of attempts