Results 1 to 3 of 3

Thread: [RESOLVED] object collision

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Resolved [RESOLVED] object collision

    I want to make a Ball fall and make it stop when it hits the ground. Now I tried this code below, but the ball doesn't move. It thinks the 'collide' is not nothing. But it does not collide. What did I do wrong? Thanks

    Code:
     Dim objects() As PictureBox
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            objects = New PictureBox() {Ball, Ground}
    
        End Sub
       
        Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Integer
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
    
                Dim collide As PictureBox = objects.FirstOrDefault(Function(o) o.Bounds.IntersectsWith(Ball.Bounds))
    
                If collide IsNot Nothing Then
            Else
                Ball.top = Ball.top + 2
            End If
    
    
        End Sub
    
    
    End Class

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,755

    Re: object collision

    This drops a picturebox straight down and has an if/then statement if hit's the bottom
    vb Code:
    1. PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 1)
    2.         If PictureBox1.Bounds.Bottom > Me.Bounds.Bottom Then
    3.             'It's hit the bottom
    4.         Else
    5.             'It didn't hit the bottom
    6.         End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: object collision

    To answer your question, it looks to me like your original code does not work because of the LINQ query you are trying to use.

    objects.FirstOrDefault(Function(o) o.Bounds.IntersectsWith(Ball.Bounds))

    This code says give me the first (or default) object in the objects array, where the object intersects with the ball object. However since the ball object is part of your array of obects, it will always be returned, since its bounds will intersect with itself technically if you are just comparing bounds. So your collide variable is always going to contain the ball picturebox (it will never be nothing).

    Here is intersection checking code that will work:

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Dim collide = Ground.Bounds.IntersectsWith(Ball.Bounds)
    
            If Not collide Then
                Ball.Top = Ball.Top + 2
            End If
    
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width