locked
Collision detection/Picturebox grouping RRS feed

  • Question

  • Hey

    My name is mark and I'm currently a year 12 Information processing and technology student. We are currently doing a project in VB.net (2010) in which we have to create a game or application. I have decided to integrate my interest in computers with my interests in chemistry by creating an organic chemistry game. The whole concept of the game is to have the program prompt the user with an organic molecule such as 2-pentene and then having the user construct the molecule from picture boxes which represent the different elements and bonds. I have gotten the basics of the game going but now I am having trouble figuring the collision detection and the grouping out.

    This will be the basic layout of the game. So what I want to be able to do by the end is have the user move the bond (red lines) onto the molecule (big black thing with a C in it) and have it "stick" together into a sort of group so that when you move it, it moves all of the stuck ones together.

    This is the code that is used to create the pictureboxes:

    If cboElementSelect.Text.Contains("Carbon") Then Dim C As New PictureBox With C .Top = 80 .Left = 12 .Width = 39 .Height = 38 .BorderStyle = BorderStyle.None .Image = My.Resources.Carbon .BackColor = Color.Transparent End With Me.Controls.Add(C) AddHandler C.MouseUp, AddressOf Box_MouseUp BoxesC.Add(C) // This repeats for each element and bond , all of the above is stored in lists

    DimBoxesC AsList(OfPictureBox) = NewList(OfPictureBox)

    etc.

    Code that allows the user to move the pictureboxes:

     Private Sub Box_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim C As PictureBox = CType(sender, PictureBox)
            C.Location = Me.PointToClient(C.PointToScreen(e.Location))
        End Sub

    I'm a bit of a noob with coding so please explain it really well, I don't expect you to give me code just tell me what to do and I'll figure it out myself but I don't even have a basic of what to do. Also if you think this is a bit too ambitious then please let me know ASAP so that I can do something a little easier, I won't take offence

    Thank you

    Kind regards

    Mark


    Thursday, April 26, 2012 11:26 AM

Answers

  • Hi Mark,

    Welcome to the MSDN forum.

     Private Sub Box_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim C As PictureBox = CType(sender, PictureBox)
            C.Location = Me.PointToClient(C.PointToScreen(e.Location))
        End Sub

    The above codes set the location of picturebox after mouse moving. Here is the references of Control.PointToClient Method and Control.PointToScreen Method. However, I think it is not the best way to finish this. If you want to move picturebox with the combobox and button, you can add codes in picturebox mousemove method or add them into a group.

    Private Sub PictureBox1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            Static mousePosX As Single, mousePosY As Single
            If e.Button = 0 Then
                mousePosX = e.X
                mousePosY = e.Y
            Else
                PictureBox1.Left = PictureBox1.Left + (e.X - mousePosX)
                PictureBox1.Top = PictureBox1.Top + (e.Y - mousePosY)
                ComboBox1.Left = ComboBox1.Left + (e.X - mousePosX)
                ComboBox1.Top = ComboBox1.Top + (e.Y - mousePosY)
                Button1.Left = Button1.Left + (e.X - mousePosX)
                Button1.Top = Button1.Top + (e.Y - mousePosY)
            End If
        End Sub

    As for Collision detection, it’s a math question in fact. For example, for x location, if one of the controls' left property is < (another controls' left property +its width property), you can consider the control are overlapped.

    If I misunderstood anything, please feel free and let me know.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

    • Marked as answer by Youen Zen Tuesday, May 8, 2012 2:44 AM
    Friday, April 27, 2012 8:06 AM