Vb.net - Games/Sim Projectile Motion

oussama_1 1 Tallied Votes 2K Views Share

VB.Net never meant for games! it's more recommended for software developement, but hey why not have some fun.
If anyone out there is looking to create an Angry Birds Game-like or a Simulator for throwing an object, you've come to the right place, this code will give you great start/push to build your own app.

Trajectory

dd5f632319f6a4422579ce0a7c3db41e

A Trajectory for a projectile have a bunch of formulas, the more equations you use, the more realistic motion you'll get.
As a start i used only 3 formulas which they're the essentiel ones,the X and Y coordinates and the angle, containing the gravity and the velocity with the respect to time.
Have fun!

ScreenShot:

7e1985143cfac0fb9ff793ae9e0a1476

J.C. SolvoTerra commented: Very Cool, I love this stuff. +3
Imports System.Math
Public Class Form1
    Dim CoordX As Single
    Dim CoordY As Single
    Dim Velocity As Double = 0
    Dim Angle As Double
    Dim Time As Double = 0
    Dim Gravity As Double = 9.8
    Dim moving As Boolean = False
    Dim mousePosX As Integer
    Dim mousePosY As Integer
    Dim X As Integer = 80
    Dim Y As Integer = 250
    Dim picturebox1 As New PictureBox
    Dim Timer1 As New Timer
    Dim Timer2 As New Timer
    Dim Tracking As New RichTextBox
    Dim Angle2 As Single
    Dim label As New Label

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Time += 0.1
        'Trajectory formula for X and Y
        CoordX = Velocity * Time * Cos(Angle)
        CoordY = (Velocity * Time * Sin(Angle)) - ((Gravity / 2) * Time * Time)
        picturebox1.Invalidate() 'draw ball

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim deltaY As Integer = 250 - mousePosY
        Dim deltaX As Integer = mousePosX - 86
        Dim temp As Single
        temp = Math.Atan2(deltaY, deltaX) * 180 / Math.PI
        If Not temp >= 0 Then
            temp += 360
        End If
        Angle2 = temp
        'keep track of variables
        Tracking.Text = " Ball Coord_X: " & CoordX & vbNewLine & " Ball Coord_Y: " & CoordY & vbNewLine _
         & " Gravity: 9.8" & vbNewLine & " Velocity: " & Velocity & vbNewLine & " Angle: " & Angle2

    End Sub



    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' in here i'm checking if the ball is not moving or bouncing then i can drag it
        If Timer1.Enabled = True Then
            moving = False
        Else
            'reset
            CoordX = 0
            CoordY = 0
            Time = 0
            Velocity = 0
            X = mousePosX - 8
            Y = mousePosY - 8
            moving = True
        End If
    End Sub


    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If Timer1.Enabled = False Then
            moving = False
            X = mousePosX - 8
            Y = mousePosY - 8
            Timer1.Interval = 10
            FindAngle()
            Timer1.Start()
            label.Visible = False
        End If
    End Sub

    Public Sub FindAngle()
        ' get the angle between two points according to the X-axis
        ' (250,80) is the center 
        Dim deltaY As Integer = 250 - mousePosY
        Dim deltaX As Integer = mousePosX - 86
        Dim temp As Single
        temp = Math.Atan2(deltaY, deltaX) * 180 / Math.PI

        'get rid of negative value
        If Not temp >= 0 Then
            temp += 360
        End If
        'inverse angle
        'if the ball is at 225 then it's going to shoot at 45 degrees path
        If temp + 180 > 360 Then
            temp += 180
        Else
            temp -= 180
        End If

        'degrees to radians
        temp = temp * PI / 180
        Angle = temp
    End Sub


    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        'drag the ball
        If moving = True Then
            mousePosX = e.X
            mousePosY = e.Y
            picturebox1.Invalidate()
        End If
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

        If moving = True Then

            ' set boundaries
            If mousePosX - 8 < 120 And mousePosX - 8 > 10 Then
                e.Graphics.FillEllipse(Brushes.OrangeRed, mousePosX - 8, mousePosY - 8, 16, 16)
            Else
                If mousePosX - 8 > 120 Then
                    e.Graphics.FillEllipse(Brushes.OrangeRed, 118, mousePosY - 8, 16, 16)
                    mousePosX = 118
                Else
                    e.Graphics.FillEllipse(Brushes.OrangeRed, 12, mousePosY - 8, 16, 16)
                    mousePosX = 12
                End If
            End If



            'set velocity by calculating how much does the ball getting pulled from the center
            'the greater the distance the greater the force
            Dim xx As Integer
            Dim yy As Integer

            If mousePosX > 80 Then
                xx = mousePosX - 80
            Else
                xx = 80 - mousePosX
            End If

            If mousePosY > 250 Then
                yy = mousePosY - 250
            Else
                yy = 250 - mousePosY
            End If

            If yy > xx Then
                Velocity = yy
            Else
                Velocity = xx
            End If



        Else
            'i used single type for location for better drawing
            e.Graphics.FillEllipse(Brushes.OrangeRed, X + CoordX, Y - CoordY, 16, 16)
        End If
        If Y - CoordY > 315 Then
            Timer1.Stop()
            Time = 0
            X = X + CoordX
            Y = Y - CoordY
            ' speed gets smaller as the ball bounces
            Velocity -= 5
            If Velocity < 1 Then
                Timer1.Stop()
                Velocity = 0
                e.Graphics.FillEllipse(Brushes.OrangeRed, 80, 250, 16, 16)
            Else
                Timer1.Start()
            End If

        End If
        e.Graphics.FillEllipse(Brushes.White, 82, 254, 8, 8)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackColor = Color.Black
        Me.Size = New Size(900, 370)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Text = "Trajectory Sim"
        picturebox1.Location = New Point(0, 0)
        picturebox1.Size = New Size(900, 390)
        picturebox1.BackColor = Color.Transparent
        picturebox1.Name = "Picturebox1"
        Tracking.Location = New Point(750, 12)
        Tracking.Size = New Size(200, 106)
        Tracking.BackColor = Color.Black
        Tracking.Cursor = Cursors.Default
        Tracking.BorderStyle = BorderStyle.None
        Tracking.ReadOnly = True
        Tracking.ForeColor = Color.GreenYellow
        Me.Controls.Add(picturebox1)
        Me.Controls.Add(Tracking)
        label.Text = "Drag and release the ball" & vbNewLine & "with your mouse"
        label.Font = New Font("Tahoma", 12, FontStyle.Bold)
        label.ForeColor = Color.White
        label.Size = New Size(220, 40)
        label.Location = New Point(20, 20)
        Me.Controls.Add(label)
        Tracking.BringToFront()
        label.BringToFront()
        AddHandler picturebox1.Paint, AddressOf PictureBox1_Paint
        AddHandler picturebox1.MouseMove, AddressOf PictureBox1_MouseMove
        AddHandler picturebox1.MouseUp, AddressOf PictureBox1_MouseUp
        AddHandler picturebox1.MouseDown, AddressOf PictureBox1_MouseDown
        AddHandler Timer1.Tick, AddressOf Timer1_Tick
        AddHandler Timer2.Tick, AddressOf Timer2_Tick
        Timer2.Start()
    End Sub
End Class
J.C. SolvoTerra 109 Eat, Sleep, Code, Repeat Featured Poster

I love this stuff, brings back the good old days. It's not even a "Complete Game" still I spent a good five minutes pinging balls all over the place.

commented: awesome..10x for your comment +4
demetriusgibson 0 Newbie Poster

is this coding is for games? If it is than can you please help me for motion grapics.Actually I am creating one animation in VB.net as well as 3d motion graphics in New York and I need to put motions on it. So can please send me the code of it.

nguyen_70 commented: You can refer to some more applications at Mod with advanced software that improves the game perfectly +0
joy_731 0 Newbie Poster

Hello! I think the code is very good and practical for someone who is learning to program.
I have a doubt, I wanted to make the ball1 intersect with a block1 that is not a graph.
I tried to do several things but nothing works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.