Picture box Collision Help? RRS feed

  • General discussion

  • Ok i need a code that when one picturebox hits another it will say collision. Thanks -civilwarrock
    Sunday, May 23, 2010 5:24 AM

All replies

  • Can you describe this any better, in a normal situotion 2 pictureboxes don't have collisions.


    Sunday, May 23, 2010 6:16 AM
  • Well one picturebox is a person and the other is a wall. When the picturebox with the person for a image hits the picturebox with the wall then it wall say collision. Thanks -civilwarrock
    Sunday, May 23, 2010 7:05 AM
  • So this are standard pictureboxes, dragged on the form, with no options changed?

    I still don't see how those can have a collission.

    Sunday, May 23, 2010 8:00 AM
  • How would the two pictureboxes move? User will drag them or using a random number for the control location?


    You will most likely need to handle the move event for the picuturebox control... Btw, is this winform or wpf?

    Sunday, May 23, 2010 8:17 AM

    This code is a picture box ( that is a ball ) that make a collision with a floor

    You just use the same technique for a person with a wall


    Public Class Form1
      Private SpeedY As Integer = 0
      Private Ball As New System.Drawing.Drawing2D.GraphicsPath
      Private BallBox As New Panel With {.Location = New Point(100, 100), .Size = New Size(50, 50)}
      Private Floor As New Panel With {.Size = New Size(Me.Width, 6), .Location = New Point(0, Me.Height - 50)}
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Ball.AddEllipse(0, 0, 50, 50)
        Timer1.Interval = 150
        Timer2.Interval = 50
        Me.DoubleBuffered = True
        Floor.BackColor = Color.Black
        Floor.BorderStyle = BorderStyle.Fixed3D
      End Sub
      '==Gravity Timer
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        SpeedY = SpeedY - 1
      End Sub
      '==Movement Timer
      Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim Loc As Point = BallBox.Location
        If Not BallBox.Bounds.IntersectsWith(Floor.Bounds) Then '< == THIS TEST FOR COLLISION
          BallBox.Location = New Point(Loc.X, Loc.Y - SpeedY)
          SpeedY = 10
          BallBox.Location = New Point(Loc.X, Loc.Y - 5)
        End If
      End Sub
      Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim G As Graphics = BallBox.CreateGraphics
        G.FillPath(Brushes.Red, Ball)
      End Sub
    End Class
    Sunday, May 23, 2010 10:30 AM
  • I think you are doing this the wrong way.  Implementing the graphics will be much simpler if you are maintaining a separate internal 'map' of your objects. 

    Your person should be an object (a class) and your wall should also be an object - perhaps they can be the same class, or they might be different enough to justify separate classes.  They should both contain the information needed to position them, to check for collisions, and to draw them.  Your processing should manipulate these objects as required (including checking for collisions), calculate their new properties (including there position) and then draw them.

    I would guess that you are already maintaining information about the person's direction and speed.  That should be part of the same class that has their position, their health and their skills.  When you move the person you use the position, direction and speed to determine if a collision occurred, and you use direction, speed, health and skills to determine what happens (bounce, die, pass through, dig under, etc).  Then you draw your objects in their new position with their new state.

    Monday, May 24, 2010 12:23 AM