Question Moon Lander

sisquo76

Active member
Joined
Dec 1, 2008
Messages
28
Location
Bosnia and Herzegovina
Programming Experience
3-5
Hello,

I am working on a small game, Moon Lander and I figured how to make rocket falling from the top of the panel which is on the form. I want to have fuel burning feature so I can land rocket properly on the bottom of the panel. Here is the code so far:

VB.NET:
Public Class Form1
    Dim G As Double = 9.81
    Dim T As Double = 0

    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
        Me.Timer1.Enabled = True
        Me.ButtonStart.Enabled = False
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.PictureBox1.Location = New Point(120, 0)
    End Sub

    Private Function H() As Integer
        Return CInt((G * T ^ 2) / 2)
    End Function

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        T += 0.1
        Me.PictureBox1.Location = New Point(120, H)
        If H() >= 615 Then
            Me.Timer1.Enabled = False
            Me.ButtonStart.Enabled = True
            T = 0
        End If
    End Sub
End Class

If anyone knows how to burn fuel (minus the gravity) so rocket starts to slow down, and eventually stop, then rise up, I would be grateful.

Regards,

Sisquo76
 
It might help to not calculate your gravity like that. I beleive that the calculation that you are using is for a free fall with no other forces being applied to it. A lot of basic game physics are done by adding a certain amount of a velocity each iteration of the timer. Meaning...

VB.NET:
Public Class Form1
    Dim velocity as Double = 0
    dim const gravity as Double = 9.9
  
 Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Apply Gravity Force
        velocity += gravity

        'Move Lander
        PictureBox1.Top += velocity
    End Sub


In order to use a throtle you need to capture when the gas button is pressed and then released. So add a variable to the form that keeps track. As an example i'm using the up arrow as the gas button.

VB.NET:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Up Then
            upPressed = True
        End If
End Sub

    Private Sub Form1_KeyUP(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUP
        If e.KeyCode = Keys.Up Then
            upPressed = False
        End If
End Sub

Now we can add that upwards force the the lander.

VB.NET:
Public Class Form1
    Dim velocity as Double = 0
    dim const gravity as Double = 9.9
    Dim const throtle as double = 30
  
 Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Apply Gravity Force
        velocity += gravity

        'If the gas is pressed then add an upwards force
        if upPressed then
          velocity -= throtle
        end if

        'Move Lander
        PictureBox1.Top += velocity
    End Sub

Now when you first press the gas button the speed at which you fall will begin to slow down and then eventually start going up.
 

Latest posts

Back
Top