Quantcast
Viewing all articles
Browse latest Browse all 112

I would like to take the next step in movement and close in (ship movement)

I have the following program - It requires just a Form, PictureBox and Button.

There are 2 questions that I have (I had help with something related to this previously, but these are newer questions).

1. If you click anywhere in the PictureBox, you will notice the heading in degrees in the Title. I would like to set this up, so if the target is in front of myship, it is always noted as 0/360. If it's to the right, then 90, behind 180, and to the left, 270. How can I set this up so I don't interfere with any calculations in the long run.

2. I added a button, which is empty. I would like to add code, that if I click on the button, myship jumps half way the distance between my ship and the target ship. Also, I would like to add another button, that does the same (it jumps a distance), except I would like that to be an increment of a fixed distance. I am working on figuring out how to close in on the target.

Please bear with me, I have a lot of help from vbforums so far, but I am only now learning to correctly understand the math behind this. I would like to better understand Radians, Degrees, the Trig functions, Pythagorean and etc. So if you could please explain your help, I would appreciate it, for example if you fix the angle notation in question 1, then can you please explain how you made the adjustments in case I want to change the coordinate system, so I actually know what I'm doing.

:sick:

Thank you.

:confused:


Code:

Public Class frmMain


    Dim myShips As New List(Of Ship)


    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = Screen.AllScreens(1).Bounds.Location
        CreateShips()
        HeadToTarget()
    End Sub


    Private Sub CreateShips()
        myShips.Add(New Ship("MyShip", New Point(500, 750)))
        myShips.Add(New Ship("EnemyShip", New Point(500, 350)))
    End Sub


    Private Sub pbMap_MouseDown(sender As Object, e As MouseEventArgs) Handles pbMap.MouseDown
        myShips(0).m_pos = New Point(e.X, e.Y)
        pbMap.Invalidate()
        HeadToTarget()
    End Sub


    Private Sub pbMap_Paint(sender As Object, e As PaintEventArgs) Handles pbMap.Paint
        For Each s As Ship In myShips
            e.Graphics.FillRectangle(Brushes.LemonChiffon, s.m_pos.X - 5, s.m_pos.Y - 5, 10, 10)
            e.Graphics.DrawRectangle(Pens.Black, s.m_pos.X - 5, s.m_pos.Y - 5, 10, 10)
            e.Graphics.DrawString(s.m_name, New Font("Arial", 12), Brushes.Black, s.m_pos.X + 15, s.m_pos.Y)
        Next
    End Sub


    Private Sub HeadToTarget()
        Me.Text = ((180 / Math.PI) * Math.Atan2(myShips(0).m_pos.X - myShips(1).m_pos.X, myShips(0).m_pos.Y - myShips(1).m_pos.Y)).ToString
    End Sub


    Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btnMove.Click


    End Sub

    Private Sub btnMove2_Click(sender As Object, e As EventArgs) Handles btnMove2.Click


    End Sub
 End Class


Viewing all articles
Browse latest Browse all 112

Trending Articles