Sign in to follow this  

Collision Detecting in Visual Basic 6.0?

This topic is 4279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm working on an RPG in VB 6.0 and I need help with collision detecting for the map. I know how to do it, I was just wondering if there was a better way of going about it. My way is this: I create lines around an object a don't want the player to be able to walk on, such as a house. Here is the code: Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyUp tmrmoveup.Enabled = True Case vbKeyLeft tmrmoveleft.Enabled = True Case vbKeyDown tmrmovedown.Enabled = True Case vbKeyRight tmrmoveright.Enabled = True End Select End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyDown tmrmovedown.Enabled = False img1.Picture = LoadPicture("C:\Documents and Settings\MacGyver1\My Documents\EarthBound - The Early Years\Images\Ness_Standing_Front.gif") Case vbKeyUp tmrmoveup.Enabled = False Case vbKeyLeft tmrmoveleft.Enabled = False Case vbKeyRight tmrmoveright.Enabled = False End Select End Sub Private Sub tmrmovedown_Timer() If img1.Top + 450 = ln4.Y1 And img1.Left > (ln4.X1 - 1) And img1.Left < ln4.X2 Or img1.Top + 450 = ln2.Y1 And img1.Left < ln2.X2 And img1.Left + 450 > ln2.X1 Then Exit Sub Else ln1.Y1 = ln1.Y1 - 25 ln1.Y2 = ln1.Y2 - 25 ln2.Y1 = ln2.Y1 - 25 ln2.Y2 = ln2.Y2 - 25 ln3.Y1 = ln3.Y1 - 25 ln3.Y2 = ln3.Y2 - 25 ln4.Y1 = ln4.Y1 - 25 ln4.Y2 = ln4.Y2 - 25 End If End Sub Private Sub tmrmoveleft_Timer() If img1.Left = ln1.X1 And img1.Top < ln1.Y2 And img1.Top > (ln1.Y1 - 1) Or img1.Left = ln3.X1 And img1.Top + 450 > ln3.Y1 And img1.Top < ln3.Y2 Then Exit Sub Else ln1.X1 = ln1.X1 + 25 ln1.X2 = ln1.X2 + 25 ln2.X1 = ln2.X1 + 25 ln2.X2 = ln2.X2 + 25 ln3.X1 = ln3.X1 + 25 ln3.X2 = ln3.X2 + 25 ln4.X1 = ln4.X1 + 25 ln4.X2 = ln4.X2 + 25 End If End Sub Private Sub tmrmoveright_Timer() If img1.Left + 450 = ln3.X1 And img1.Top > (ln3.Y1 - 1) And img1.Top < ln3.Y2 Or img1.Left + 450 = ln1.X1 And img1.Top < ln1.Y2 And img1.Top + 450 > ln1.Y1 Then Exit Sub Else ln1.X1 = ln1.X1 - 25 ln1.X2 = ln1.X2 - 25 ln2.X1 = ln2.X1 - 25 ln2.X2 = ln2.X2 - 25 ln3.X1 = ln3.X1 - 25 ln3.X2 = ln3.X2 - 25 ln4.X1 = ln4.X1 - 25 ln4.X2 = ln4.X2 - 25 End If End Sub Private Sub tmrmoveup_Timer() If img1.Top = ln2.Y1 And img1.Left < ln2.X2 And img1.Left > (ln2.X1 - 1) Or img1.Top = ln4.Y1 And img1.Left + 450 > ln4.X1 And img1.Left < ln4.X2 Then Exit Sub Else ln1.Y1 = ln1.Y1 + 25 ln1.Y2 = ln1.Y2 + 25 ln2.Y1 = ln2.Y1 + 25 ln2.Y2 = ln2.Y2 + 25 ln3.Y1 = ln3.Y1 + 25 ln3.Y2 = ln3.Y2 + 25 ln4.Y1 = ln4.Y1 + 25 ln4.Y2 = ln4.Y2 + 25 End If End Sub I was thinking of putting the movement into procedures, in attempt to clean up the code and possible lower file sizes (I know it wouldn't be by much, by every bit counts on Dial-Up :P) As you can see by the code (uncommented, sorry :(), I have to add quite a bit of code for every snigle line I use. The map I am using for my game is pretty big and I esmate it would be well over 100 lines, plus I am using multiple maps, so I would have to do it over and over again. Also, in case you were wondering, the movement is supposed to move the map around the player. Is there a better way of doing this?? And here's one last question for now: I know there is a way to save and load integers and variables to a .txt file, but I was wondering if there was a better way of dong this, something a bit less hackable (harder then just opening a file and changing a number). I have about 2 and a half months to finish this project, which is plenty of time in my opinion (at least to finish the basics and get a decent mark) so I am willing to learn other things like simple ActiveX controls or anything like that if I need to in order to do a save feature or better movement. Thanks in advance.

Share this post


Link to post
Share on other sites
Bump. And I remembered about something else I was curious about.

Is there a way to use animated .gif's in vb 6.0 without the use of ActiveX controls? I have the source code for using the ActiveX control on my computer but I don't really understand it that well. I'm still fairly new to VB.

If smoeone could maybe link me to a site with a good explanation of the ActiveX control for using animated .gif's that would be great, or if someone knows of a way to use them without ActiveX.

Thanks in advance.

Share this post


Link to post
Share on other sites
Hey,
Are you using a tile map? Or can objects be at any position?
Quote:
I create lines around an object a don't want the player to be able to walk on, such as a house

I suggest creating a rectangle type for this, if you are not using a tile map.
e.g.
TYPE Ractangle
x1 as INTEGER
y1 as INTEGER
x2 as INTEGER
y2 as INTEGER
END TYPE

and assign a rectangle for each object ...

Quote:
Also, in case you were wondering, the movement is supposed to move the map around the player.

For this type of thing, I would suggest using absolute positions, and screen positions. So if your player moves 1 pixel to the right, you increase your x offset by 1. Then when you draw everything, you add this offset to every single thing you are drawing. This will clean up the code a lot. I think it would be easier as well if you added all the places your guy can't go into an array, which will be an array of Rectangle ... by doing this, this is possible ...
FOR i = 1 TO NUMBER_OF_OBJECTS STEP 1
IF player.x1 > objects(i).x1 AND player.y1 > object(i).y1
IF player.x2 <
' etc.
NEXT i

I hope some of this helped, and that my usually poor grammer doesn't cause any confusion :)
Good luck!

PS: I haven't used BASIC in a while so hopefully I used the right syntax in the examples!

[Edited by - AsOne on March 28, 2006 12:17:06 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by McG7
Hehe, It didn't help that much because I'm not using BASIC, I'm using Visual Basic 6.0 :P

Thanks for the response though :)


The code would be the same.

You need to make your code more data-driven. You could create a text file that described your map and from that create your collision rectangles.

Dots are open area, anything else is something you'd collide into:


XXXXXXXXXX
X........X
X...XX...X
X...XX...X
X........X
XXXXXXXXXX



With this you can create a 10x10 cell map. If each of your cells were 64x64 pixels then all you need to do is check to see if any part of the player is over one of the cells that isn't a dot. I'd write a "CollideRects" type function to figure out if two rects collide.



Type Rect
X As Long
Y As Long
Width As Long
Height As Long
End Type

Function CreateRect(X As Long, Y As Long, Width As Long, Hieght As Long) As Rect
With CreateRect
.X = X
.Y = Y
.Width = Width
.Height = Height
End CreateRect
End Function

' note that you could instead return a rect which is the overlapping
' area, but You're not Going to Need It
Function CollideRects(r1 As Rect, r2 As Rect) As Boolean
<magic collision detection here>
End Function

' Then when you test for collision
Dim r1 As Rect
Dim r2 As Rect
r1 = CreateRect(Player.X, Player.Y, Player.Width, Player.Height)
For r = 0 To Ubound(Map, 1)
For c = 0 To Ubound(Map, 2)
If Map(r, c) <> "." Then
r2 = CreateRect(r * 64, y * 64, 64, 64)
If CollideRects(r1, r2) Then
' Can't move here.
End If
End If
Next
Next


Share this post


Link to post
Share on other sites
Oh, sorry, I just didn't recognize the TYPE syntax. I must be even more of a beginner then I thought :P

I look through the code you gave me and try to learn what most of it is and use that instead of the lines.

Thanks.

Share this post


Link to post
Share on other sites

This topic is 4279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this