Help with collision detection (rectangular) 
Author Message
 Help with collision detection (rectangular)

I'm making a game in VB and need help on how to detect collisions between the
sprites and hidden rectangles on the map.  I also would like to know how you
could tell what direction the sprite was moving when it collided.  Any help
would be greatly appreciated.


Fri, 31 Dec 2004 03:11:55 GMT  
 Help with collision detection (rectangular)
I tried to do this ages ago with coliding circles. Collision with a flat
surface is easy but calculating the direction and velocity of two circles
after they colide is near impossible (at least for me. Anyone got any wizdom
they could share about that?). Anyway since you are dealing with rectangles
it's easy. First if you want them to move i'd give them a vector to follow.
That way you can easily change the direction and velocity should you need
to. First try make a sprite bounce around a form. Deal with collisions
between individual sprites later.
You need to give the sprite an X and Y velocity. It's probably best if I
show an example.

Dim Sprite1X As Integer
Dim Sprite1Y As Integer

Private Sub Form_Load()
  Sprite1X = 50
  Sprite1Y = 50
End Sub

Now make a Timer object and give it an interval of somewhere around 50ms.
Then add code to make the object (Called Sprite1) move.

Private Sub Timer1_Timer()
  Sprite1.Left = Sprite1.Left + Sprite1X
  Sprite1.Top = Sprite1.Top + Sprite1Y
End Sub

Now for the collision detection. Add this to the Timer Event:

  If ((Sprite1.Left + Sprite1.Width) > Me.ScaleWidth) Or (Sprite1.Left < 0)
Then
    Sprite1X = Sprite1X * -1
  End If
  If ((Sprite1.Top + Sprite1.Height) > Me.ScaleHeight) Or (Sprite1.Top < 0)
Then
    Sprite1Y = Sprite1Y * -1
  End If

When the object hits something the X velocity change sign and so goes the
other direction. Because we are using vector form you can change the
object's direction with just two If statements.

I hope that now you can see that once you have detected a collision between
two different sprites it's quite easy to change their direction accordingly.

Alex


Quote:
> I'm making a game in VB and need help on how to detect collisions between
the
> sprites and hidden rectangles on the map.  I also would like to know how
you
> could tell what direction the sprite was moving when it collided.  Any
help
> would be greatly appreciated.



Fri, 31 Dec 2004 18:37:52 GMT  
 Help with collision detection (rectangular)
I take it you are looking to avoid the brute-force aproach?

Dale


Quote:
> I'm making a game in VB and need help on how to detect collisions between
the
> sprites and hidden rectangles on the map.  I also would like to know how
you
> could tell what direction the sprite was moving when it collided.  Any
help
> would be greatly appreciated.



Sat, 01 Jan 2005 00:47:32 GMT  
 Help with collision detection (rectangular)

decided to enlighten us with :

Quote:
>I take it you are looking to avoid the brute-force aproach?

>Dale



>> I'm making a game in VB and need help on how to detect collisions between
>the
>> sprites and hidden rectangles on the map.  I also would like to know how
>you
>> could tell what direction the sprite was moving when it collided.  Any
>help
>> would be greatly appreciated.

Try the IntersectRect API function.

        J.
        Jeremiah D. Seitz
        Porch karaoke king and the guy who runs with 8< scissors >8
        Omega Techware
        http://www.omegatechware.com



Sat, 01 Jan 2005 20:33:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help with collision detection

2. Bad collision detection..Help??

3. Collision detection routines between images

4. Sprites and collision detection

5. Collision detection problems w/ scrolling?

6. Collision detection

7. Collision detection

8. VB InterSectRect Collision detection...

9. Collision Detection

10. collision detection

11. Collision Detection Problems

12. Collision Detection in VB5

 

 
Powered by phpBB® Forum Software