locked
moving map RRS feed

  • Question

  • I'm trying to develop an app which has an airplane image appearing to fly over a map of the terrain. And the map needs to be moving from top to bottom regardless of the direction of the airplane's movement. In other words, if the airplane turns so many degrees to the left the map turns ever how many degrees to the right so as to make it appear as though the map has the direction that the aircraft is flying always at the top of the screen. Can anyone steer me to the solution. I've tried this repeatedly but I think there is something about programming graphics that I need to learn and I don't know where to start.

    Thanks,

    Daniel

    Tuesday, November 27, 2018 11:36 PM

All replies

  •  Are you familiar with using the Graphics Class and using the drawing methods?  If so, then use the Paint event of the Form with one of the DrawImage overloads and the RotateTransform method to draw the image and rotate it.  If you are not familiar with any of these methods then you should start by learning how to use the Graphics methods before attempting to jump right into creating the 'game' or whatever it is. Otherwise you will most likely be lost when looking at any examples.

     This could get quite complex when you start moving the image top to bottom too.  Not sure how much experience you have but,  this would not be something for a beginner to start off with.

     Anyways,  here is a small example of drawing and rotating an image that you can experiment with.  Create a new Form project and paste this code into it.  Set the path to your image in the very top line.  When you press the left or right arrow keys,  the image will rotate in the way you seem to want.

    Public Class Form1
        Private map As New Bitmap("C:\TestFolder\Wentworth Inn_bg.jpg")
        Private angle As Single = 0
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True
            Me.ResizeRedraw = True
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            Dim pf As New PointF(CSng(Me.ClientSize.Width / 2), CSng(Me.ClientSize.Height / 2))
            With e.Graphics
                .TranslateTransform(pf.X, pf.Y)
                .RotateTransform(angle)
                .TranslateTransform(-pf.X, -pf.Y)
                .DrawImage(map, CSng(pf.X - (map.Width / 2)), CSng(pf.Y - (map.Height / 2)), map.Width, map.Height)
                .ResetTransform()
            End With
        End Sub
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
            Select Case keyData
                Case Keys.Left
                    angle += 5
                    If angle > 359 Then angle = 0
                    Me.Invalidate(False)
                    Return True
                Case Keys.Right
                    angle -= 5
                    If angle < 0 Then angle = 359
                    Me.Invalidate(False)
                    Return True
            End Select
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    End Class
     

     


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, November 28, 2018 12:43 AM
    Wednesday, November 28, 2018 12:36 AM
  • Is this what you mean? 

    Public Class Form6
        Private WithEvents timer1 As New System.Windows.Forms.Timer With {.Interval = 30, .Enabled = True}
        Private map As New Bitmap("c:\bitmaps\mf1b.png")
        Private plane As New Bitmap("c:\bitmaps\plane1.png")
        Private mapOriginPt As PointF
        Private mapRatio As Double = 2
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            mapOriginPt.X = CInt((ClientSize.Width / 2) - (map.Width * mapRatio / 2))
            mapOriginPt.Y = CInt(ClientSize.Height - (map.Height * mapRatio / 2))
            HScrollBar1.Dock = DockStyle.Bottom
            HScrollBar1.Minimum = -180
            HScrollBar1.Maximum = 180
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            'draw map
            e.Graphics.DrawImage(map, mapOriginPt.X, mapOriginPt.Y,
                                 CInt(map.Width * mapRatio), CInt(map.Height * mapRatio))
    
            'plane center
            Dim x2 As Integer = CInt(ClientSize.Width / 2)
            Dim y2 As Integer = CInt(ClientSize.Height / 2)
            'plane upper left location
            Dim x As Integer = CInt(x2 - (plane.Width / 2))
            Dim y As Integer = CInt(y2 - (plane.Height / 2))
    
            'plane transparent color
            Dim attr1 As New System.Drawing.Imaging.ImageAttributes
            attr1.SetColorKey(Color.FromArgb(250, 250, 250), Color.FromArgb(255, 255, 255))
    
            'draw plane
            e.Graphics.TranslateTransform(x2, y2)
            e.Graphics.RotateTransform(HScrollBar1.Value)
            e.Graphics.TranslateTransform(-x2, -y2)
            e.Graphics.DrawImage(plane, New Rectangle(x, y, plane.Width, plane.Height),
                                 0, 0, plane.Width, plane.Height,
                                 GraphicsUnit.Pixel, attr1)
        End Sub
    
        Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick
            mapOriginPt.X += CInt((-1 * Math.Sin(HScrollBar1.Value / 57.3)))
            mapOriginPt.Y += CInt((1 * Math.Cos(HScrollBar1.Value / 57.3)))
            Invalidate()
        End Sub
    
        Private Sub HScrollBar1_ValueChanged(sender As Object, e As EventArgs) Handles HScrollBar1.ValueChanged
            Text = HScrollBar1.Value.ToString
            Invalidate()
        End Sub
    End Class

    Wednesday, November 28, 2018 11:24 AM