|
-
Mar 30th, 2012, 02:17 PM
#1
Thread Starter
Addicted Member
[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
-
Mar 30th, 2012, 02:25 PM
#2
Re: object collision
This drops a picturebox straight down and has an if/then statement if hit's the bottom
vb Code:
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 1)
If PictureBox1.Bounds.Bottom > Me.Bounds.Bottom Then
'It's hit the bottom
Else
'It didn't hit the bottom
End If
-
Mar 30th, 2012, 03:10 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|